Vba 在Excel工作簿中搜索特定字符串

Vba 在Excel工作簿中搜索特定字符串,vba,excel,Vba,Excel,因此,我需要在VBA中创建一个Excel宏来搜索字符串,然后将其与我选择的预设字符串进行比较,然后更改另一个工作表中单元格的值 事情是这样的: Sub Macro1() Dim A As Integer Dim WS As Worksheet Dim ToCompare, Coniburo As String Coniburo = "My String" For Each WS In Worksheets For A = 1 To Rows.Count ToCompare

因此,我需要在VBA中创建一个Excel宏来搜索字符串,然后将其与我选择的预设字符串进行比较,然后更改另一个工作表中单元格的值

事情是这样的:

Sub Macro1()

Dim A As Integer
Dim WS As Worksheet

Dim ToCompare, Coniburo As String

Coniburo = "My String"

For Each WS In Worksheets
    For A = 1 To Rows.Count
    ToCompare = Left(Cells(A, 3), 100)
        If InStr(ToCompare, Coniburo) > 0 Then
            Sheets("Last Sheet").Cells(21, 2).Value = "233"
        End If
    Next A
Next
宏工作。。。。。。。如果我删除第一个(搜索表格的那个),只要我在一个有“我的字符串”的表格中。否则,它就不起作用了。由于共有17张纸,因此处理需要很长时间,超过一分钟

为什么不工作?我在这里读了很多帖子,微软开发论坛,一个叫做网络技术的网站,但我仍然缺少一些东西,但我不知道为什么


有人能给我指出正确的方向吗?

用。。。以结束,以聚焦循环每次迭代的父工作表

Option Explicit

Sub Macro1()
    Dim a As Long, Coniburo As String, ws As Worksheet

    Coniburo = "My String"

    For Each ws In Worksheets
        With ws
            For a = 1 To .Cells(.Rows.Count, "C").End(xlUp).Row
                If CBool(InStr(Left(.Cells(a, 3), 100), Coniburo, vbTextCompare)) Then
                    Worksheets("Last Sheet").Cells(21, 2).Value = 233
                End If
            Next a
        End With
    Next

End Sub
当在带有…的数组中时,需要在行、范围和单元格调用的前面加上句点,如
.Rows…
.Range(…)
.Cells(…)
。。。以方块结束。这将使用with..描述的父工作表来标识它们。。以…结束

我还使用
vbTextCompare
使比较不区分大小写


在同一张工作表的同一单元格中写入和重写233还有一个问题,但这是另一回事。

我在这里稍微改变了一些规则,但我想展示一下如何使用内置的FIND函数来显著加快速度。简单地说,我们将只处理C列中的每个工作表;我们将使用FIND函数查找C列包含搜索字符串的行号。。。。然后,根据您的要求,我们将仔细检查该单元格,查看您的搜索字符串是否在前100个字符内。如果是,我们会考虑这场比赛。除了在“最后一页”中记录“233”的结果外,我还添加了一些亮绿色高亮显示,以帮助查看发生了什么

Sub findConiburo()
    Coniburo = "My String"
    For Each ws In Worksheets
        With ws.Range("C:C")
            myName = ws.Name 'useful for debugging

            queue = 1 'will be used to queue the FIND function

            x = 0 'loop counter

            Do 'loop to find multiple results per sheet

                On Error Resume Next 'Disable error handling

                'FIND Coniburo within ws column C, log row number:
                'Note ".Cells(queue, 1)" is a relative reference to the current WS, column C
                foundRow = .Find(What:=Coniburo, After:=.Cells(queue, 1), LookIn:=xlFormulas, LookAt _
                    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                    False, SearchFormat:=False).Row

                'If no result found then an error number is stored. Perform error handling:
                If Err.Number <> 0 Then
                    'No results found, don't do anything, exit DO to skip to next sheet:
                    Exit Do
                End If
                On Error GoTo 0 'Re-enable error handling

                If x = 0 Then
                    'first loop - log the first row result:
                    originalFoundRow = foundRow
                ElseIf foundRow = originalFoundRow Then
                    'Not the first loop. Same result as original loop = we're back at the start, so exit loop:
                    Exit Do
                End If

                'Update queue so next loop will search AFTER the previous result:
                queue = foundRow

                'check if the string is not only SOMEWHERE in the cell,
                'but specifically within the first 100 characters:
                ToCompare = Left(.Cells(foundRow, 1), 100)
                If InStr(ToCompare, Coniburo) > 0 Then
                    .Cells(foundRow, 1).Interior.ColorIndex = 4 'highlight green
                    Sheets("Last Sheet").Cells(21, 2).Value = "233"
                End If

                'Update loop counter:
                x = x + 1
            Loop
        End With
    Next ws
End Sub
Sub-findConiburo()
Coniburo=“我的弦”
对于工作表中的每个ws
带ws.Range(“C:C”)
myName=ws.Name'对调试很有用
queue=1'将用于对FIND函数进行排队
x=0'循环计数器
Do'循环查找每张工作表的多个结果
错误恢复下一步“禁用错误处理”
'在ws列C中查找Coniburo,日志行号:
“Note”.Cells(queue,1)”是对当前WS,C列的相对引用
foundRow=.Find(What:=Coniburo,After:=.Cells(queue,1),LookIn:=xlFormulas,LookAt_
:=xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=_
False,SearchFormat:=False)。行
'如果未找到结果,则存储错误号。执行错误处理:
如果错误号为0,则
'未找到结果,不执行任何操作,退出“执行”以跳到下一张工作表:
退出Do
如果结束
错误转到0时,请重新启用错误处理
如果x=0,则
'第一个循环-记录第一行结果:
originalFoundRow=foundRow
ElseIf foundRow=originalFoundRow然后
“不是第一个循环。与原始循环相同的结果=我们回到起点,因此退出循环:
退出Do
如果结束
'更新队列,以便下一个循环在上一个结果后搜索:
queue=foundRow
'检查字符串是否不仅位于单元格中的某个位置,
'但特别是在前100个字符内:
ToCompare=左侧(.Cells(foundRow,1),100)
如果InStr(ToCompare,Coniburo)>0,则
.Cells(foundRow,1).Interior.ColorIndex=4'亮显绿色
表格(“最后一张”)。单元格(21,2)。Value=“233”
如果结束
'更新循环计数器:
x=x+1
环
以
下一个ws
端接头

这需要花费很长时间,因为您要在每一行中循环,全部100多万次,即超过1700万次循环。这需要一些时间,找到每张工作表上最后一行的数据并循环到该行。您每次都会将
“233”
写入并重写到最后一张工作表上的同一单元格中,对吗?您还有
行。Count
,这是不合格的。它只计算活动工作表上的行数。您需要将其限定为
Cells()
,稍后使用
WS
,即A=1到WS.Rows.Count,以及
…Left(WS.Cells(A,33),…
@BruceWayne在行和单元格中添加WS修复了它!我真不敢相信我错过了。非常感谢你,伙计!不要担心233,让我担心233。我也会尝试这种方法,你可以避免使用一个额外的变量,这对于将来的参考非常好。谢谢,伙计。