在搜索循环中设置变量列通过工作表不起作用,

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在搜索循环中设置变量列通过工作表不起作用,相关的知识,希望对你有一定的参考价值。

我正在研究一个宏,需要在“sheet1”中查找范围内的每个值,并在工作簿的所有工作表中搜索它(可能最多7页),因为它需要太长时间,寻找值在整个工作表中,我想将每个工作表上的搜索字段缩减到一个带有标题IP的变量列。

这是我到目前为止的宏,但我无法让它只在指定的范围内工作,但是,删除这些行会使宏工作正常。

先感谢您。

Sub findInventory()
Dim ws As Worksheet
Dim strWhat, rngFound, mString As String
Dim rngSearch, osfind, rfind, rfcol As Range
Dim i, x As Integer
Dim LastRow, oscol, lcol, e, lrowA, remChar, fcol As Long




Sheets("GVM Report").Cells(1, 1).Offset(0, 1).Resize(, 2).EntireColumn.Insert
Sheets("GVM Report").Cells(1, 1).Offset(0, 1).Value = "INVENTORY"
Sheets("GVM Report").Cells(1, 1).Offset(0, 2).Value = "OPSDB"

Set rfind = ActiveWorkbook.Sheets("GVM Report").Rows("1:3").Find(What:="IP", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
lcol = rfind.Column

Set osfind = ActiveWorkbook.Sheets("GVM Report").Rows("1:3").Find(What:="OS*", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
oscol = osfind.Column

LastRow = Sheets("GVM Report").Range("A" & Rows.Count).End(xlUp).Row

For x = 2 To LastRow

strWhat = Sheets("GVM Report").Cells(x, lcol)
    For Each ws In Worksheets
    Set rfcol = ws.Rows("1:3").Find(What:="IP", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    fcol = rfcol.Column
    With ws.Columns(fcol)
    Select Case ws.name
    Case "Operations", "Data", "FYI all OS", "Unique Values", "GVM Report"

    Case Else
       Set rngSearch = ws.Cells.Find(What:=strWhat)
        If strWhat <> "" Then
            If Not rngSearch Is Nothing Then
                i = i + 1
                If i = 1 Then
                    rngFound = rngSearch.Worksheet.name
                Else
                    rngFound = rngFound & " | " & rngSearch.Worksheet.name
                End If
                    End If

        End If

                Sheets("GVM Report").Cells(x, 2) = rngFound


    End Select
    End With
    Next ws
    rngFound = ""
    i = 0
Next x
End Sub
答案

我不确定我是否可以复制你的错误,但我已经整理了你的代码,它可能对你有用,所以这里有:

Sub findInventory()
Dim ws As Worksheet
Dim wsGVM As Worksheet: Set wsGVM = ThisWorkbook.Sheets("GVM Report")
Dim strWhat As String, rngFound As String, mString As String
Dim rngSearch As Range, osfind As Range, rfind As Range, rfcol As Range
Dim i As Integer, x As Long, LastRow As Long, oscol As Long, lcol As Long, fcol As Long

wsGVM.Cells(1, 1).Offset(0, 1).Resize(, 2).EntireColumn.Insert
wsGVM.Cells(1, 1).Offset(0, 1).Value = "INVENTORY"
wsGVM.Cells(1, 1).Offset(0, 2).Value = "OPSDB"

Set rfind = wsGVM.Rows("1:3").Find(What:="IP", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rfind Is Nothing Then lcol = rfind.Column

Set osfind = wsGVM.Rows("1:3").Find(What:="OS*", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not osfind Is Nothing Then oscol = osfind.Column

LastRow = wsGVM.Range("A" & Rows.Count).End(xlUp).Row

For x = 2 To LastRow
    strWhat = wsGVM.Cells(x, lcol)
    For Each ws In Worksheets
        Set rfcol = ws.Rows("1:3").Find(What:="IP", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
        If Not rfcol Is Nothing Then
            fcol = rfcol.Column
            With ws.Columns(fcol)
                Select Case ws.Name
                    Case "Operations", "Data", "FYI all OS", "Unique Values", "GVM Report"
                    ''''
                    Case Else
                        If strWhat <> "" Then
                            Set rngSearch = .Find(What:=strWhat)
                            If Not rngSearch Is Nothing Then
                                i = i + 1
                                If i = 1 Then
                                    rngFound = rngSearch.Worksheet.Name
                                Else
                                    rngFound = rngFound & " | " & rngSearch.Worksheet.Name
                                End If
                            End If
                        End If
                        wsGVM.Cells(x, 2) = rngFound
                End Select
            End With
        End If
    Next ws
    rngFound = ""
    i = 0
Next x
End Sub
另一答案

我用虚拟数据进行了测试,得到了以下结果:

INVENTORY
Sheet2 | Sheet3
Sheet2
Sheet3

这与我创建的虚拟数据所期望的一致。也许存在前导空格或尾随空格或其他不可见的差异导致数据看起来相等的数据不匹配?

以上是关于在搜索循环中设置变量列通过工作表不起作用,的主要内容,如果未能解决你的问题,请参考以下文章

如果全局不起作用,在Python中访问for循环之外的变量?

pgadmin 4 导入/导出表不起作用

迭代lua表不起作用

带有 Ajax + 数据表的 CRUD 表不起作用

实时matplotlib图在循环中不起作用[重复]

LeetCode Q1 二和单遍哈希表不工作