Excel清除表格样式:如何清除Excel中表格的“应用和清除”格式

将 Table 插入 Excel 后,我想更改 Table Style。但是,它不会更改样式。如何在 VBA 中使用 'Apply and Clear Formatting' 功能?

For Each ws In ActiveWorkbook.Worksheets
    With ws
        If .Index <> 1 Then
'Insert Table with the Data starting in Column A, Row 3
    Dim myTable As ListObject
    Set myTable = .ListObjects.Add(xlSrcRange, .Range("A3", .Range("A3").End(xlToRight).End(xlDown)), , xlYes)
    With myTable
        .Name = .Name & "_Table"
        .TableStyle = "TableStyleMedium12"
        .Range.Font.Bold = True
        .Range.Font.Size = 16
    End With
1

在 Excel 中,单元格由其坐标定义,如 A3。相应的 VBA 语法是Cells(3, "A")或更好的是Cells(3, 1)

在 Excel 中,范围由其第一个和最后一个单元格定义,如 A3:D12。在 VBA 中,类似于Range(Cells(3, "A"), Cells(12, "D"))或-更熟练-Range(Cells(3, 1), Cells(12, 4))。更喜欢数字来定义列,因为它们可以计算。Excel 可以找到Columns(3 + 4),但找不到Columns("C" + 4)

Cells(Rows.Count, "A")定义一个等于Cells(1048576, 1)的单元格。它是列 A 中最后一个可能的单元格。同样,Cells(3, Columns.Count)定义了第 3 行最右边的单元格。Columns.Count= 16384,但在使用早期版本的 Excel 创建的工作表中可能是一个较小的数字。

上述表达式与.End(xlUp)End.(xlToLeft)的组合只是描述了与已经定义的单元格的偏移量,以查找第一个占用的单元格(End),upTo [the] Left。因此,这些表达式定义了一个正确的单元格。如果它们在语法中显示为正确的范围,则

在下面的代码中,我已经尽力带你通过小步骤为你的表定义范围的过程。它有效,这很好。但关键是它应该为你工作,只有当你完全理解它时,它才会这样做。我希望上面的解释有助于实现这一目标。

Sub InsertTable()
    ' 026
    Dim Ws      As Worksheet
    Dim Tbl     As ListObject
    Dim Rng     As Range                ' range in which to set the table
    Dim Rl      As Long                 ' last row
    Dim Cl      As Long                 ' last column
    For Each Ws In ActiveWorkbook.Worksheets
        With Ws
            If .Index > 1 Then
                ' find the last used row in column A
                Rl = .Cells(.Rows.Count, "A").End(xlUp).Row
                ' find the last used column in row 3
                Cl = .Cells(3, .Columns.Count).End(xlToLeft).Column
                ' set the range for the table
                Set Rng = .Range(.Cells(3, "A"), .Cells(Rl, Cl))
                ' convert the range to a table
                Set Tbl = .ListObjects.Add(xlSrcRange, Rng, , xlYes)
            End If
        End With
        With Tbl
            .Name = .Name & "_Table"
            .TableStyle = "TableStyleMedium12"
            .Range.Font.Bold = True
            .Range.Font.Size = 16
        End With
    Next Ws
End Sub

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(578)
上古卷轴5特技点代码:Webpack 5入口点
上一篇
备考cpa需要多长时间:TLD可能需要多长时间 (longest url in the world)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(61条)