Looping through tables in vba, ignore empty tables?

Lil2606

Board Regular
Joined
Jan 29, 2019
Messages
79
Hi all,

So I have this following code (piece):

File-Copy-icon.png

Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">
Dim i As Long
Dim LOC As Long

If Not Sheet4.ListObjects("BLANKTable").DataBodyRange Is Nothing Then

LOC = Sheet4.ListObjects.Count

ElseIf Sheet4.ListObjects("BLANKTable").DataBodyRange Is Nothing Then

LOC = Sheet4.ListObjects.Count - 1

End If

For i = 1 To LOC

Sheet4.ListObjects(i).DataBodyRange.ClearFormats
Sheet4.ListObjects(i).DataBodyRange.ClearFormats
Sheet4.ListObjects(i).DataBodyRange.ClearFormats
Sheet4.ListObjects(i).DataBodyRange.ClearFormats

Sheet4.ListObjects(i).ListColumns("One of the columns").DataBodyRange.Interior.Color = RGB(207, 207, 237)
Sheet4.ListObjects(i).ListColumns("Another one").DataBodyRange.Interior.Color = RGB(245, 212, 231)
Sheet4.ListObjects(i).ListColumns("A column").DataBodyRange.Interior.Color = RGB(220, 209, 233)
Sheet4.ListObjects(i).ListColumns("A different column").DataBodyRange.Interior.Color = RGB(220, 209, 233)

Next i</code>

Which was working perfectly until I had 5 tables and the last one was the "BLANKTable" but I had to add a new table, to which the interior colour change should apply.. but if the BLANKTable is empty then the code throws an error. (and I realized other tables may end up empty as well before this step, so this code piece is really not right)

I can see why.. but I don't know how to loop through all the tables except for the ones that's databody range is empty.. which is what I would need here I think. Could someone help with this?

Question posted on Excelforum as well. Link:
https://www.excelforum.com/excel-pr...-in-vba-igrnore-empty-tables.html#post5120216
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Re: Looping through tables in vba, igrnore empty tables?

yeah.. I had the solution in the question....

Code:
For i = 1 To Sheet4.ListObjects.Count

If Not Sheet4.ListObjects(i).DataBodyRange Is Nothing Then


Sheet4.ListObjects(i).DataBodyRange.ClearFormats
Sheet4.ListObjects(i).DataBodyRange.ClearFormats
Sheet4.ListObjects(i).DataBodyRange.ClearFormats
Sheet4.ListObjects(i).DataBodyRange.ClearFormats


<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sheet4.ListObjects(i).ListColumns("One of the columns").DataBodyRange.Interior.Color = RGB(207, 207, 237)
Sheet4.ListObjects(i).ListColumns("Another one").DataBodyRange.Interior.Color = RGB(245, 212, 231)
Sheet4.ListObjects(i).ListColumns("A column").DataBodyRange.Interior.Color = RGB(220, 209, 233)
Sheet4.ListObjects(i).ListColumns("A different column").DataBodyRange.Interior.Color = RGB(220, 209, 233)</code>


End If


Next i

just ignore me please!
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,770
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top