Breaking User Form Code into modules question
You have probably read Chip's narrative by now. He paints a broader picture
than what I got into. However, to clarify the difference between public and
private procedures, Public simply means that it is accessible by other code
and can be incorporated into different procedures by call up during runtime.
Private does not mean that other people cannot access the code. It means
that the code is restricted to operate within the particular object that
holds the code, such as UserForm, ThisWorkbook or sheet modules and
generally speaking cannot be called by a procedure outside of those objects.
Notice that I say, generally speaking. There are exceptions to the rule but
I will not go into that here. For planning purposes in designing a project,
stick with the basic conditions of Public and Private and you will be better
off.
You really need to get a book that explains the basics of the VBA
conventions, although some of these things are explained in one of the
manuals included in VBA help file. You just have to look through it for a
while to find the parts that give the broader explanations. You can test a
lot of the things you want to do and if they won't work, the worst that can
happen is you get an error message saying you can't do that.
But here is something that might make sense to you as an engineer. You can
do modular programming without using a bunch of different code windows.
Here is a form of modular programming that can all be in one code window.
Sub Main()
Macro1
MsgBox "Macro1 Completed"
Macro2
MsgBos "Macro2 Completed"
Macro3
MsgBox "Program Completed"
End Sub
Sub Macro1()
'Declarations
'Variable assignment
'Code body
End Sub
Sub Macro2()
'Same pincipal as Macro1
End Sub
Sub Macro3()
'Same principal as Macro1
End Sub
The Main procedure simply calls the other 3 procedures which can perform
completely different actions, maybe one copies data, another sets formats
and the other does clean-up work or opens a UserForm for user interface.
The point is that by breaking the code into small procedures it is being
modularized and any one of thoses separate procedures can be called in any
order by the main procedure. It is like taking tools out of a tool kit as
you need them.
"Brian" wrote in message
...
I was hoping to make all the modules Private Sub. I do not want them being
changed after I am done.
All the subs in the User Form are are Private. Can I just cut and paste
them
into a module and then in User Code Window call that Sub.
Private Sub Update_Installer_Forms_10_Click()
Call Update_Installer_Forms_10_Click() 'Macro1 located in
Module1
End Sub
"JLGWhiz" wrote:
Hi Brian,
1: My question is in the UserForm Code Window how do I refer to a module?
A1: You can call any procedure that is in the public modules (Module1,
Mocule2, etc.) from the UserForm code module. If you are using event
code
to respond to some user's interaction, then in the event code you would
call
the working procedure out of the public module:
Private Sub Listbox1_Click()
Call Macro1 'Macro1 located in Module1
End Sub
2: If the Code is to large can one Module call another module?
Something like User form Code calls Module 1 and at the end of module
1
code, it calls for module 2, etc...
A2: Yes, any public module can call from and respond to any other
public
module.
..
3: Most of my Code is With statements / End With then more code.
A3: So? That is just a style of writing code. I do not believe it
would influence whether the code can be called from another module or
not.
As a general rule, private procedures can call any public procedure, but
the
reverse is not necessarily true.
"Brian" wrote in message
...
I have a user form with alot of code in it. As of now all the code is in
the
Userform
Code window, but there is so much code that it's getting confusing. I
have
one Sub that is to large and needs to be split into several subs.
When I was done I was going to break all the Subs down into different
modules to clean it up. I guess I will have to do that now, but I am
not
sure
how to do it.
1: My question is in the UserForm Code Window how do I refer to a
module?
2: If the Code is to large can one Module call another module?
Something like User form Code calls Module 1 and at the end of
module 1
code, it calls for module 2, etc....
3: Most of my Code is With statements / End With then more code.
Here is an Example of the Code in the UserForm Code Window:
Private Sub Update_Installer_Forms_10_Click()
With Workbooks("Master Installer Forms.xlsm").Sheets("Install Pack
Con")
**Code for the sub is here**
End With
With Workbooks("Master Installer Forms.xlsm").Sheets("Install Chk
List")
**Code for the sub is here**
End With
End Sub
I really need to sort this out so it's not so confusing to work on. Can
anyone please help me or explain it in english to me.
.
|