如何从录制的宏中清除并删除不必要的步骤?

时间:2014-06-06 14:06:10

标签: excel vba formatting

我是VBA的新手,我正在整理下面记录的宏并删除默认情况下的任何细节,并且不需要在那里或删除任何细节不是必要的,只会使代码混乱。任何人都可以提出一些建议吗?

     With ActiveWorkbook.Styles("Normal")
    .IncludeNumber = True
    .IncludeFont = True
    .IncludeAlignment = True
    .IncludeBorder = True
    .IncludePatterns = True
    .IncludeProtection = True
End With
ActiveWorkbook.Styles("Normal").NumberFormat = "#,##0.0;[Red](#,##0.0);-"
With ActiveWorkbook.Styles("Heading 1")
    .IncludeNumber = False
    .IncludeFont = True
    .IncludeAlignment = False
    .IncludeBorder = True
    .IncludePatterns = True
    .IncludeProtection = False
End With
ActiveWorkbook.Styles("Heading 1").Borders(xlLeft).LineStyle = xlNone
ActiveWorkbook.Styles("Heading 1").Borders(xlRight).LineStyle = xlNone
ActiveWorkbook.Styles("Heading 1").Borders(xlTop).LineStyle = xlNone
With ActiveWorkbook.Styles("Heading 1").Borders(xlBottom)
    .LineStyle = xlContinuous
    .ThemeColor = 4
    .TintAndShade = 0
    .Weight = xlThick
End With
ActiveWorkbook.Styles("Heading 1").Borders(xlDiagonalDown).LineStyle = xlNone
ActiveWorkbook.Styles("Heading 1").Borders(xlDiagonalUp).LineStyle = xlNone
With ActiveWorkbook.Styles("Heading 1").Interior
    .Pattern = xlSolid
    .PatternColorIndex = 0
    .ThemeColor = xlThemeColorLight2
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
With ActiveWorkbook.Styles("Heading 1")
    .IncludeNumber = False
    .IncludeFont = True
    .IncludeAlignment = False
    .IncludeBorder = True
    .IncludePatterns = False
    .IncludeProtection = False
End With
With ActiveWorkbook.Styles("Heading 2")
    .IncludeNumber = False
    .IncludeFont = True
    .IncludeAlignment = False
    .IncludeBorder = True
    .IncludePatterns = False
    .IncludeProtection = False
End With
ActiveWorkbook.Styles("Heading 2").Borders(xlLeft).LineStyle = xlNone
ActiveWorkbook.Styles("Heading 2").Borders(xlRight).LineStyle = xlNone
ActiveWorkbook.Styles("Heading 2").Borders(xlTop).LineStyle = xlNone
With ActiveWorkbook.Styles("Heading 2").Borders(xlBottom)
    .LineStyle = xlContinuous
    .ThemeColor = 4
    .TintAndShade = 0
    .Weight = xlThick
End With
ActiveWorkbook.Styles("Heading 2").Borders(xlDiagonalDown).LineStyle = xlNone
ActiveWorkbook.Styles("Heading 2").Borders(xlDiagonalUp).LineStyle = xlNone
With ActiveWorkbook.Styles("Heading 4")
    .IncludeNumber = False
    .IncludeFont = True
    .IncludeAlignment = False
    .IncludeBorder = False
    .IncludePatterns = False
    .IncludeProtection = False
End With
With ActiveWorkbook.Styles("Heading 4").Font
    .Name = "Calibri"
    .Size = 11
    .Bold = True
    .Italic = False
    .Underline = xlUnderlineStyleNone
    .Strikethrough = False
    .ThemeColor = 2
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
End With
  With ActiveWorkbook.Styles("Total")
    .IncludeNumber = False
    .IncludeFont = True
    .IncludeAlignment = False
    .IncludeBorder = True
    .IncludePatterns = False
    .IncludeProtection = False
End With
ActiveWorkbook.Styles("Total").Borders(xlLeft).LineStyle = xlNone
ActiveWorkbook.Styles("Total").Borders(xlRight).LineStyle = xlNone
With ActiveWorkbook.Styles("Total").Borders(xlTop)
    .LineStyle = xlContinuous
    .ThemeColor = 2
    .TintAndShade = 0
    .Weight = xlThin
End With
With ActiveWorkbook.Styles("Total").Borders(xlBottom)
    .LineStyle = xlContinuous
    .ThemeColor = 2
    .TintAndShade = 0
    .Weight = xlThin
End With
ActiveWorkbook.Styles("Total").Borders(xlDiagonalDown).LineStyle = xlNone
ActiveWorkbook.Styles("Total").Borders(xlDiagonalUp).LineStyle = xlNone
ActiveWindow.SmallScroll Down:=21
ActiveWorkbook.Styles("Percent").NumberFormat = "0.00%;[Red](0.00%);-"
End Sub

1 个答案:

答案 0 :(得分:1)

在此特定示例中,可以对与ActiveWorkbook.Styles("Heading 1")相关的所有行进行分组,并将整个代码括在With..End With构造中。同意正常风格:

With ActiveWorkbook.Styles("Normal")
    .IncludeNumber = True
    .IncludeFont = True
    .IncludeAlignment = True
    .IncludeBorder = True
    .IncludePatterns = True
    .IncludeProtection = True
    .NumberFormat = "#,##0.0;[Red](#,##0.0);-"
End With   
With ActiveWorkbook.Styles("Heading 1")
    .IncludeNumber = False
    .IncludeFont = True
    .IncludeAlignment = False
    .IncludeBorder = True
    .IncludePatterns = True
    .IncludeProtection = False
    .Borders(xlLeft).LineStyle = xlNone
    .Borders(xlRight).LineStyle = xlNone
    .Borders(xlTop).LineStyle = xlNone
    .Borders(xlBottom)
    .LineStyle = xlContinuous
    .ThemeColor = 4
    .TintAndShade = 0
    .Weight = xlThick
End With

通常,您还需要删除所有Activate。 Intead of

Range("g2").Activate
ActiveCell.Value = 6

你会写:Range("g2").Value = 6