VBA Table producing too many rows

CalRich1023

New Member
Joined
Mar 15, 2021
Messages
48
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I have a search sheet to loop through my entire workbook and return results into a table-style format. for some reason it is producing 500 rows, but I only want it to produce enough rows for the data that is returned. I will paste the code below, and color the font of where the problem is occurring. The table data is from "A6:K6" and varying amount of rows (depending on search results. Table headers are in "A5:K5" I know I'm missing something small, but I just can't seem to find it. Thanks!

'FORMAT RESULTS AS A TABLE
'BUILD THE TABLE
Dim LastRow1 As Long
LastRow1 = Cells(Rows.Count, "A").End(xlUp).Row
ActiveWorkbook.Sheets("SEARCH").ListObjects.Add(xlSrcRange, Range("A5:K5" & LastRow1), , xlYes) _
.Name = "SEARCH_RESULTS_TABLE"

'COLOR THE TABLE GRAY/WHITE
ActiveWorkbook.Sheets("SEARCH").ListObjects("SEARCH_RESULTS_TABLE").TableStyle = "TableStyleMedium1"
'SHOWS TOTAL QUANTITY AT BOTTOM OF COLUMN "D"
ActiveWorkbook.Sheets("SEARCH").ListObjects("SEARCH_RESULTS_TABLE").ShowTotals = True
ActiveWorkbook.Sheets("SEARCH").ListObjects("SEARCH_RESULTS_TABLE").ListColumns(4) _
.TotalsCalculation = xlTotalsCalculationSum
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
What happens if you step through then hover over the LastRow1 variable what does it give you
I don't have Excel at the moment but change this line
Rich (BB code):
ActiveWorkbook.Sheets("SEARCH").ListObjects.Add(xlSrcRange, Range("A5:K5" & LastRow1), , xlYes) _
to
Rich (BB code):
ActiveWorkbook.Sheets("SEARCH").ListObjects.Add(xlSrcRange, Range("A5:K" & LastRow1), , xlYes) _
 
Upvote 0
Solution
What happens if you step through then hover over the LastRow1 variable what does it give you
I don't have Excel at the moment but change this line
Rich (BB code):
ActiveWorkbook.Sheets("SEARCH").ListObjects.Add(xlSrcRange, Range("A5:K5" & LastRow1), , xlYes) _
to
Rich (BB code):
ActiveWorkbook.Sheets("SEARCH").ListObjects.Add(xlSrcRange, Range("A5:K" & LastRow1), , xlYes) _
This small change made the difference. Thank you! I swear I tried this before posting here, but maybe I just imagined it HAHA.
Thanks again for your help!!
 
Upvote 0

Forum statistics

Threads
1,213,490
Messages
6,113,956
Members
448,535
Latest member
alrossman

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