简体   繁体   English

在不同的工作簿中搜索工作表; 如果找不到匹配项,则添加工作表

[英]Search for Worksheet in Different Workbook; Add Worksheet if No Match Found

I have MONTHLY data by District, Territory, and Period (Period=Month) in a series of approximately 500 workbooks.在大约 500 个工作簿的系列中,我有按地区、领土和期间(期间 = 月)划分的每月数据。 There is a MASTER workbook for each District, with separate worksheets for each Territory summarizing the monthly data.每个地区都有一个 MASTER 工作簿,每个地区都有单独的工作表汇总每月数据。

The process needed is as follows.所需的过程如下。 Open each District's MASTER workbook, open each District's MONTHLY files, find the Territory worksheet in the MASTER workbook that matches the Territory identified in a cell in the MONTHLY file, paste the monthly data in the MASTER workbook's Territory worksheet, close the monthly file, then loop to the next monthly file.打开每个 District 的 MASTER 工作簿,打开每个 District 的 MONTHLY 文件,在 MASTER 工作簿中找到与 MONTHLY 文件中单元格中标识的 Territory 匹配的 Territory 工作表,将每月数据粘贴到 MASTER 工作簿的 Territory 工作表中,关闭月度文件,然后循环到下一个月度文件。

I need to create a NEW Territory worksheet if a Territory is added to a District's MONTHLY files sometime after the District's MASTER workbook was initially created.如果在最初创建 District 的 MASTER 工作簿后的某个时间将 Territory 添加到 District 的 MONTHLY 文件中,我需要创建一个 NEW Territory 工作表。

The code, it seems, is not evaluating the current MONTHLY Territory name against ALL possible MASTER workbook Territory worksheet names.代码似乎没有根据所有可能的 MASTER 工作簿 Territory 工作表名称评估当前的 MONTHLY Territory 名称。
If a match is found, it should copy, paste, close the monthly file, and loop to the next MONTHLY file.如果找到匹配项,它应该复制、粘贴、关闭每月文件,然后循环到下一个 MONTHLY 文件。
If no match is found, then it should create a new worksheet, copy, paste, close the monthly file, and loop to the next MONTHLY file.如果没有找到匹配项,那么它应该创建一个新的工作表,复制、粘贴、关闭每月文件,然后循环到下一个 MONTHLY 文件。

Sub DSMReportsP02()

    Dim DistrictDSM As Range, DistrictsDSMList As Range
    Dim Period As String, Path As String, DistPeriodFile As String, Territory As String
    Dim YYYY As Variant
    Dim WBMaster As Workbook, DistMaster As Workbook, CurDstTerrFile As Workbook
    Dim wsFind As Worksheet, SheetXXX As Worksheet
    Dim wsCount As Integer, x As Integer
    Set WBMaster = ActiveWorkbook
    Period = Range("C6").Value
    YYYY = Range("C8").Value

    Set DistrictsDSMList = Range("E11:E" & Cells(Rows.Count, "E").End(xlUp).Row)

    For Each DistrictDSM In DistrictsDSMList.Cells
        Workbooks.Open Filename:="H:\Accounting\Monthend " & YYYY & "\DSM Files\DSM Master Reports\" & DistrictDSM & ".xlsx"
        Set DistMaster = ActiveWorkbook
        wsCount = Application.Sheets.Count
        Path = "H:\Accounting\Monthend " & YYYY & "\DSM Files\" & DistrictDSM & "\P02"
        DistPeriodFile = Dir(Path & "\*.xlsx")

        Do While DistPeriodFile <> ""
            Workbooks.Open Filename:=Path & "\" & DistPeriodFile, UpdateLinks:=False
            DistPeriodFile = Dir
            Set CurDstTerrFile = ActiveWorkbook
            Territory = CurDstTerrFile.Sheets("Index").Range("A3").Value

            For x = 1 To wsCount
                If DistMaster.Worksheets(x).name = Territory Then
                    CurDstTerrFile.Sheets("Index").Range("F20").Copy 'PM
                    DistMaster.Sheets(Territory).Activate
                    Range("C3").PasteSpecial Paste:=xlPasteValues
                End If

                If DistMaster.Worksheets(x).name <> Territory Then
                    CurDstTerrFile.Sheets("Index").Range("F20").Copy 'PM
                    WBMaster.Sheets("ReptTemplate").Activate
                    Range("C3").PasteSpecial Paste:=xlPasteValues
                    WBMaster.Sheets("ReptTemplate").Copy after:=DistMaster.Sheets(DistMaster.Sheets.Count)
                    DistMaster.Sheets("ReptTemplate").name = DistMaster.Sheets("ReptTemplate").Range("A1").Value
                End If
                CurDstTerrFile.Close
            Next x
        Loop
    Next DistrictDSM
End Sub

I believe the following will do what you expect, it may be worthwhile to note that fully qualifying your ranges is recommended specially when working with various workbooks/worksheets, so as to not confuse the code when referring to the ActiveSheet, so I would further amend the code to make sure each range is pointing to the desired workbook/worksheet:我相信下面会做你所期望的,可能值得注意的是,在使用各种工作簿/工作表时,特别建议完全限定你的范围,以免在引用 ActiveSheet 时混淆代码,所以我会进一步修改确保每个范围都指向所需工作簿/工作表的代码:

Sub DSMReportsP02()
Dim DistrictDSM As Range, DistrictsDSMList As Range
Dim Path As String, DistPeriodFile As String, Territory As String, YYYY As String
Dim WBMaster As Workbook, DistMaster As Workbook, CurDstTerrFile As Workbook
Dim wsCount As Integer, x As Integer
Dim FoundFlag As Boolean
Set WBMaster = ThisWorkbook
Period = Range("C6").Value
YYYY = Range("C8").Value
FoundFlag = False

Application.ScreenUpdating = False
    Set DistrictsDSMList = Range("E11:E" & Cells(Rows.Count, "E").End(xlUp).Row)

    For Each DistrictDSM In DistrictsDSMList.Cells
        Set DistMaster = Workbooks.Open("H:\Accounting\Monthend " & YYYY & "\DSM Files\DSM Master Reports\" & DistrictDSM & ".xlsx")
        wsCount = DistMaster.Sheets.Count
        Path = "H:\Accounting\Monthend " & YYYY & "\DSM Files\" & DistrictDSM & "\P02"
        DistPeriodFile = Dir(Path & "\*.xlsx")

        Do While DistPeriodFile <> ""
            Set CurDstTerrFile = Workbooks.Open(Path & "\" & DistPeriodFile)
            DistPeriodFile = Dir
            Territory = CurDstTerrFile.Sheets("Index").Range("A3").Value

            For x = 1 To wsCount
                If DistMaster.Worksheets(x).Name = Territory Then
                    FoundFlag = True 'set flag to found if worksheet found
                    DistMaster.Sheets(Territory).Range("C3").Value = CurDstTerrFile.Sheets("Index").Range("F20").Value 'PM
                End If
            Next x
            If FoundFlag = False Then 'if worksheet not found then add it
                WBMaster.Sheets("ReptTemplate").Range("C3").Value = CurDstTerrFile.Sheets("Index").Range("F20").Value 'PM
                WBMaster.Sheets("ReptTemplate").Copy After:=DistMaster.Sheets(DistMaster.Sheets.Count)
                DistMaster.Sheets("ReptTemplate").Name = DistMaster.Sheets("ReptTemplate").Range("A1").Value
            End If
            CurDstTerrFile.Close
            FoundFlag = False 'reset the flag before next iteration of the loop
        Loop
    Next DistrictDSM
Application.ScreenUpdating = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM