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

时间:2014-01-21 23:05:11

标签: excel vba excel-vba

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

End Sub

1 个答案:

答案 0 :(得分: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应用于该范围即可。您开始使用的是宏录制器代码的典型代表,只要知道有更简洁的方法。