Expand/Collapse Table using VBA

XL_NOOB_89

New Member
Joined
Feb 8, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
VBA INLINE AT BOTTOM
Issue: I have a chart connected to a table (Table1). Sometimes, not every line in Column1 of this table has data in it, and this is driving the table. These blank lines in Column1 are currently displayed in the legend of the chart as blank lines. Column2 of the table are values (formulas) associated with the info in Column1. So column1 is text, and Column2 are numbers.

Solution to the problem: I need the VBA to expand/collapse the table, so there are no blanks. Also sort the values from largest to smallest in Column2 of the table.

Scope: This VBA attached WORKS PROPERLY, except for one issue. The UsedRows command is counting the blank cells as used cells. I can force this VBA to work 100% by doing the following:
  1. Put a breakpoint on the line where UsedRows = Application.CountA(myColumn.DataBodyRange)
  2. Press play to run the VBA
  3. When the VBA hits the break point, highlight the blank lines in Column1 of the table and press “Delete” on my keyboard
  4. Press the play button and continue to run the VBA and it will work as I need it
Notes for Code: Table1 is defined as range AI9:AJ13. There is a formula in cells AH9:AH13, which only displays text if multiple criteria are met. If the criteria are not met, then the formula returns a “” value

Any help here would be appreciated. VBA below

Sub Test()
'Clear Table contents
Worksheets("Test Document").ListObjects("Table1").ListColumns(1).DataBodyRange.Clear


Dim myColumn As ListColumn
Set myColumn = Sheets("Test Document").ListObjects("Table1").ListColumns("Column1")

For Each rng In Range("AI9:AI13")
rng.Value = rng.Offset(0, -1)
Next rng


'Find used rows
Dim UsedRows As Long
UsedRows = Application.CountA(myColumn.DataBodyRange)


'Find total rows
Dim oSheetName As Worksheet
Dim sTableName As String
Dim loTable As ListObject
Dim loRows As Integer
Dim iRows As Integer

sTableName = "Table1"
Set oSheetName = Sheets("Test Document")
Set loTable = oSheetName.ListObjects(sTableName)

'Resize table to only used rows
loTable.Resize loTable.Range.Resize(UsedRows + 1)

MsgBox "Done"

End Sub
 

Attachments

  • Picture1.png
    Picture1.png
    14.8 KB · Views: 23

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
VBA Code:
For Each Rng In Range("AI9:AI13")
          If Rng.Offset(0, -1).Value <> "" Then Rng.Value = Rng.Offset(0, -1)
     Next Rng
 
Upvote 0
Solution
worked beautifully - I really liked the simplicity of it. Thank you BSALV
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,970
Members
448,933
Latest member
Bluedbw

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