简体   繁体   中英

How to remove fill in excel table (restore default background color ) - VBA

I have a simple table and want to remove fill (restore default background color) using VBA.

This is my code:

Function cleanTable(tableName As String)
    Dim i As Long
    Dim table As ListObject: Set table = ThisWorkbook.Worksheets("Sheet1").ListObjects(tableName)

    For i = 1 To table.DataBodyRange.Rows.Count
        table.ListRows(i).Range.Interior.Pattern = 0
    Next i
End Function

But I am receiving the error:

Application defined or object defined error

And have no idea how to solve it.

Will be really thankful for your support

Using almost your code, starting with this:

在此处输入图片说明

I get this:

在此处输入图片说明

Without getting any error. How are you calling the function exactly? This is what I have used:

Sub TestMe()
    cleanTable "myTable"
End Sub

Function cleanTable(tableName As String)
    Dim i As Long
    Dim table As ListObject
    Set table = ThisWorkbook.Worksheets(1).ListObjects(tableName)
    For i = 1 To table.DataBodyRange.Rows.Count
        table.ListRows(i).Range.Interior.Pattern = 0
    Next i
End Function

You can reset to the default background with:

Range("A1").Interior.ColorIndex = xlColorIndexNone

...if that's what you're asking

tablename.Shading.BackgroundPatternColor = wdColorWhite

为我工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM