VBA无法识别列出对象的标头

时间:2019-05-02 12:45:41

标签: excel vba

我无法理解excel为什么停止识别列出的对象的标头;特别是这一行Range("Offer_Table[[#Headers], [LOC]").Select 我想更改特定标头的格式,并且我首先可以使用以下代码来做到这一点。

Range("Offer_Table[[#Headers], [LOC]").Select
    With Selection.Font
        .Name = "Calibri"
        .FontStyle = "Bold"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = -0.249977111
        .ThemeFont = xlThemeFontMinor
    End With
    With Selection.Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent4
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
    End With

但是,一旦我关闭所有活动工作表以对新工作表进行首次完整测试,代码便停止工作。我不明白为什么,但是Excel现在不再识别标头。是从VBA创建新的LO还是手动设置新表。

下面是示例代码

'Referring to the Table
    Dim Offer_table As ListObject
    CntCol = Range(Cells(1, 1), Cells(1, 1).End(xlToRight)).Count
    CntRow = Range(Cells(1, 2), Cells(1, 2).End(xlDown)).Count


MsgBox CntCol
MsgBox CntRow
    Set Offer_table = ActiveSheet.ListObjects.Add(xlSrcRange, Range(Cells(1, 1), Cells(CntRow, CntCol)), , xlYes)
    Offer_table.Name = "Offer_Table"
    Offer_table.TableStyle = "TableStyleLight2"


'Highlight headers
        Range("Offer_Table[[#Headers], [Order Creation]").Select
    With Selection.Font
        .Name = "Calibri"
        .FontStyle = "Bold"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = -0.249977111
        .ThemeFont = xlThemeFontMinor
    End With
    With Selection.Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent4
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
    End With

         Range("Offer_Table[[#Headers],[SEGMENT]").Select
    With Selection.Font
        .Name = "Calibri"
        .FontStyle = "Bold"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = -0.249977111
        .ThemeFont = xlThemeFontMinor
    End With
    With Selection.Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent4
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
    End With

1 个答案:

答案 0 :(得分:0)

我认为您的错误是由于您未明确引用表所在位置而引起的。您还可以将其声明为ListObject,然后在以后引用它时不要使用它。看一下以下内容。请确保将With Activesheet更新为带有表格的工作表的显式引用,否则一旦移至另一工作表,您将再次遇到问题

'Referring to the Table
Dim Offer_table As ListObject
Dim CntCol As Long, CntRow As Long

' Update with reference to your sheet
With ActiveSheet
    CntCol = Range(.Cells(1, 1), .Cells(1, 1).End(xlToRight)).Count
    CntRow = Range(.Cells(1, 2), .Cells(1, 2).End(xlDown)).Count

    MsgBox CntCol & vbNewLine & CntRow

    Set Offer_table = ActiveSheet.ListObjects.Add(xlSrcRange, Range(Cells(1, 1), Cells(CntRow, CntCol)), , xlYes)
    Offer_table.Name = "Offer_Table"
    Offer_table.TableStyle = "TableStyleLight2"
End With

'Highlight headers
' Range("Offer_Table[[#Headers], [Order Creation]").Select
With Offer_table
    With .HeaderRowRange(.ListColumns("Order Creation").Range.Column)
        With .Font
            .Name = "Calibri"
            .FontStyle = "Bold"
            .Size = 11
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorAccent2
            .TintAndShade = -0.249977111
            .ThemeFont = xlThemeFontMinor
        End With
        With .Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent4
            .TintAndShade = 0.799981688894314
            .PatternTintAndShade = 0
        End With
    End With

    ' Range("Offer_Table[[#Headers],[SEGMENT]").Select
    With Offer_table.HeaderRowRange(.ListColumns("SEGMENT").Range.Column)
        With .Font
            .Name = "Calibri"
            .FontStyle = "Bold"
            .Size = 11
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorAccent2
            .TintAndShade = -0.249977111
            .ThemeFont = xlThemeFontMinor
        End With
        With .Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent4
            .TintAndShade = 0.799981688894314
            .PatternTintAndShade = 0
        End With
    End With
End With