在 Excel 工作簿中的所有工作表中展开当前 VBA 搜索代码

Expand current VBA search code across all sheets in excel workbook

提问人:RadioEye 提问时间:9/21/2023 最后编辑:JohnMRadioEye 更新时间:9/21/2023 访问量:46

问:

我已经为我的问题找到了很多答案,但不幸的是,由于我缺乏经验,我无法将其他解决方案重新应用于我的代码。

我试图完成的是使用我当前的工作代码,我在其中输入一个我想在工作表中搜索的字符串,并将其扩展到其他工作表上。

顺便说一句,我不确定“仅将我当前的代码扩展”到其他工作表是否足够(我猜不是),而不添加某种循环以确保我不会在第一次成功搜索后卡在最后一个工作表上?

无论哪种方式,我尝试过的方法都没有奏效。但它也没有引起任何错误或变化。该代码仍然像我尝试将其扩展到多个工作表之前一样工作。

    Private Sub suchbutton_Click()

    Dim fullstring As String, searchstring As String
    Dim partfound As String, locationfound As String, partqtyinfound As String
    Dim partnotein As String, partspecialin As String, lastbin As String
    Dim columnpart As String, columnlocation As String, lineqtydetail As String
    Dim quickbin2 As String
    Dim ergebnis As String
    Dim j As Long
    Dim lenght_a As Long, lenght_b As Long

    Set ws = ThisWorkbook.Worksheets(Array("INPUT-1", "SINGLE-2", "SINGLE-3")) 'changed from Set ws = ThisWorkbook.Worksheets("INPUT-1")

    For Each ws In ThisWorkbook.Worksheets(Array("INPUT-1", "SINGLE-2", "SINGLE-3")) 'added this

    lastbin = "A"
    columnlocation = "B"
    quickbin2 = "C"
    columnpart = "L"
    partqtyinfound = "M"
    lineqtydetail = "N"
    partnotein = "O"
    partspecialin = "P"
    
    lenght_a = Len(limiter_a)
    lenght_b = Len(limiter_b)
    
    fullstring = SucheTeilenummer.userinput.Value
    
    openPos = InStr(fullstring, limiter_a)
    closePos = InStr(fullstring, limiter_b)
   
    If openPos > "0" And closePos > "0" Then
    
        searchstring = Mid(fullstring, openPos + lenght_a, closePos - openPos - lenght_b)
        lastRow = ws.Cells(ws.Rows.Count, columnpart).End(xlUp).Row
        
        For j = lastRow To 2 Step -1
        
            If Range(columnpart & j).Value = searchstring Then
                booFound = True
                
                SucheTeilenummer.partfound = ws.Range(columnpart & j).Value
                SucheTeilenummer.locationfound = ws.Range(columnlocation & j).Value
                SucheTeilenummer.partqtyinfound = ws.Range(partqtyinfound & j).Value
                SucheTeilenummer.partnotein = ws.Range(partnotein & j).Value
                SucheTeilenummer.partspecialin = ws.Range(partspecialin & j).Value
                SucheTeilenummer.lastbin = ws.Range(lastbin & j).Value
                SucheTeilenummer.lineqtydetail = ws.Range(lineqtydetail & j).Value & Chr(vbKeySpace) & "PCS" & " - " & ws.Range(columnpart & j).Value & vbCrLf & ws.Range(partnotein & j).Value & vbCrLf & "##################" & vbCrLf & SucheTeilenummer.lineqtydetail
                SucheTeilenummer.quickbin2 = ws.Range(quickbin2 & j).Value
                
            End If
            
        Next j
        
    Else
        searchstring = "Keine Limiter gefunden"
    End If
        
    If Not booFound Then
        SucheTeilenummer.partfound = searchstring
        emptySucheForm
    End If

    SucheTeilenummer.userinput.Value = ""
    fullstring = ""
    ergebnis = ""
    SucheTeilenummer.userinput.SetFocus
    
    Next 'tried closing here

    End Sub
Excel VBA 搜索 工作表

评论

0赞 Darren Bartrup-Cook 9/21/2023
确实应该在每个模块的顶部添加。Tools ~ Options ~ Require Variable Declaration 将自动执行此操作。也就是说 - 您应该添加和定义 - 它们都是空变体。它们意味着什么?Option ExplicitDim ws as Worksheetlimiter_alimiter_b
0赞 Darren Bartrup-Cook 9/21/2023
此外,不需要第一行。值在循环中传递给它。ws =For Each ws ...
1赞 CDP1802 9/21/2023
搜索是否只有 1 个结果?
0赞 Black cat 9/21/2023
如果搜索多个工作表,则每个引用都需要对象限定符。If ws.Range(columnpart & j).Value = searchstring Then
0赞 RadioEye 9/23/2023
@DarrenBartrup-Cook感谢您的帮助。Limiter_a 和 _b 被定义为在输入到搜索字段的字符串中要关注的某些位置,它写在另一个公共子中,我没有包含在这篇文章中。

答: 暂无答案