【发布时间】:2019-08-27 01:08:46
【问题描述】:
我有一个宏可以做我想做的事,但它在一组单元格上运行。我一直试图让它只在用户选择(突出显示)的单元格上运行。我尝试了使用 Dim Rng 作为 Range 以及 Selection 方法来定义范围的各种组合。我:没有VBA经验可言,一些python经验。
工作代码(定义范围)
Sub NoHalve()
'
' Macro to remove less-than sign and report only the LOR formatted grey and underlined .
' x = columns, y = rows
For x = 1 To 200
For y = 2 To 3000
If Left(Cells(y, x), 1) = "<" Then
Cells(y, x) = (Right(Cells(y, x), Len(Cells(y, x)) - 1))
Cells(y, x).Select
Selection.Font.ColorIndex = 16
Selection.Font.Underline = xlUnderlineStyleSingle
End If
Next y
Next x
End Sub
这是我尝试让它在用户选择的单元格上运行,这给了我 r.Select 行的对象所需的错误:
Sub NoHalve_selection()
Set Rng = Selection
For Each r In Rng
If Left(r, 1) = "<" Then
r = (Right(r, Len(r) - 1))
r.Select
Selection.Font.ColorIndex = 16
Selection.Font.Underline = xlUnderlineStyleSingle
End If
Next
End Sub
【问题讨论】:
-
这可能是
Application.InputBox选择范围的候选对象。 -
不需要遍历选择中的每个单元格。您可以使用
.Find和.FindNext来循环仅看起来像“ -
也可以把
Cells(y, x) = (Right(Cells(y, x), Len(Cells(y, x)) - 1))写成Cells(y, x) = Mid(Right(Cells(y, x),2)