【问题标题】:Search a cell value in another workbook tab and print data在另一个工作簿选项卡中搜索单元格值并打印数据
【发布时间】:2020-08-23 16:21:31
【问题描述】:

我有一个要求,我有一组数据存储在“Sheet1”列A中,如下所示

Cat
Dog
Mouse
Horse

我还有另一个工作簿 - “Animaldetails.xlsx”,其中有几个选项卡,其中包含“Cat”、“Dog”、“Mouse”、“Horse”和其他几个名称

我需要将“sheet1”中的所有值搜索到“Animaldetails.xlsx”选项卡中,并将每个选项卡中存在的所有数据复制到单独的工作表中 - “sheet2”

谈到我所做的,我尝试了 .Find 方法,但我只能为 1 个单元格而不是多个值。我可以在工作表中搜索,但我想在工作簿的标签中搜索

我知道这可以使用INDIRECT 函数来实现,但我希望通过 VBA 来实现

【问题讨论】:

  • "Cat","Dog" 在标签(工作表)的名称中还是要在单元格中搜索(工作表的数据)?
  • 这些是另一个工作簿中工作表的名称
  • 然后您可以将这些变量用作工作表名称,并将该对象用作从范围中提取数据的参考。
  • @user1838000 是您的工作表的名称完全正确 CatDog 或者它们只是包含这些单词并且实际上命名为 Somthing with Cat in itxyDoggyZ?如果它是第一个,那么我的回答不是最快的方法。
  • 它的名字完全一样。

标签: excel vba


【解决方案1】:

要查找名称为 Cat 的工作表,您需要遍历所有工作表并将其名称与 Cat 进行比较。

例子:

Dim Wb As Workbook
Set Wb = Application.Workbooks("Animaldetails.xlsx")  'or set any other workbook

Dim Ws As Worksheet
For Each Ws In Wb.Worksheets 'loop through all worksheets
    If Ws.Name Like "*Cat*" Then  'note the asterisks as placeholder
        'the worksheet Ws has `Cat` in its name
    End If
Next Ws

不仅要检查Cat,还要检查所有其他人,您需要另一个循环来检查列表中所有名称的每个W:

Dim Wb As Workbook
Set Wb = Application.Workbooks("Animaldetails.xlsx")  'or set any other workbook

Dim TestNameList() As Variant
TestNameList = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10").Value 'adjust range to your data

Dim Ws As Worksheet
For Each Ws In Wb.Worksheets 'loop through all worksheets
    Dim TestName As Variant
    For Each TestName in TestNameList
        If Ws.Name Like "*" & TestName & "*" Then
            'the worksheet Ws has TestName in its name
            Debug.Print Ws.Name & " has " & TestName & " in its name."
            'your copy actions here
            'Ws.Range("A1").Copy Destination:=ThisWorkbook.Worksheets("Sheet2").Range("A1") 'adjust to your needs

            'if every sheetname can only match one of the names then you can exit for here to fasten the code
            Exit For
        End If
    Next TestName
Next Ws

如果您的工作表的名称完全正确 CatDog 那么我们更容易直接使用它们:

Dim Wb As Workbook
Set Wb = Application.Workbooks("Animaldetails.xlsx")  'or set any other workbook

Dim TestNameList() As Variant
TestNameList = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10").Value 'adjust range to your data

Dim TestName As Variant
For Each TestName in TestNameList
    Dim Ws As Worksheet
    Set Ws = Nothing
    On Error Resume Next  'next line throws error if sheet name does not exist
    Set Ws = Wb.Worksheets(TestName)
    On Error Goto 0  're-enable error reporting

    If Not Ws Is Nothing Then
        'Ws is now your worksheet with `TestName`

        'your copy actions here
        'Ws.Range("A1").Copy Destination:=ThisWorkbook.Worksheets("Sheet2").Range("A1") 'adjust to your needs
    Else
        MsgBox "Worksheet '" & TestName & "' not found."
    End If
Next TestName

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多