提问人:RadioEye 提问时间:9/21/2023 最后编辑:JohnMRadioEye 更新时间:9/21/2023 访问量:46
在 Excel 工作簿中的所有工作表中展开当前 VBA 搜索代码
Expand current VBA search code across all sheets in excel workbook
问:
我已经为我的问题找到了很多答案,但不幸的是,由于我缺乏经验,我无法将其他解决方案重新应用于我的代码。
我试图完成的是使用我当前的工作代码,我在其中输入一个我想在工作表中搜索的字符串,并将其扩展到其他工作表上。
顺便说一句,我不确定“仅将我当前的代码扩展”到其他工作表是否足够(我猜不是),而不添加某种循环以确保我不会在第一次成功搜索后卡在最后一个工作表上?
无论哪种方式,我尝试过的方法都没有奏效。但它也没有引起任何错误或变化。该代码仍然像我尝试将其扩展到多个工作表之前一样工作。
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
答: 暂无答案
评论
Option Explicit
Dim ws as Worksheet
limiter_a
limiter_b
ws =
For Each ws ...
If ws.Range(columnpart & j).Value = searchstring Then