背景
まず第一に、これらすべてがデータベースにとって完璧なタスクであることに気付きましたが、現在そのオプションを利用することはできず、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