Basic question on modules

mark hansen

Well-known Member
Joined
Mar 6, 2006
Messages
534
Office Version
  1. 2016
Platform
  1. Windows
I have a basic question regarding modules. At a concept level, what are they? Are they inherently different code from the code I put under a button?

I cut my teeth with computers programming dBase II and we had programs call other programs when we needed the code to be used more than once. It saved lots of repetitive programming.

Is this what modules are for?

I have done lots of research on code that can e-mail and all of it, is in modules. I do most of my stuff in buttons to kick off the code. In one of my spreadsheets, I have the same long code put in two spots, under two buttons - for ease of use. I didn't want my users to scroll over to get to the single reset button. Could I have put the same code in a module and reference that code by the name of the module on two buttons?

I guess the basic question is code in a module inherently different from code used by a button?

Sorry for such a basic question concept question.

Mark
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Could I have put the same code in a module and reference that code by the name of the module on two buttons?

Yes.
Most simply:

In Module1:
Code:
Sub Foo()
'blah blah blah
End Sub
In Button1:
Code:
Sub Button1_OnClick()
    Call Foo
End Sub

In Button2:
Code:
Sub Button2_OnClick()
    Call Foo
End Sub

I personally don't put a lot of code in buttons on worksheets as there's odd issues with scope that arise from time to time (not a problem but something to be aware of). Generally, I just call module code from button code.
 
Last edited:
Upvote 0
Could I have put the same code in a module and reference that code by the name of the module on two buttons?

Yes.
Most simply:

In Module1:
Code:
Sub Foo()
'blah blah blah
End Sub
In Button1:
Code:
Sub Button1_OnClick()
    Call Foo
End Sub

In Button2:
Code:
Sub Button2_OnClick()
    Call Foo
End Sub

I personally don't put a lot of code in buttons on worksheets as there's odd issues with scope that arise from time to time (not a problem but something to be aware of). Code is code. But the container of the code is what differs. Generally, I just call module code from button code, unless its very simple button code or its quite specific to that button and that button alone, rather than code that works with worksheets or other Excel objects. I try not to put code in specific containers (Worksheets, ThisWorkbook, Buttons, Userforms) unless it really relates directly to that container and that container only.
 
Last edited:
Upvote 0
Thanks xenou,
That clears it up quite nicely. With the help of this board, I have been able to help out my users with some pretty impressive "magic". I always thought there may be a better way and I'm reminded what one of my first mentors told me "You never know what you don't know ---so, eventhough you got it, keep looking for a better way".

I've beed reading the website that hiker95 mentioned, and others like it. I can see I have much more foundation work to do on my VBA skills.

Thanks again,
Mark
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,337
Members
452,907
Latest member
Roland Deschain

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