【问题标题】:Apply Excel VBA macro to all highlighted cells将 Excel VBA 宏应用于所有突出显示的单元格
【发布时间】:2014-02-11 19:14:45
【问题描述】:

提前谢谢你,我对 excel VBA 很陌生。这个问题可能非常初级,但我没有在广泛的搜索中找到答案。我最初录制了这个宏,并用我在网上找到的东西对其进行了调整。如果您一次应用到一个单元格,则此宏有效(或者如果您拖动多行,则将在最左上角的单元格的行上工作)。有没有办法可以进一步调整它以让我的宏将更改应用于所有选定单元格的行,以便用户可以批量更改行?

Range("A" & ActiveCell.Row & ":I" & ActiveCell.Row).Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.249977111117893
    .PatternTintAndShade = 0
End With
Range("A" & ActiveCell.Row).Select
ActiveCell.FormulaR1C1 = "5"
Range("B" & ActiveCell.Row & ":I" & ActiveCell.Row).Select
With Selection.Font
    .Name = "Calibri"
    .FontStyle = "Regular"
    .Size = 11
    .Strikethrough = True
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With
Range("B" & ActiveCell.Row).Select

结束子

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    也许这就是你所追求的?

    'Instead of this:
    'Range("A" & ActiveCell.Row & ":I" & ActiveCell.Row).Select
    'Do this:
    With Application.Intersect(Selection.EntireRow, Range("A:I")).Interior
    'The range at hand is now all the cells in the rows of the selection, 
    '  but limited to columns A:I.
    'Notice we haven't actually modified the selection
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.249977111117893
        .PatternTintAndShade = 0
    End With
    'Range("A" & ActiveCell.Row).FormulaR1C1 = "5"
    Application.Intersect(Selection.EntireRow, Range("A:A")).FormulaR1C1 = "5"
    'Range("B" & ActiveCell.Row & ":I" & ActiveCell.Row).Select
    With Application.Intersect(Selection.EntireRow, Range("B:I")).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = True
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    Range("B" & ActiveCell.Row).Select
    

    注意:不必先.SELECT 一个范围然后再做something。您通常可以将something 应用于该范围。您开始使用的是典型的宏记录器代码,只要知道有一种更简洁的方法即可。

    【讨论】:

    • 是的,这正是我想要的。感谢您的帮助!有没有办法让宏让用户在运行宏之前选择相同的单元格?就像他们 Ctrl+单击多个单元格然后将此格式化宏应用于单元格一样,之后会选择相同的单元格吗?谢谢!
    • 当然——只需注释掉最后一行,让它看起来像'Range("B" & ActiveCell.Row).Select
    猜你喜欢
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多