如果通过搜索找到单元格值,则返回工作表名称

时间:2018-12-04 01:15:09

标签: excel vba office365

我总是坚持那些看似简单的方法。由于IFS公式不想超越第一组,我认为一个简单的VBA脚本将是一个快速解决方案。

我想在同一工作簿的C列中查找字符串,在其他工作表中查找,并在A列中查找找到的工作表名称。

我的主页有4列; 备份作业;大小;名称;注释 (我们将忽略此查询的大小和注释) 我在其他工作表(例如EG)中搜索的字母数字填充了“名称”列(C):

  • ARCDC001
  • BENDC001
  • BRODC001
  • CAPDC001
  • GWFNSWAPP010
  • GWFNSWSQL001

所有这些都在数据表中。

我的“ SheetList”工作表具有其他工作表的名称。这使我可以更改搜索顺序 例如。

  • 7y地球
  • 已启用7y-GRT
  • 已禁用7y-GRT
  • 已启用2y-GRT
  • 已禁用2y-GRT

我的数据表可以与其他表重复,我只希望找到第一个实例。

不会出现找不到数据的情况。

到目前为止,我得到的最接近的Ive在我的vLookup中给了我一个堆栈溢出错误。 您能看到我该如何解决吗? Workbook in question (xlsm)

'

Sub BackupJob()

Dim bCheck As Boolean, aJobName As Object, aJobList As Object, aServer As Object, vaLookup As Variant, lLastRow As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim rJobName As Range
Set wb = ThisWorkbook
Set rserverlist = Application.Range("ALL_DCA!C2:C209")
Set rJobList = wb.Worksheets("Sheetlist").Range("JobList")




For Each aServer In rserverlist.Cells


    For Each rJobName In rJobList.Cells
        Do Until bCheck = True

            With wb.Worksheets(rJobName.Value2)
                lLastRow = .Cells(Rows.Count, "A").End(xlUp).Row


                Dim vaSearch As Variant
                vaSearch = ((rJobName.Value2) & "!" & "A1:A" & lLastRow)

                ' if vaLookup is a variable it will give "error2015", which is Stack Overflow.
                ' but this is the closest I've gotten so far.
                vaLookup = Application.VLookup(aServer, vaSearch, 1, False)
                ' vaLookup = Application.VLookup(aServer, ((rJobName.Value2) & "!" & "A1:A" & lLastRow), 1, False)
                    If vaLookup = aServer Then
                        bCheck = True
                    Else
                        bCheck = False
                    End If
            End With
        Loop
    Next rJobName

rserverlist.Offset(-2, 0) = aJobName

Next aServer

结束子

2 个答案:

答案 0 :(得分:1)

我会尝试另一种方法。

Sub BackupJob()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim rngName As Range, rngSheet As Range
    Dim cllName As Range, cllSheet As Range

    Set wb = ThisWorkbook

    Set rngName = Range("ALL_DCA!C2:C209")
    Set rngSheet = wb.Worksheets("Sheetlist").Range("JobList")

    For Each cllName In rngName

        For Each cllSheet In rngSheet

            Set rSearch = Worksheets(cllSheet.Value).Range("A1").CurrentRegion

            With rSearch
                    Set c = .Find(cllName.Value2, _
                                LookIn:=xlValues, _
                                searchdirection:=xlNext)

                    If Not c Is Nothing Then
                        cllName.Offset(0, 1).Value = cllSheet.Value
                        Exit For
                    End If

            End With

        Next
    Next

End Sub

已测试:

enter image description here

答案 1 :(得分:0)

已编辑 欢迎来到SO。我认为如果按工作表查找可能会更容易。我在您的文件上运行了此示例,并按预期填充了该文件。

Sub BackupJob()

    Dim rCell As Range, rserverList As Range, ws As Worksheet
    Dim rJobName As Range
    Set rserverList = Application.Range("ALL_DCA!C2:C209")
    Set rJobList = Range("JobList")

    'Loop through your name of sheets first
    For Each rCell In rJobList.Cells
            'checks for sheets that match cell value
            For Each ws In ThisWorkbook.Worksheets

                If ws.Name = rCell.Value Then

                    'Loops through your full list
                    For Each rJobName In rserverList.Cells
                        'this is where your test searches.
                        'Countif is a great fast function for things like this.
                        If Application.WorksheetFunction.CountIf(ws.Columns(1), rJobName.Value) > 0 Then
                            rJobName.Offset(0, rJobName.Column * -1 + 1).Value = ws.Name

                        End If

                    Next rJobName


                End If

            Next ws

    Next rCell

End Sub