首页 文章

将Excel VBA宏应用于所有突出显示的单元格

提问于
浏览
1

提前谢谢你,我很擅长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

结束子

1 回答

  • 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 应用于该范围 . 您开始使用的是典型的宏录制器代码,只知道有一种更清洁的方式 .

相关问题