【问题标题】:Loop through all sheets to find cells which contain special characters遍历所有工作表以查找包含特殊字符的单元格
【发布时间】:2019-09-24 11:28:17
【问题描述】:

我有这个宏来替换我工作簿中任何工作表中的特殊字符。

它去掉了这些字符:! @#$​​%^&()/

Sub Macro3()

Dim splChars As String
Dim ch As Variant
Dim splCharArray() As String

splChars = "! @ # $ % ^ & () /" splCharArray = Split(splChars, " ")

For Each ch In splCharArray
    Cells.Replace What:="~" & ch, Replacement:="", LookAt:=xlPart, SearchOrder:= _
      xlByRows, MatchCase:=True
Next ch

End Sub

我需要第二个宏,它会为每个工作表中的每个单元格执行Cells.Find,然后创建一个新工作表来列出所有单元格地址和找到的特殊字符。

我在网上找到的:

Public Sub SearchForText()
    Dim rngSearchRange As Range
    Dim vntTextToFind As Variant
    Dim strFirstAddr As String
    Dim lngMatches As Long
    Dim rngFound As Range
  
    On Error GoTo ErrHandler
    vntTextToFind = Application.InputBox( _
      Prompt:="Enter text to find:", _
      Default:="Search...", _
      Type:=2 _
      )
    If VarType(vntTextToFind) = vbBoolean Then Exit Sub
  
    On Error Resume Next
    Set rngSearchRange = Application.InputBox( _
      Prompt:="Enter range for search:", _
      Default:=ActiveCell.Parent.UsedRange.Address, _
      Type:=8 _
      )

    On Error GoTo ErrHandler
    If rngSearchRange Is Nothing Then Exit Sub
    Set rngFound = rngSearchRange.Find( _
      What:=CStr(vntTextToFind), _
      LookIn:=xlValues, _
      LookAt:=xlPart _
      )
  
    If rngFound Is Nothing Then
        MsgBox "No matches were found.", vbInformation
    Else
        With ThisWorkbook.Sheets.Add
            With .Range("A1:B1")
                .Value = Array("Cell", "Value")
                .Font.Bold = True
            End With
            strFirstAddr = rngFound.Address
            Do
                lngMatches = lngMatches + 1
                .Cells(lngMatches + 1, "A").Value = rngFound.Parent.Name & "!" _
                                          & rngFound.Address(0, 0)
                .Cells(lngMatches + 1, "B").Value = rngFound.Value
                Set rngFound = rngSearchRange.FindNext(rngFound)
            Loop Until (rngFound.Address = strFirstAddr)
            .Columns("A:B").AutoFit
        End With
    End If
    Exit Sub
  
ErrHandler:
    MsgBox Err.Description, vbExclamation
End Sub

此代码有效。我的问题是,我需要设置每次搜索的范围,并且只能是一张纸,所以基本上如果我有 10 张纸,我需要运行这个宏 10 次才能获得所需的结果。

我想在我的工作簿的每个工作表中搜索每个字符,然后创建一个新工作表并返回整个工作簿中包含我声明的任何字符的每个单元格的地址。

我想我可以将新变量 ws 声明为工作表,并循环使用为每个工作表选择相同范围的所有工作表。

【问题讨论】:

    标签: excel vba search


    【解决方案1】:

    试试这个。您只需要另一个循环用于工作表,以及一个循环用于查找。

    此代码不做任何替换。

    Sub Macro3()
    
    Dim splChars As String
    Dim ch As Variant
    Dim splCharArray() As String
    Dim r As Range, s As String
    Dim ws As Worksheet
    
    splChars = "! @ # $ % ^ & () /"
    splCharArray = Split(splChars, " ")
    
    Sheets.Add().Name = "Errors" 'to list characters and location
    
    For Each ch In splCharArray
        For Each ws In Worksheets
            If ws.Name <> "Errors" Then
                Set r = ws.Cells.Find(What:=ch, Lookat:=xlPart, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False)
                If Not r Is Nothing Then
                    s = r.Address
                    Do
                        Sheets("Errors").Range("A" & Rows.Count).End(xlUp)(2) = ch 'character
                        Sheets("Errors").Range("B" & Rows.Count).End(xlUp)(2) = r.Address(external:=True)
                        Set r = ws.Cells.FindNext(r)
                    Loop Until r.Address = s 'loop until we are back to the first found cell
                End If
            End If
        Next ws
    Next ch
    
    End Sub
    

    【讨论】:

    • 这非常好用,我刚刚为错误表添加了标题行 :) 非常感谢先生
    猜你喜欢
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多