我正在使用下面的VBA首先找到特定文本,然后选择该单元格,然后查找并替换文本。在这两种情况下,我都希望允许工作表中可能不存在的文本,而不会导致错误。
查找和选择文本字符串以及查找和替换文本名称的最佳方法是什么?
Sub test2()
'
' test2 Macro
' Keyboard Shortcut: Ctrl+g
Cells.Find(what:="STRING", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range("G11").Select
Cells.Find(what:="STRING", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("I13").Select
Cells.Find(what:="STRING", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Columns("A:A").Select
ActiveCell.Replace what:="NAME", Replacement:="ALTERNATIVENAME", lookat:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Find(what:="NAME", After:=ActiveCell, LookIn:=xlFormulas, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.Replace what:="NAME", Replacement:="ALTERNATIVENAME", lookat:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
答案 0 :(得分:0)
要在整个电子表格中用“newtext”替换所有名为“example”的子字符串,我只需使用代码:
Sub test()
Dim LastRow As Long
Dim LastCol As Long
Dim rgn As Range
With ThisWorkbook.Sheets("Sheet1")
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
LastCol = .Cells.Find("*", SearchOrder:=xlByColumns, LookIn:=xlValues, SearchDirection:=xlPrevious).Column
Set rgn = .Range(.Cells(1, 1), .Cells(LastRow, LastCol))
End With
For Each cell In rgn
cell.FormulaR1C1 = Replace(cell.Value, "example", "newtext")
Next
End Sub
请注意,您必须将“Sheet1”替换为电子表格的名称。此外,如果电子表格为空,代码将生成运行时错误。
答案 1 :(得分:0)
如果您正在考虑在整个工作表中只替换一个文本字符串,则可以使用:
Sub ReplaceString()
ThisWorkbook.Worksheets("Sheet1").Cells.Replace _
What:="STRING", Replacement:="", LookAt:=xlPart, MatchCase:=False
End Sub
我认为您收到错误的原因是因为您正在尝试查找值,然后选择具有该值的单元格。如果没有找到则会导致错误 - 这是您在选择单元格之前需要检查的内容。
这使用变量来存储找到的范围。在选择范围之前,或者对范围做任何事情之前,都要进行检查以确保范围不是什么。
Sub Test()
Dim MyFoundRange As Range
Set MyFoundRange = ThisWorkbook.Worksheets("Sheet1").Cells.Find(what:="STRING", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not MyFoundRange Is Nothing Then
MyFoundRange.Select
End If
End Sub
答案 2 :(得分:0)
Sub Test2()
With ActiveSheet '<--| or, if you want to specify a given sheet, use 'With Worksheets("MyGivenSheetName")'
Intersect(.UsedRange, .Columns(1)).Replace What:="NAME", Replacement:="ALTERNATIVENAME", LookAt:=xlPart, MatchCase:=False '<--| act on used cells only to avoid processing all column A cells
End With
End Sub