Make Table using .UsedRange in VB instead of using cntl-T

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi there,
I have a worksheet whose data change periodically leading to larger or smaller ranges of columns being used depending on the data. The number of rows are always the same.

In my VBA module that gets the data, I would like to make a table (instead of cntl-T afterwards) with the data each time I grab it and always have the same table name.

I've tried a few things including this below which will only make a table of the A column.

I tried using ActiveSheet.UsedRange.Select assigned to a Range variable, but that didn't work.

Since my data table changes size I can't use a pre-defined range in the column direction so I'm not sure how to make it expand or shrink to "wrap" columns for making it into a table. I tried counting columns, converting the max to a letter and then putting it in, but Is there a way to do this without having to count columns and pass that to the second part of the range below?

Thank you!

VBA Code:
Worksheets("BaseData").Activate
  
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:A" & 1259), , xlYes).Name = _
        "BaseDataTable"

    Range("Table1[#All]").Select
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi GeeWhiz7,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim lngLastCol As Long
    
    Application.ScreenUpdating = False
    
    With Worksheets("BaseData")
        lngLastCol = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        .ListObjects.Add(xlSrcRange, Range(.Cells(1, 1), Cells(1259, lngLastCol)), , xlYes).Name = "BaseDataTable"
    End With
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Upvote 0
Solution
another option to try
VBA Code:
With Worksheets("BaseData")
    .ListObjects.Add(xlSrcRange, .Range("A1").CurrentRegion, , xlYes).Name = "BaseDataTable"
End With
 
Upvote 0
Hi GeeWhiz7,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim lngLastCol As Long
   
    Application.ScreenUpdating = False
   
    With Worksheets("BaseData")
        lngLastCol = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        .ListObjects.Add(xlSrcRange, Range(.Cells(1, 1), Cells(1259, lngLastCol)), , xlYes).Name = "BaseDataTable"
    End With
   
    Application.ScreenUpdating = True
   
End Sub

Regards,

Robert
Hi Robert,

Thanks for the solution suggestion. I gave it a try and get this error and the highlight in VBA shows on the line starting with .ListObjects...
Thanks, -Will
1638827870860.png


VBA Code:
    With Worksheets("BaseData")
        lngLastCol = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        .ListObjects.Add(xlSrcRange, Range(.Cells(1, 1), Cells(1259, lngLastCol)), , xlYes).Name = "BaseDataTable"
    End With
 
Upvote 0
Hi Robert,

Thanks for the solution suggestion. I gave it a try and get this error and the highlight in VBA shows on the line starting with .ListObjects...
Thanks, -Will
View attachment 52804

VBA Code:
    With Worksheets("BaseData")
        lngLastCol = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        .ListObjects.Add(xlSrcRange, Range(.Cells(1, 1), Cells(1259, lngLastCol)), , xlYes).Name = "BaseDataTable"
    End With
Thanks Robert and NoSparks! It works now. The issue was the second "Cell" was missing a "." in front of it. Changing that made it work.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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