简体   繁体   English

从定义范围转换Excel宏,以选择的细胞仅操作上?

[英]Convert excel macro from defined range, to operate on selected cells only?

I have a macro that does what I want but it runs on a set range of cells. 我有一个宏可以执行我想要的操作,但是它可以在一定范围的单元格上运行。 I have been trying to make it run only on cells that are selected (highlighted) by the user. 我一直试图使其仅在用户选择(突出显示)的单元格上运行。 I've tried various combinations of defining the range using Dim Rng as Range and also Selection method. 我尝试了使用Dim Rng作为Range和Selection方法定义范围的各种组合。 Me:No VBA experience to speak of, some python experience. 我:没有VBA经验可言,有些python经验。

Working code (defined range) 工作代码(定义范围)

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

This is my attempt to make it run on user selected cells that gives me object required error for the r.Select line: 这是我尝试使其在用户选择的单元格上运行,这使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

You're nearly there 你快到了

Sub NoHalve_selection()
    Dim r As Range, Rng As Range
    Set Rng = Selection

    For Each r In Rng.Cells ' .Cells is implied in For Each r in Rng
        With r  'Using With block is more efficient as it does fewer lookups to Excel
            If Left$(.Value, 1) = "<" Then  ' .Value uses the With block (so is the same as r.Value).  Value is the default property of a Range 
                .Value = Mid$(.Value, 2)
                .Font.ColorIndex = 16
                .Font.Underline = xlUnderlineStyleSingle
            End If
        End With
   Next
End Sub

Original post for comparison 原始帖子以供比较

Sub NoHalve_selection()
    Dim r As Range, Rng As Range
    Set Rng = Selection

    For Each r In Rng
        If Left(r, 1) = "<" Then
            r = (Right(r, Len(r) - 1))
            r.Font.ColorIndex = 16
            r.Font.Underline = xlUnderlineStyleSingle
        End If
   Next
End Sub

This should be relatively easy by replacing the hard coded numbers with Selection.Columns.Count and Selection.Rows.Count . 通过用Selection.Columns.CountSelection.Rows.Count替换硬编码数字,这应该相对容易。

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 Selection.Columns.Count
        For y = 2 To Selection.Rows.Count
            If Left(Cells(y, x), 1) = "<" Then
            Cells(y, x) = (Right(Cells(y, x), Len(Cells(y, x)) - 1))
            Cells(y, x).Font.ColorIndex = 16
            Cells(y, x).Font.Underline = xlUnderlineStyleSingle
            End If
        Next y
    Next x
End Sub

If you are making a simple text substitution (removing a sign), as long as that sign is left most then I'd also suggest maybe using something that doesn't rely so heavily on the position of the characters in a string. 如果您要进行简单的文本替换(删除符号),只要该符号最左边,那么我还建议您使用一些不太依赖字符串中字符位置的东西。 So something like replace : 所以像replace

Cells(y, x) = replace(Cells(y, x),"<","",,1)

Also I don't believe the line Cells(y, x).Select is required and may change the active selection unnecessarily. 我也不相信Cells(y, x).Select是必需的,并且可能会不必要地更改活动选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM