Question on Scope

Brad24

Board Regular
Joined
May 4, 2015
Messages
81
Hi, I have two questions about scope.

1. Can two modules have the same subroutine names other than the first one called from the user form? Ie, I have two user forms with a button on each. I want one button to call the subroutine on one module and the second button to call a subroutine on the second module. The code is very similar for both. Other than those very first subroutines that are called, can the rest of them be named the same on both modules? Once in the module, does it have scope?

Incase I didn't explain it properly:
module1 has:
GetData()
ProcessData()

module2 has:
GetOtherData()
ProcessData()

Will that work or mess everything up?

2. In multiple areas in my code, I have the constant MyWorkbookName. I have to change this multiple places at the beginning of every year. Is there a place where I can put that where all the code will reference it, so I only have to change it in the one place?
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
1 You can have subs/functions with same name in different modules, you'll need to be careful when calling them though. For example to call ProcessData in Module1 you would need to use this.
Code:
Call Module1.ProcessData ' Call optional

2 How/where are you using MyWorkbookName?

You could declare it publicly in a standard module, then it would be available throughout the code but you would need to be careful where/when you set it's value

PS Does MyWorkbookName refer to the workbook the code is in?
 
Upvote 0
Hi, thanks for the reply. So once I call GetData() with a button on a user form, and then inside this GetData subroutine, if I want to call the other subroutines in this module, I just preface the subroutine with the Module name? If I didn't do that..it wouldn't know where to go? Ie, it might see that there is a similarly named subroutine in module2 and would fail? Do I understand that correctly? :)

Yes, the MyWorkbookName is the actual name of the workbook the code is in.
 
Upvote 0
depending on what you want to change in the sub, you can select all data and use find and replace from the edit menu
 
Upvote 0
Yes, the MyWorkbookName is the actual name of the workbook the code is in.

Then you can just use ThisWorkbook.Name rather than a constant that you have to keep altering.
 
Upvote 0
Oh, I just clued in to what you meant there. I understand that now. I had some code written by another member and he probably wrote that so it would run in my file. I will fix that up.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

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