Help needed to resolve Object variable or With block variable not set

Sanjay 1 Reputation point
2020-05-27T12:05:41.223+00:00

I am writing a VB script to clear all table data from all the sheets in excel workbook. Each sheet has multiple table .
Below is the code written but getting Object variable or With block variable not runtime error .

Sub cleardata()

Dim tbl As ListObject
Dim sht As Worksheet

For Each sht In ThisWorkbook.Worksheets

If sht.Name <> "Contents" And sht.Name <> "Cover Page" Then
For Each tbl In sht.ListObjects

  tbl.DataBodyRange.ClearContents

  Next tbl

End If

Next sht

End Sub

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,960 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dave Patrick 426.1K Reputation points MVP
    2020-05-27T12:07:04.397+00:00

    Scripting is not supported here on QnA. I'd try asking for help in dedicated forums here.

    https://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?forum=ITCG

    --please don't forget to Accept as answer if the reply is helpful--


    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    0 comments No comments