Unknown number of tables

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
I am working on a macro that will calculate overall product ratings based on my ratings of various properties of the products that are available. I am in the market for an electric car, so I thought it would be a good test.

Most of the EV properties are numerical, such as price, miles/kWh, range, horsepower, headroom, and legroom. For numerical properties, all I need is to know whether higher numbers are better (range, headroom, star ratings), or lower numbers are (price, availability, rankings).

Several properties are not numerical, like body style (sedan, SUV, ...) and drive (4WD, AWD, RWD, FWD). And some numerical properties, like number of doors, are not linear. For properties that are not numerical and linear, I create "helper" tables that convert the options to linear (and proportional) numbers. Here are a few examples:

Weighted Ratings.xlsm
BCDEFGHIJKLM
4Body TypeRtgCommentsDriveRtgHeaterRtgDoorsRtg
5Sedan104 doors, traditional trunk4WD5Heat Pump355
6SUV7AWD4Resistive16+4
7MPV7Multi-purpose vehicleRWD342
8Hatch7HatchbackFWD120
EV Tables


The macro will use these tables to convert non-numerical and non-linear values to linear numerical values that can be aggregated with the other properties.

The problem is how to handle these in the macro. The names of the tables are not fixed so I can't declare them. I need some sort of dynamically variable way of accessing the tables. It is my understanding that VBA supports arrays of arrays (also called "jagged" arrays). Can I declare a jagged array then load these tables into it? I have never done this before, so before I try it, I thought I'd ask if this is the way to go or if there is a better way. I would appreciate any hints or tips on how to do it.

Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try:

VBA Code:
Sub Loop_Tables()
  Dim sh As Worksheet
  Dim tbl As ListObject
  
  Set sh = Sheets("Ev Tables")
  
  For Each tbl In sh.ListObjects
    
    'Here you can do what you need using the tbl object, for example:
    tbl.DataBodyRange.Select
  
  Next
  
End Sub
 
Upvote 0
Try:

VBA Code:
Sub Loop_Tables()
  Dim sh As Worksheet
  Dim tbl As ListObject
 
  Set sh = Sheets("Ev Tables")
 
  For Each tbl In sh.ListObjects
   
    'Here you can do what you need using the tbl object, for example:
    tbl.DataBodyRange.Select
 
  Next
 
End Sub
There are a couple of details that I am not sure of.
  • "Ev Tables" is the name of the sheet containing the helper tables, right?
  • Is "DataBodyRange" the name of one of the tables or is that a keyword?
  • What exactly does the "tbl.DataBodyRange.select" statement do?
  • How do I load one of the tables into a VBA array?
  • Does this code assume that the Ev Tables sheet contains only helper tables and no other objects?
  • What code would I need if I wanted to load just one table, say, "TblHeater" or TblDoors"?
Thanks for this help. It looks like it will do what I need if I can only get my head around it. 🤔🤨😣🤪
 
Upvote 0
I will try to answer everything:
"Ev Tables" is the name of the sheet containing the helper tables, right?
Right
-------------------------------

Is "DataBodyRange" the name of one of the tables or is that a keyword?
DatabodyRange is a part of a table
1674518183786.png

-------------------------------
What exactly does the "tbl.DataBodyRange.select" statement do?
Select body of table
VBA Code:
Sub Loop_Tables()
  Dim sh As Worksheet
  Dim tbl As ListObject
  Dim tbname As String
 
  Set sh = Sheets("Ev Tables")
 
  For Each tbl In sh.ListObjects
  
    'select Body
    tbl.DataBodyRange.Select
  
    'get table name
    tbname = tbl.Name
  
  Next
End Sub
-------------------------------
How do I load one of the tables into a VBA array?
For example:
VBA Code:
Sub test2()
  Dim sh As Worksheet
  Dim tbl As ListObject
  Dim arrtbls As New Collection
  Dim itm As Variant
 
  Set sh = Sheets("Ev Tables")
 
  For Each tbl In sh.ListObjects
    arrtbls.Add tbl.Name
  Next
 
  For Each itm In arrtbls
    MsgBox itm
  Next

End Sub

-------------------------------
Does this code assume that the Ev Tables sheet contains only helper tables and no other objects?
Only listobjects (tables)

-------------------------------
What code would I need if I wanted to load just one table, say, "TblHeater" or TblDoors"?
VBA Code:
Sub test1()
  Dim sh As Worksheet
  Dim tbl As ListObject
 
  Set sh = Sheets("Ev Tables")
 
  Set tbl = sh.ListObjects("TblHeater")
 
  'select all table
  tbl.Range.Select
End Sub
-------------------------------
😅

----- --
 
Last edited:
Upvote 0
Just a thought. The key values in all your tables look to be Unique across all tables, the only one that is worrying is the Doors table.
Also all the tables are Col1 Lookup value, Col2 rating.
Is it worth considering loading them all into a dictionary with key=col1 and value=col2 ?
Presumably you are planning to do a lookup to get the rating.

Without a more code & making it more complex than it needs to be, I don't believe there is an efficient way to load all the tables in a single read statement. Looping through the table per Dante's suggestion would be the way to go.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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