How to pass two separate, but "connected", ranges to a macro called by a button control?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,537
Office Version
  1. 365
Platform
  1. Windows
I am working on some macro code that will process a range of data. It will actually need access to two separate, but "connected" ranges. I would love to make them both tables so I can take advantage of the table features, but I don't see how to make that work.

Here's a simple version of my data:

Weighted Ratings.xlsx
BCDEFG
2TypeLoHiHiHi
3Max$30.00
4Min4.2200
5Weight1112
6
7ItemPriceRtg#RevCapacityComments
8D$22.994.459525 lb
9I$49.504.582630 lb
10A$14.174.610,90440 lb
11G$19.954.615940 lb
12C$54.984.750450 lb
13E$23.954.72,09545 lb
14F$7.994.75350 lb
15H$31.974.747640 lb
16B$13.124.810,58355 lb
Simple


The data in rows 2-5 must align with the columns in the table. The problem is how to keep them aligned if I need to add, delete, or relocate and columns.

When I have just one table, I always add rows by right-clicking on the column header and selecting Insert. That preserves the size of the other columns. If I do it by right-clicking inside the table, any columns to the right get messed up. This is what happens it I right-click in D7 and select Insert Column to the Left.

Weighted Ratings.xlsx
BCDEFGH
7ItemPriceColumn1Rtg#RevCapacityComments
8D$22.994.459525 lb
9I$49.504.582630 lb
10A$14.174.610,90440 lb
11G$19.954.615940 lb
12C$54.984.750450 lb
13E$23.954.72,09545 lb
14F$7.994.75350 lb
15H$31.974.747640 lb
16B$13.124.810,58355 lb
Simple


But if I make B2:G5 into a table, Excel will no longer allow me to insert from the column header. I can only do it from inside the tables and then I have to make sure I do it exactly the same in both tables to keep them aligned.

I guess my current plan is to leave the top rows as a range (not a table) so I can insert and delete from the column headers. That should keep everything aligned.

Are there any better ways?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hello, not sure if this is still an issue for you.. but I was wondering something

When I have just one table, I always add rows by right-clicking on the column header and selecting Insert. That preserves the size of the other columns. If I do it by right-clicking inside the table, any columns to the right get messed up. This is what happens it I right-click in D7 and select Insert Column to the Left.
Have you tried naming your columns in vba and working with those ranges?

For example...
VBA Code:
tabl1 = ' add first table
tabl2 = ' add second table
For i = lbound(tabl1,2) to ubound(tabl1,2)
     If tabl1(1,i) = "Heading" then priceHeading = i  'priceHeading being the header for the desired column in tabl2
Next i

For i = lbound(tabl2) to ubound(tabl2)
    tabl2(i,priceHeading) = ' Whatever you need done
Next i



'and after its all done, just write the new data over the old tables... that way you can add columns to your tables and future proof it

Roughly like that... apologies if I have misunderstood your question
 
Upvote 0
I see that I didn't explain the problem very well. Let me try again.

I am working on a weighted rating macro. It will analyze a table of products I am considering buying. The table will contain values for each product on a number of properties, such as price, weight, capacity, etc. The problem I am having is that the macro needs some additional data about each property that does not fit in the table. I have come up with two possible solutions.

Solution #1 - Data aligned with table columns

Put the additional data just above the property columns. There are a couple of problems with this solution.
  1. How to keep the data outside the table aligned with the corresponding table columns if I add or delete table columns. If I insert a column for the sheet, things stay aligned. If I insert a column just for the table, it does not. My solution to that is to try to remember to always insert or delete table columns for the entire sheet. But in case I forget, I will replicate the table headings above the outside data. That way the macro can check that the data is properly aligned.
  2. How to pass the data to the macro. If the macro has the name of the table, can it find the corresponding external data using the cell addresses of the table?
This minisheet illustrates this approach. The table has 9 products and 4 properties for each product: Price, Rtg (ratings from some source, like Amazon), MPG (miles.gallon if this is a vehicle), and dB (noise level in decibels if this is a product where that matters).

The outside data is in the 3 rows above the table. The Order row tells the macro whether, for that property, high values are best (HiLo) or low ones are (LoHi). The Weight row tells the macro how much (relative) weight to assign to each property. The Alignment Check row simply copies the table headings so the macro can check that they are properly aligned.

Weighted Ratings.xlsx
BCDEFGH
3OrderLoHiHiLoHiLoLoHi
4Weight1.001.501.001.25
5Alignment CheckPriceRtgMPGdB
6ProdCommentsWtd RtgPriceRtgMPGdB
7Prod 1All above average$18.004.63755
8Prod 2All averages$20.004.53550
9Prod 3All below average$22.004.43345
10Prod 4High, high, high, low$15.004.84040
11Prod 5High, low, high, low$15.004.84040
12Prod 6Low, high, low, high$25.004.23060
13Prod 7Low, low, low, high$25.004.23060
14Prod 8High to Low$15.004.63340
15Prod 9Low to high$25.004.43760
MrExcel1


Solution #2 - Data in second table aligned with main table columns

Put the additional data in a second table keeping the corresponding columns aligned. There are a couple of problems with this solution.
  1. How to keep the columns of the two tables aligned. With two tables, Excel won't let me add a column to the sheet, so I'll have to make any additions or deletions to each table separately. I have replicated the headings, so this should not be major problem, but if there is a better way, I'd like to know about it.
  2. How to pass two table names to the macro. If I define the table names in a constant in the macro, then I cannot have more than one table in any workbook, because Excel, in violation of modern naming conventions, chose to make table names global with no way to make them local.
Weighted Ratings.xlsx
BCDEFGH
3FieldPriceRtgMPGdB
4OrderLoHiHiLoHiLoLoHi
5Weight1.001.501.001.25
6
7ProdCommentsWtd RtgPriceRtgMPGdB
8Prod 1All above average$18.004.63755
9Prod 2All averages$20.004.53550
10Prod 3All below average$22.004.43345
11Prod 4High, high, high, low$15.004.84040
12Prod 5High, low, high, low$15.004.84040
13Prod 6Low, high, low, high$25.004.23060
14Prod 7Low, low, low, high$25.004.23060
15Prod 8High to Low$15.004.63340
16Prod 9Low to high$25.004.43760
MrExcel2


Is that any clearer?

I would appreciate any comments or suggestions for how to do this efficiently and effectively.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,728
Messages
6,126,523
Members
449,316
Latest member
sravya

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