突出显示单元格区域,然后按下按钮以仅为该范围运行宏
问题描述:
我试图选择并突出显示列A中的单元格区域,然后按按钮以运行该宏以查找并替换所有“NL”到“N”。突出显示单元格区域,然后按下按钮以仅为该范围运行宏
到目前为止,这只适用于如果我选择一个单元格而不是整个选定范围。
Sub ReplaceAll_NL()
ActiveCell.Select
Selection.Replace What:="NL", Replacement:="N", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
答
如果您希望您的宏使用Selection
用户已经选择了,不要在宏内改变Selection
是不同的东西:
Sub ReplaceAll_NL()
'ActiveCell.Select '<-- This line was changing the selection to be something
' different to what the user had selected.
Selection.Replace What:="NL", Replacement:="N", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
End Sub