2017-02-27 7 views
0

同じプレフィックスを持つディレクトリ内のすべてのExcelファイルからデータをインポートしようとしています。データはこれらのファイルから集約され、マスターファイルに書き込まれます。私は正常にワイルドカードでDIRを使用して3つのテストファイルを識別できるスクリプトを作成しました。しかし、ファイルを開くコマンドを追加すると、ファイルを開いて閉じた後、最初のパスの後にdoが失敗します。 openとcloseコマンドとdo whileループをコメントアウトすると、テストファイルを識別するループが3回繰り返されます。最終的には、open/closeコマンドを、ファイルを開き、データを集約してマスターファイルに書き込むサブの呼び出しで置き換えたいと思います。これをコード化する方法が変更された場合に備えてこれを言及します。私はフォーラムを検索し、目的の一部を達成するためにいくつかの他の方法を見つけましたが、すべてではありません。 1つの例はファイル名のワイルドカードです。どんな助けもありがとうございます。 Workbooks.Openコマンドでループ内で開いて閉じるコマンドを追加するとExcel VBA Dirループが失敗する

Sub LoopThroughFiles() 

Dim strName As String 
Dim strPath As String 
Dim strFile As String 


strPath = ThisWorkbook.Path 
strName = "Employee Gross Sales" 
strFile = Dir(strPath & "\" & strName & "*") 

Do While Len(strFile) > 0 
    Debug.Print strFile 
' Call OpenFile(strPath, strFile) <-- Eventually will replace open/close commands below 

    Workbooks.Open FileName:=strPath & "\" & Dir(strPath & "\" & strFile) 
' Read/Aggregate/Write data code here or in called sub 
    Workbooks(strFile).Close SaveChanges:=False 

    strFile = Dir 
Loop 

End Sub 

Sub OpenFile(strPath, strFile) 
Dim wbTarget, wbSource As Workbook 

Set wbSource = Workbooks.Open(FileName:=strPath & "\" & Dir(strPath & "\" & strFile)) 
wbSource.Close SaveChanges:=False 

End Sub 
+1

はあなたを持っていますエラーが発生したときに値を確認するためにコードをステップ実行しようとしましたか? – SJR

+0

私はコードをステップ実行しようとしました。私がOpen and Closeコマンドをコメントアウトしたとき、ループの直前のstrFile = Dirは次のファイル名で埋められます。行をコメント解除し、strFile = Dirは ""になりますが、私はなぜそれがわからないのですか? YowE3Kは私をまっすぐにしました。 – Jackyl

答えて

3

あなたDir(strPath & "\" & strFile)は、元Dirを「上書き」されて - あなただけのその時点でstrFileを使用する必要があります。あなたはDirの影響を受けているだけビットにダウンあなたの現在のコードをカットした場合

、それは次のようになります。

strFile = Dir(some_string_including_wildcard) 
'The above statement returns the first file name matching the wildcarded expression 
Do While Len(strFile) > 0 
    ... Dir(specific_filename_being_processed) ... 
    'The above statement finds the first file name matching the specific filename 
    'which will obviously be the specific filename 

    strFile = Dir 
    'That statement gets the next file name matching the argument last used as 
    ' a parameter to a Dir. As the last argument was a specific file, and there 
    ' are no more files matching that argument (because it contained no wildcards) 
    ' this returns an empty string. 
Loop 
あなたのコードは次のように書かれるべき

Sub LoopThroughFiles() 

    Dim strName As String 
    Dim strPath As String 
    Dim strFile As String 

    strPath = ThisWorkbook.Path 
    strName = "Employee Gross Sales" 
    strFile = Dir(strPath & "\" & strName & "*") 

    Do While Len(strFile) > 0 
     Debug.Print strFile 
    ' OpenFile strPath, strFile ' <-- Eventually will replace open/close commands below 

     Workbooks.Open FileName:=strPath & "\" & strFile 
    ' Read/Aggregate/Write data code here or in called sub 
     Workbooks(strFile).Close SaveChanges:=False 

     strFile = Dir 
    Loop 

End Sub 

Sub OpenFile(strPath As String, strFile As String) 
    Dim wbTarget As Workbook, wbSource As Workbook 

    Set wbSource = Workbooks.Open(FileName:=strPath & "\" & strFile) 
    wbSource.Close SaveChanges:=False 

End Sub 
+0

YowE3Kありがとうございました。乾杯。 – Jackyl