Select table using dynamic range

khall56

New Member
Joined
Nov 17, 2017
Messages
7
Hi!

I 'wrote' a macro using key strokes. I created a table which is done via the code below:

ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$M$75"), , xlYes).Name = _
"Table1"


My issue is I don't want the Range to always be the Range as defined here.
I will be using worksheets that will ALWAYS start at A1, but I have no idea how long or wide they will be.

I need something to allow the range to be variable based on where the data are.

I've seen a lot of code with:

lcol = ws.Cells(Rowno, 1).End(xlToRight).Column
lrow = ws.Cells(Rows.Count, Colno).End(xlUp).Row
Start = Cells(Rowno, Colno).Address

wb.Names.Add Name:="lcol", _
RefersTo:="=COUNTA($" & Rowno & ":$" & Rowno & ")"
wb.Names.Add Name:="lrow", _
RefersToR1C1:="=COUNTA(C" & Colno & ")"
wb.Names.Add Name:="myData", RefersTo:= _
"=" & Start & ":INDEX($1:$65536," & "lrow," & "Lcol)"

But can't seem to make that work. I'm relatively new to VBA coding, so I'm having some difficulty debugging.
Any help (along with what is happening in the code) is greatly appreciated!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
To use a range from A1 to M last row

lr = Cells(Rows.Count, 1).End(xlUp).Row 'find last row


'your line becomes
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$M"&lr), , xlYes).Name = _
"Table1"
[/CODE]
 
Upvote 0
Thanks Scott-
That is closer to what I need, but it creates a table with only one row (which it inserts above my data).
I have a feeling, I'm missing something in my execution.
More info:
I'm testing on a worksheet with data in A1 through J75
I first add three columns on the left giving me a range I want to select as A1: M75.
Here's what I have to get to this point with the added last row finder you provided.

Columns("A:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "state_code"
Range("B1").Select
ActiveCell.FormulaR1C1 = "jobtitle"
Range("C1").Select
ActiveCell.FormulaR1C1 = "sched_num"
lr = Cells(Cells.Rows.Count, 100).End(xlUp).Row 'find last row
ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1:M" & lr), , xlYes).Name = "Table1"


As I said, at this point I'm left with a one row 13 column table (all the correct headings- so that's something)
None of my data are selected as the table.

QUOTE=Scott T;4953021]To use a range from A1 to M last row

lr = Cells(Rows.Count, 1).End(xlUp).Row 'find last row


'your line becomes
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$M"&lr), , xlYes).Name = _
"Table1"
[/CODE][/QUOTE]
 
Upvote 0
Sounds like column A have data in it except the header.

This looks in column A (column 1), change to a column that will always have data. Column B is 2, C is 3...
Code:
lr = Cells(Rows.Count, [COLOR=#ff0000]1[/COLOR]).End(xlUp).Row 'find last row


You do not need to select the cells first this will do the same thing
Code:
Columns("A:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1") = "state_code"
Range("B1") = "jobtitle"
Range("C1") = "sched_num"


 lr = Cells(Rows.Count, 1).End(xlUp).Row 'find last row ' change to 1 to column that will always have data


 'your line becomes
 ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$M"&lr), , xlYes).Name = "Table1"
 
Upvote 0
THANK YOU!!

I feel sort of stupid with the column numbering- but then I'm still learning!
This worked- it will save many people a LOT of time so again THANK YOU, THANK YOU, THANK YOU
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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