gpt4 book ai didi

excel - 循环遍历所有工作表以查找包含特殊字符的单元格

转载 作者:行者123 更新时间:2023-12-03 02:17:33 25 4
gpt4 key购买 nike

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

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

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 声明为工作表,并循环遍历所有工作表,并为每个工作表选择相同的范围。

最佳答案

试试这个。您只需要另一个工作表循环和“查找”循环。

此代码不进行任何替换。

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

关于excel - 循环遍历所有工作表以查找包含特殊字符的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58079278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com