10

この質問は、提案された回答からの長いコメントと更新のために編集されました。

ここで要求されるのはモジュール13です。

Sub SaveInFormat()
Application.DisplayAlerts = False
Workbooks.Application.ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\jammil\Desktop\AutoFinance\ProjectControl\Data\" & Format(Date, "yyyymm") & "DB" & ".xlsx",   leFormat:=51
Application.DisplayAlerts = True
End Sub

また、エラー処理に問題があります。私はそれが間違っていることを知っていますが、私がそれに入る前の瞬間にクローズ関数を修正することにもっと興味があります。これがいくつかの作業を必要とするエラー処理コードです

Sub test()

Dim wk As String, yr As String, fname As String, fpath As String
Dim owb As Workbook

wk = ComboBox1.Value
yr = ComboBox2.Value
fname = yr & "W" & wk
fpath = "C:\Documents and Settings\jammil\Desktop\AutoFinance\ProjectControl\Data"
owb = Application.Workbooks.Open(fpath & "\" & fname)
On Error GoTo ErrorHandler:
ErrorHandler:
If MsgBox("This File Does Not Exist!", vbRetryCancel) = vbCancel Then Exit Sub Else Call Clear

'Do Some Stuff

Call Module13.SaveInFormat

owb.Close

これはあなたのテストコードとファイルパスと名前の変更です

4

2 に答える 2

8

ディスカッションの投稿後、回答が更新されました:

Option Explicit
Sub test()

    Dim wk As String, yr As String
    Dim fname As String, fpath As String
    Dim owb As Workbook

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    wk = ComboBox1.Value
    yr = ComboBox2.Value
    fname = yr & "W" & wk
    fpath = "C:\Documents and Settings\jammil\Desktop\AutoFinance\ProjectControl\Data"

    On Error GoTo ErrorHandler
    Set owb = Application.Workbooks.Open(fpath & "\" & fname)

    'Do Some Stuff

    With owb
        .SaveAs fpath & Format(Date, "yyyymm") & "DB" & ".xlsx", 51
        .Close
    End With

    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
        .EnableEvents = True
    End With

Exit Sub
ErrorHandler: If MsgBox("This File Does Not Exist!", vbRetryCancel) = vbCancel Then

Else: Call Clear

End Sub

エラー処理:

特定のエラーをキャッチするには、次のようなことを試すことができます。

    On Error Resume Next
    Set owb = Application.Workbooks.Open(fpath & "\" & fname)
    If Err.Number = 1004 Then
    GoTo FileNotFound
    Else
    End If

    ...
    Exit Sub
    FileNotFound: If MsgBox("This File Does Not Exist!", vbRetryCancel) = vbCancel Then

    Else: Call Clear
于 2012-10-18T13:25:17.037 に答える
2

私はいくつかの異なることに答えようとしますが、私の貢献はあなたの質問のすべてをカバーしていないかもしれません。たぶん私たちの何人かはこれから異なるチャンクを取り出すことができます。ただし、この情報は役に立ちます。どうぞ..

別のファイルを開く:

ChDir "[Path here]"                          'get into the right folder here
Workbooks.Open Filename:= "[Path here]"      'include the filename in this path

'copy data into current workbook or whatever you want here

ActiveWindow.Close                          'closes out the file

指定された日付のファイルが存在する場合はそれを開く:

ディレクトリを検索してファイルが存在するかどうかを確認する方法はわかりませんが、私の場合はわざわざ検索する必要はありません。ディレクトリを開いてエラーチェックを実行し、存在しない場合はエラーチェックを実行します。存在しない場合は、このメッセージを表示するか、xyzを実行してください。

いくつかの一般的なエラーチェックステートメント:

On Error Resume Next   'if error occurs continues on to the next line (ignores it)

ChDir "[Path here]"                         
Workbooks.Open Filename:= "[Path here]"      'try to open file here

または(より良いオプション):

存在しない場合は、メッセージボックスまたはダイアログボックスを表示して、「ファイルが存在しません。新しいファイルを作成しますか?」と表示します。

GoTo ErrorHandlerこれを実現するには、以下に示すものを使用する可能性があります

On Error GoTo ErrorHandler:

ChDir "[Path here]"                         
Workbooks.Open Filename:= "[Path here]"      'try to open file here

ErrorHandler:
'Display error message or any code you want to run on error here

ここでのエラー処理に関する詳細情報:http ://www.cpearson.com/excel/errorhandling.htm


また、VBAで詳細を知りたい場合、またはより一般的に知る必要がある場合は、Siddharth Routのサイトをお勧めします。彼には、チュートリアルとサンプルコードがたくさんあります: http ://www.siddharthrout.com/vb-dot-net-and-優れている/

お役に立てれば!


エラーコードが毎回実行されないようにする方法の例:

エラーハンドラの前にコードをデバッグするExit Subと、エラーがあるかどうかを確認するたびにエラーハンドラが実行されることがすぐにわかります。コード例の下のリンクは、この質問に対する以前の回答を示しています。

  Sub Macro

    On Error GoTo ErrorHandler:

    ChDir "[Path here]"                         
    Workbooks.Open Filename:= "[Path here]"      'try to open file here

    Exit Sub      'Code will exit BEFORE ErrorHandler if everything goes smoothly
                  'Otherwise, on error, ErrorHandler will be run

    ErrorHandler:
    'Display error message or any code you want to run on error here

  End Sub

また、これがどのように機能するかについてさらに参照する必要があるので、この他の質問を見てください: gotoブロックが機能しないVBA


于 2012-10-18T13:07:25.070 に答える