Best way to pass a named range to a macro?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,531
Office Version
  1. 365
Platform
  1. Windows
I am working on a macro that will analyze data in a table and some data outside the table or possibly in a second table. What's the best way to pass those ranges to the macro?

I prefer to call the macro using a button form control. I've done that several times. But as far as I know, there is no way to pass any parameters (arguments) that way.

Thanks for any help.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You can pass arguments using a macro assigned to a button, but they should be data values, not objects. (It also won't work if your workbook is saved in xlsb format).

If it is always the same table(s), then I'd just refer to them directly in the code.
 
Upvote 0
You can pass arguments using a macro assigned to a button, but they should be data values, not objects. (It also won't work if your workbook is saved in xlsb format).
At the moment, it's saved as xlsx. I've never used an xlsb format.

If it is always the same table(s), then I'd just refer to them directly in the code.
If I do that, then I cannot have another table in a different sheet in the same workbook, because Excel chose to make table named global with no option to make them local. I believe this is in violation of modern structured programming conventions. (sigh)
 
Upvote 0
I'm getting an error. I suspected it might be because the macro is in an add-in module. Is that a no-no?

To test it, I saved the workbook as xlsm, added a code module, and moved the macro code to that module. Now the macro code works.

But I want it in the add-in so it can be called from more than 1 workbook. I did a little checking and found a webpage that said I needed to include the full path to the add-in module.

Here's the macro code:
VBA Code:
Sub WtdRtg(pTblParms As String, pTblData As String)

MsgBox "Hi"

End Sub

The add-in is in this folder: C:\Users\username\AppData\Roaming\Microsoft\AddIns

So I moved the macro code back to the add-in module and changed the macro assign string in the Form Button control to this:

Code:
['C:\Users\username\AppData\Roaming\Microsoft\AddIns\My Add-Ins.xlam'!'WtdRtg "TblParms", "TblData"'

But when I click on the button, I get this error message:

1668672424986.png


I then checked the macro assign string and it had gone back to this: 'Weighted Ratings.xlsm'!'WtdRtg "TblParms", "TblData"', which is what it was when the macro was in the workbook module. I tested this a couple of times. Every time I pasted the longer text with the add-in path and clicked OK, and then reopened it, it had gone back.

Can you tell me what I am doing wrong now?
 
Upvote 0
I'm getting an error. I suspected it might be because the macro is in an add-in module. Is that a no-no?
I no longer say something cannot be done as there is always someone out there that will tell you otherwise

I have never used a forms button to call code in that manner & cannot immediately give you any informed guidance - maybe another can.
I am on granddad (school run) duties today but If find a moment I will have a play and if have any success, will come back.

Dave
 
Upvote 0
Using:

Code:
'C:\Users\username\AppData\Roaming\Microsoft\AddIns\My Add-Ins.xlam'!'WtdRtg("TblParms", "TblData")'

should work.
 
Upvote 0
Solution
Using:

Code:
'C:\Users\username\AppData\Roaming\Microsoft\AddIns\My Add-Ins.xlam'!'WtdRtg("TblParms", "TblData")'

should work.
I wondered about the lack of parens. But that's what the webpage Dave recommended said to do (no parens).

1668676861679.png


I just tried your way and got this error message:

1668676706998.png
 
Upvote 0
In which module in the addin is the WtdRtg macro?
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,039
Latest member
Mbone Mathonsi

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