VBA Excel循环通过特定表格格式化

时间:2016-03-25 02:54:46

标签: excel vba excel-vba

我在工作表上有77个表,我只想格式化特定的表。

当执行具有特定命名表的循环时,如何格式化循环?

例如,我想更改Table2,Table4,Table5和Table6中的第二列(此示例的缩写列表)。

到目前为止,我有:

For  Table2, Table4, Table 5 In ws.ListObjects

On Error Resume Next

ws.ListObjects("Table").ListColumns(2).DataBodyRange.Style = "Comma"
ws.ListObjects("Table").ListColumns(2).DataBodyRange.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"

Next

On Error GoTo 0

关于如何解决这个问题的任何建议?

1 个答案:

答案 0 :(得分:1)

Sub test()
Dim AllTableNames As String
Dim TableNames As Variant
Dim ws As Worksheet
Dim i As Long

Set ws = ActiveSheet
AllTableNames = "Table2,Table4,Table5,Table6"
TableNames = Split(AllTableNames, ",")

For i = LBound(TableNames) To UBound(TableNames)
    ws.ListObjects(TableNames(i)).ListColumns(2).DataBodyRange.Style = "Comma"
    ws.ListObjects(TableNames(i)).ListColumns(2).DataBodyRange.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
Next i
End Sub