0

背景
まず第一に、これらすべてがデータベースにとって完璧なタスクであることに気付きましたが、現在そのオプションを利用することはできず、Excel でこれを続けることは良い学習経験になると思います。

それぞれに識別番号のリストを含む複数のワークブックがあります。以下のコードを使用して、必要なワークブックの名前を入力すると、リストが複数のデータ列を含むメインのワークブックにインポートされます。次に、Match and Export サブを実行して、メイン データ セットを別のシートに分割しました。

質問
各ワークブックを順番に識別する必要がないように、格納フォルダー内の各ファイルに対して for ループを使用する方法はありますか?

Sub Export_Specified_Contractor()

    Dim listwb As Workbook, mainwb As Workbook
    Dim fname As String
    Dim sht As Worksheet, oput As Worksheet
    Dim LRow As Long, oLRow As Long
    Dim cprng As Range, orng As Range

    '--> Get the name of the contractor list to be exported
    fname = Application.InputBox("Enter Contractor Name", "Name?")

    Set mainwb = ThisWorkbook

    With Application

    '--> Set contractor list file
    Set listwb = .Workbooks.Open _
    ("C:\Documents and Settings\alistairw\My Documents\Disallowed Items\Contractor Lists\" & fname)
    End With

    Set sht = listwb.Sheets("Sheet1")

    '--> Copy contractor list
    With sht
        LRow = .Range("A" & Rows.Count).End(xlUp).Row
        .Range("A1:A" & LRow).Copy
    End With

    mainwb.Activate

    '--> Create contractor list sheet in main workbook and paste values
    With mainwb
        On Error Resume Next
        Sheets("Sheet2").Delete
        Sheets.Add.Name = "Sheet2"
        Set oput = Sheets("Sheet2")
        With oput
            .Range("A1").PasteSpecial
        End With
    End With

    Call Match_and_Export

    '--> Delete the list workbook and list sheet
    Application.DisplayAlerts = False
    listwb.Close
    oput.Delete
    Application.DisplayAlerts = True
End Sub
4

1 に答える 1

2

フォルダをループする:

MyPath = "C:\Documents and Settings\alistairw\My Documents\Disallowed Items\Contractor Lists\"
strFilename = Dir(MyPath & "\*.xlsx", vbNormal) 'change to xls if needed

If Len(strFilename) = 0 Then Exit Sub ' exit if no files in folder

Do Until strFilename = ""
    'Your code here
    strFilename = Dir()    
Loop
于 2012-06-06T16:01:28.243 に答える