Why Modules?

rahulbassi

Board Regular
Joined
Aug 14, 2011
Messages
130
Hi Everyone,
I've created a tool with the help of Excel and VBA.
It is working fine. I've done the coding the Sheet and forms section. But I've not used any Module.
Definitely, me code is a mess....but working fine...
Please explain me with an example why and where do we use Modules....??
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

rahulbassi

Board Regular
Joined
Aug 14, 2011
Messages
130
Thanks for guiding....
Gone through the link too....
still need some more clarification....
Suppose I want a piece of code to run one the click of a command button. Then should i write the code in module or in the form window which opens when I click the button twice in DESIGN MODE..?
If I need to write it in module say Module1.
Then how do I link that code to click event?
 

rahulbassi

Board Regular
Joined
Aug 14, 2011
Messages
130
Actually I think I've been writing the code in Userform Modules......But i want to know about the usage of Regular modules...
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650

ADVERTISEMENT

The code for a command button should go in its userform code module.

A regular module would be use for code like this

Code:
Sub test()
Dim i As Long
For i = 1000 To 2 Step -1
    If Range("A" & i).Value = Range("A" & i - 1).Value Then Rows(i).Delete
Next i
End Sub
 

Mindpsyche

Well-known Member
Joined
Mar 19, 2012
Messages
760
All userform codes go in the userform module, as in the one that open when you click the command button. All other macros are stored in your regular modules.

Lets say you have a userform. Now the code associated with this userform will be entered in the userform module. But when you want to call the userform through a macro, that macro will be in your regular module.

The best thing would be to spend about an hour and go through the gazillion videos explaining the basics of vba on youtube.
 

rahulbassi

Board Regular
Joined
Aug 14, 2011
Messages
130
Thanks for explaining......I think I'm getting the things now......will surely follow the videos for coding in VBA.....afterall the quality of code too matters.......
 

Watch MrExcel Video

Forum statistics

Threads
1,123,270
Messages
5,600,638
Members
414,398
Latest member
dhune

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
Top