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

时间:2016-02-10 04:47:48

标签: excel vba excel-vba

我正试图找出一个问题,因为这个想法是为了缩短"使用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

1 个答案:

答案 0 :(得分:0)

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