【问题标题】:Highlight range of cells and press button to run macro only for that range突出显示单元格范围并按下按钮仅针对该范围运行宏
【发布时间】:2017-10-16 05:36:47
【问题描述】:

我正在尝试选择并突出显示 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

【问题讨论】:

    标签: excel replace vba


    【解决方案1】:

    如果您希望您的宏使用用户选择的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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      相关资源
      最近更新 更多