我需要一种解决方案来遍历一系列区域,以打开与那些地区相关的摘要文件,然后使用单独的一系列区域名称,遍历那些地区文件中特定于该领域的工作表。目的是将数据从特定区域的列下的第二个工作簿复制到“区域摘要”文件中的相应区域工作簿中。
地区范围在一个工作簿的一列中,而地区范围在第二个工作簿的第一行中。第一个工作簿中的行数和第二个工作簿中的列数可能因月份而异。您能建议如何最好地做到这一点吗?联盟选项引发了错误。这是我的部分代码:
Sub CustBanks3()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Dim WBMaster As Workbook, CustBank As Workbook, SSBMaster As Workbook, DistMaster As Workbook, CurDstTerrFile As Workbook
Dim wsCB As Worksheet, wsSSB As Worksheet
Dim PivotCol As Range, PivotRow As Range, SSBCol As Range, SSBRow As Range
Dim Period As String, YYYY As Variant
Dim DistrictDSM As Range, DistrictsDSMList As Range, CBTerr As Range, CBTerrList As Range, rLastCell As Range
Dim Path As String, DistPeriodFile As String, Territory As String, District As String, PCol As String, CBDist As String
Dim wsFind As Worksheet, Wksht As Worksheet
Dim wsCount As Integer, x As Integer, Z As Integer, FoundFlag As Boolean, LastCol As Long
Set WBMaster = ThisWorkbook
Period = WBMaster.Sheets("START").Range("C6").Value
YYYY = WBMaster.Sheets("START").Range("C8").Value
WBMaster.Sheets("START").Activate
Set DistrictsDSMList = Range("E11:E" & Cells(Rows.Count, "E").End(xlUp).Row)
Set CustBank = Workbooks.Open("H:\Accounting\Monthend " & YYYY & "\Customer Bank Reports\CB " & Period & Right(YYYY, 2) & ".xlsx")
CustBank.Worksheets("Terr Summary").Activate
Set Wksht = CustBank.Worksheets("Terr Summary")
Set rLastCell = Wksht.Cells.Find(What:="*", After:=Wksht.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
LastCol = rLastCell.Column
Set CBTerrList = CustBank.Worksheets("Terr Summary").Range("A2:A" & LastCol)
For Each CBTerr In Union(DistrictsDSMList, CBTerrList).Cells 'IS THIS HOW TO DO IT???
Set DistMaster = Workbooks.Open("H:\Accounting\Monthend " & YYYY & "\DSM Files\DSM Master Reports\" & DistrictDSM & ".xlsx", _
UpdateLinks:=False)
wsCount = DistMaster.Sheets.Count
District = Left(DistrictDSM, InStr(DistrictDSM, " DSM"))
For x = 1 To wsCount
If DistMaster.Worksheets(x).name = Territory Then
FoundFlag = True
CurDstTerrFile.Sheets("Index").Range("F20").Copy 'PM
DistMaster.Sheets(Territory).Range(PCol & "3").PasteSpecial Paste:=xlPasteValues
End If
Next x
Loop
End sub