【问题标题】:Reference a different workbook参考不同的工作簿
【发布时间】:2021-05-06 16:48:14
【问题描述】:

我在这里使用指南:Find and replace in a loop using macro in Excel

我有一个工作簿“TLA Lookup.xlsx”保存在共享位置。它有一个名为 TLAs 的工作表。 A 列由 TLA 列表组成,B 列是相应的企业名称。

我有一个宏来检查 TLA 查找工作簿,只要存在 TLA,就将其替换为公司名称。我可以在同一个工作簿中执行此操作。

我有不同的工作簿,希望在其中进行查找/替换。我每次都必须从 TLA Lookups 工作簿中复制 TLA 表。我想改为自动引用该工作簿。

如何将 TLA 查找引用为宏需要在其中查找/替换文本的工作簿?

Sub find_replace_2()
    Dim TLA As String
    Dim NAME As String
    Dim i As Long
    Dim wb As Workbook
    Dim sht1 As Worksheet
   
    'Open the Workbook that has all of the TLAs and CI Names from the K drive,
    ' so now both workbooks are open

    Workbooks.Open Filename:= _
      "K:\CLE01\Team_QA\Upcoming Change Highlights\TLA Lookup.xlsx"
        
    Set wb = TLA Lookup.xlsx    ' <----  Here is where I get a syntax error
    Set sht1 = wb.Sheets("TLAs")  

    For i = 1 To 4000
        TLA = wb.sht1.Range("A" & i).Value
        NAME = wb.sht1.Range("B" & i).Value

        Selection.Replace What:=TLA, replacement:=NAME _
          , LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat _
          :=False, ReplaceFormat:=False            
    Next i

End Sub 

【问题讨论】:

    标签: excel vba loops


    【解决方案1】:

    您可以轻松做到这一点,但您必须了解ThisWorkbookActiveWorkbook 之间的区别。 (请参阅解释 hereherehere 以获得一些指导。)

    有了这些知识,只需清楚您在代码中引用的是哪个工作簿:

    编辑:下面示例中的扩展代码

    Option Explicit
    
    Sub find_replace_3()
        Dim tlaLookupWB As Workbook
        Dim tlaSheet As Worksheet
        Set tlaLookupWB = Workbooks.Open(Filename:= _
            "K:\CLE01\Team_QA\Upcoming Change Highlights\TLA Lookup.xlsx")
        Set tlaSheet = tlaLookupWB.Sheets("TLAs")
        
        '--- determine how many rows of TLAs exist
        Dim numberOfTLAs As Long
        With tlaSheet
            numberOfTLAs = .Cells(.Rows.Count, 1).End(xlUp).Row
        End With
        
        '--- replacements will be made in the currently active workbook
        Dim wb As Workbook
        Set wb = ActiveWorkbook
        
        '--- now check all of the TLAs and make replacements if found
        Dim i As Long
        For i = 1 To numberOfTLAs
            Dim tla As String
            Dim name As String
            tla = tlaSheet.Cells(i, 1).Value
            name = tlaSheet.Cells(i, 2).Value
                
            '--- search all of the worksheets in the current workbook
            '    and replace the tla with the name
            Dim ws As Worksheet
            For Each ws In wb.Sheets
                ws.Cells.Replace What:=tla, Replacement:=name, _
                                 LookAt:=xlWhole, SearchOrder:=xlByRows, _
                                 MatchCase:=False, SearchFormat:=False, _
                                 ReplaceFormat:=False
            Next ws
        Next i
        
        tlaWB.Close SaveChanges:=False
    End Sub
    

    【讨论】:

    • 谢谢,这确实帮助我理解了我缺少的工作簿。但是现在实际的替换不起作用。并且评论部分不允许我将新代码放入大声笑中。它会打开和关闭 TLA 查找工作簿,但从不进行实际替换。
    • @Shae1999 - 上面提供的示例中的代码适用于我自己的示例数据。
    • 我看到正在发生的事情是它正在进行更改,但在查找表而不是我正在使用的工作表上。我认为我仍然遇到 ActiveWorkbook 与 ThisWorkbook 的问题。跨度>
    • 永远不应编辑 tlaLookup 工作簿。我在“book1”中,我有一个突出显示的单元格,我希望宏引用 tlaLookup 工作簿,Book1 单元格 a1 中存在的 TLA 的列 A,并将其替换为匹配行的 B 列的内容。所以 book1 中的“ABC”现在变成了“Alpha Bravo Charlie”,因为 tlaLookup 在单元格 a4 中显示 ABC,在 b4 中显示 Alpha Bravo Charlie。
    • 请修改代码示例以匹配您的数据、工作簿和环境。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多