Link to home
Start Free TrialLog in
Avatar of Daniele Questiaux
Daniele QuestiauxFlag for Australia

asked on

Making a Table with a unique name referring to the active worksheet

Good Morning,
Unlike named range, tables are unique to a range in a workbook and cannot be referred with the same name in each worksheet.
If I choose a range for a table and assign it a name, I cannot via name manager change the scope from workbook to every worksheet, or use the old trick of =!X1:X12 where the ! means that range is for each worksheet.
This means that if I have identically structured worksheet, I need to have tables made for each individual sheets. This is feasible with the number of sheets I have only about 12, but I was wondering whether there is another way of proceeding for other workbook where I would like to do the same?
The table in this case will be filled through VBA by data that is obtained from one calculated range from data copied from another source. The rows of the table are built up from that data but not identically for each worksheet as some results give null results in some experiments or real values in others. That is to say I would run the VBA code for each sheet independently so that all is done only in the active worksheet.
I wanted to keep one unique name in the VBA code as I organise a spreadsheet, then copy it for the number of experiments being run. Right now, the table built in the first sheet say Table1 remains for that sheet whilst the other sheets, a new table name is created. At least a table is created ! so how would I then make sure that with my code, the code refers to that (new) table in an active worksheet?
Here is my code
Sub Recup()
Dim sht As Worksheet
Dim tbl As ListObject
Dim table_object_row As ListRow

Set sht = Sheets(4)
Set tbl = sht.ListObjects("Table1")
last_row_with_data = sht.Range("Y65536").End(xlUp).Row
If last_row_with_data = sht.Range("Y13") Then
Set table_object_row = tbl.ListRows
Else: Set table_object_row = tbl.ListRows.Add
End If

sht.Range("Y" & last_row_with_data).Value = Range("AJ10")
sht.Range("Z" & last_row_with_data).Value = Range("AK10")
sht.Range("AA" & last_row_with_data).Value = Range("AL10")
sht.Range("AB" & last_row_with_data).Value = Range("AM10")
sht.Range("AC" & last_row_with_data).Value = Range("AN10")
sht.Range("AD" & last_row_with_data).Value = Range("AO10")
sht.Range("AE" & last_row_with_data).Value = Range("AP10")
sht.Range("AF" & last_row_with_data).Value = Range("AQ10")
sht.Range("AG" & last_row_with_data).Value = Range("AR10")

End Sub 

Open in new window


The table in the first sheet is called Table 1 so that the next one might be called Table2 etc.
The tab numbers have names referring to the individual experiment either by date or by individuals so that it is actually difficult to see which one is the next sheet unless the dates follow quite regularly which they do not.
How could I make a table in each sheet that is recognisable by one single macro (above)?
What line of code can make sure I choose the right region in the active worksheet?
There are NO other tables in that workbook except those duplicated tables. There might be an extra table added in an extra sheet added afterwards to compile all the results, but this table would only apply to that worksheet.

Named ranges seem more flexible than tables and probably would be the best choice here?
 I wrote the code from snippets of codes  and I would not know how to proceed to change the code for a table to code for a named range.


Thank you helping me out Table properties, and/or VBA workaround to the limitation of Tables, if indeed Tables can only refer to one unique specific range in a specific worksheet.
I have a long way to go with coding, so any help there would be a huge help in this case.

Cheers,
Danièle
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Daniele Questiaux

ASKER

A thorough reply!

Exactly as requested, a code line to refer to the last table created in the worksheet without having to know its index number together with a line of code to ensure that any worksheet where the code should not apply to are definitely left out.
A thorough answer.

Thanks!
Danièle
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015