VBA - 使用 With 命令时出现预期标识符或括号中的表达式错误



我正在尝试找出类的一个问题,其想法是使用 With 命令"缩短"代码。以下是我到目前为止的代码。我很好奇为什么你不能在 with 命令下使用 shtThisSheet 变量,当 .

谢谢大家!

Option Explicit
Sub Formatting()
Dim shtThisSheet As Worksheet
Set shtThisSheet = ActiveWorkbook.Worksheets("Sheet1").Range

With shtThisSheet
    .Font.Bold = True
    .Font.Size = 14
    .HorizontalAlignment = xlLeft
    . ("A3:A6").Font.Bold = True
    .("A3:A6").Font.Italic = True
    .("A3:A6").Font.ColorIndex = 5
    .Range("A3:A6").InsertIndent 1
    .Range("B2:D2").Font.Bold = True
    .Range("B2:D2").Font.Italic = True
    .Range("B2:D2").Font.ColorIndex = 5
    .Range("B2:D2").HorizontalAlignment = xlRight
    .Range("B3:D6").Font.ColorIndex = 3
    .Range("B3:D6").NumberFormat = "$#,##0"
End With
End Sub
Option Explicit
Sub Formatting()
Dim shtThisSheet As Worksheet
Set shtThisSheet = ActiveWorkbook.Worksheets("Sheet1")
With shtThisSheet
    With .Range("A3:A6")
        .Font.Bold = True
        .Font.Size = 14
        .HorizontalAlignment = xlLeft
        .Font.Bold = True
        .Font.Italic = True
        .Font.ColorIndex = 5
        .Range("A3:A6").InsertIndent 1
    End With
    With .Range("B2:D2")
        .Font.Bold = True
        .Font.Italic = True
        .Font.ColorIndex = 5
        .HorizontalAlignment = xlRight
    End With
    With .Range("B3:D6")
        .Font.ColorIndex = 3
        .NumberFormat = "$#,##0"
    End With
End With
End Sub

最新更新