简体   繁体   English

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

[英]Apply Excel VBA macro to all highlighted cells

Thank you in advance, I am very new to excel VBA. 在此先感谢您,我是excel VBA的新手。 This question may be very elementary, but I haven't found the answer in an extensive search. 这个问题可能非常简单,但是我没有在广泛的搜索中找到答案。 I originally recorded this macro and tweaked it with stuff I've found online. 我最初记录了此宏,并使用在网上找到的内容对其进行了调整。 This macro works if you apply to one cell at a time (or if you drag across multiple rows, will work on the row of the top-left-most cell). 如果您一次套用至一个储存格(或如果您拖曳多列,将在最左上角储存格的行上使用),这个巨集就会运作。 Is there a way I can further tweak it to get my macro to apply the changes to the rows of all selected cells so that the user can make changes to rows in bulk? 有没有一种方法可以进一步调整它,使我的宏将更改应用于所有选定单元格的行,以便用户可以批量更改行?

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 结束子

Maybe this is what you're after? 也许这就是你所追求的?

'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

Note: It is not necessary to .SELECT a range and then do something . 注意:不必先.SELECT一个范围,然后再执行something You can usually just apply something to the range. 通常,您可以将something应用于范围。 What you're starting with is typical for macro recorder code, just know there is a cleaner way. 您开始时通常会使用宏记录器代码,只是知道有一种更干净的方法。

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

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