[VBA]: Delete empty rows from multiple tables and then print the range to pdf

IIII

New Member
Joined
Jan 26, 2021
Messages
18
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hi All,

I have this worksheet (see below) that has multiple formatted tables. Users are able to add or remove table rows as needed.

What I'm wishing for it to do via VBA is the following:
  1. Remove any empty rows from any of the tables
  2. Print the whole thing from A2 down to F...the row just after the last table row -- which in the example below is F50). Columns A & F are empty - both are used to create a blank border for print to pdf.
I haven't been able to work this one out or even know where to look.

If additional clarity is needed, please let me know. Thanks for any help or guidance in advance. Cheers!

Example of Shathousery.xlsx
ABCDEF
2
3Lorem ipsum dolor sit amet, consectetur adipiscing elit.
4 Total Cost for Thing: $ -
5
6ThingDatesDetailsCost
7Car (Staff)Fri, 21 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
8Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
9Car (Staff)Mon, 24 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.
10Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
11FuelLorem ipsum dolor sit amet, consectetur adipiscing elit.$ 400.00
12Sub-total cost for Transport$ 400.00
13
14ThingDatesDetailsCost
15HotelFri, 21 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
16Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
17HotelMon, 24 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
18$ -
19Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ 824.00
20Sub-total cost for Accomodation$ 824.00
21
22ThingDatesDetailsCost
23LessonFri, 21 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
24Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
25Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ 172.00
26TourSat, 22 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.
27Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
28Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ 480.00
29Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
30RideSun, 23 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
31Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ 350.00
32Sub-total cost for Activities$ 1,002.00
33
34ThingDatesDetailsCost
35Day 1Fri, 21 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ 100.00
36Day 2Sat, 22 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ 100.00
37Day 3Sun, 23 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ 100.00
38Day 4Mon, 24 Apr 23Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ 60.00
39Lorem ipsum dolor sit amet, consectetur adipiscing elit.$ 50.00
40
41
42
43
44
45
46Sub-total cost for Meals$ 410.00
47
48ThingDatesDetailsCost
49Staff NameTBALorem ipsum dolor sit amet, consectetur adipiscing elit.$ -
50
Plan
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I'll try to assist. Are there always the same number of tables? What are the tables' names? Are empty rows those without a date?
 
Upvote 0
I'll take the liberty of naming the first four tables based on the expense category: Transportation, Accommodations, Activities and Meals.
 
Upvote 0
Code below seems to do what you want. Note that I found out the hard way that you cannot name a table if the name is already in use in another worksheet. So, if you have more than one expense report worksheet you cannot use the same names for the tables. If you do copy a sheet with named tables Excel will add digits to the end of the table names.

That said, my code should still work as I added code to trim those digits off.

VBA Code:
Option Explicit

Function TrimDigitsFromString(psString As String) As String

    Dim iLen As Long
    
    Dim iCharNum As Long
    
    iLen = Len(psString)

    TrimDigitsFromString = "?"
    
    For iCharNum = 1 To iLen

        If Right(psString, 1) Like "#" _
         Then
            
            psString = Left(psString, Len(psString) - 1)

        Else
            TrimDigitsFromString = psString
            
            Exit Function

        End If
        
    Next iCharNum
    
End Function

VBA Code:
Option Explicit

Sub DeleteEmptyTableRows()

    Dim wsExpenses As Worksheet

    Dim tDataTable As ListObject

    Dim iTableRow As Long

    Dim iKeyColumn As Long

    Dim avDataArray() As Variant
    
    Dim sName As String

'   Worksheet where tables are located.
    Set wsExpenses = Worksheets("Plan") '<= change this if the Expenses worksheet name changes

'   Key column used to determine if a row is empty. 2 is Date.
    iKeyColumn = 2 '<= change this if the column used to determine if the row is "empty" changes

    For Each tDataTable In wsExpenses.ListObjects

        With tDataTable

            sName = TrimDigitsFromString(.Name)
            
'           Only process specific tables in the Expenses worksheet.
            If sName = "Transportation" Or _
               sName = "Accommodations" Or _
               sName = "Activities" Or _
               sName = "Meals" _
             Then

'               Put table contents into an array.
                avDataArray = .DataBodyRange

'               Loop "backwards" through each item in Date Column of Table
                For iTableRow = UBound(avDataArray) To LBound(avDataArray) Step -1

                    If avDataArray(iTableRow, iKeyColumn) = "" _
                     Then
                        .ListRows(iTableRow).Delete
                    End If

                Next iTableRow

            End If

        End With

    Next tDataTable

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,525
Members
452,651
Latest member
wordsearch

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