Variables initiation

A

Alberto Ast

I need to initiate some variables...

to initiate the variables within a form I use

Private Sub UserForm_Initialize()
......
End Sub

How do I do it within a module?
 
R

Ryan H

You declare variables many ways. Check this link out by Chip Pearson. It
helped me when I was learning.

Hope this helps! If so, let me know, click "YES" below.
 
A

Alberto Ast

Thanks Ryan..

Somehow I can not see this post when I list all those I have had some
activities but Mike H have forward me the link and I was able to see the
post... any way... pearson link showed me how to declare variables but how do
I gave them an initial value different than its default value?
 
M

Mike H

Hi,

In a macro the variables are 'generally' initialised at the top of the code:-

Sub MyMacro()
Dim x as Long
Dim MyRange as range

etc.

By default when initialised they ere empty and you then have to assign a value

Sub MyMacro()
Dim x as Long
Dim MyRange as range
x=999
set MyRange=range("A1:A1000")

Mike
 
A

Alberto Ast

I have several macros than while runing the program I can execute one or the
other but need a public variable to be set up to a certain value. Then the
variable keep changing during the execution of several macros. If I do not
initialize my variable to a specific value my macros might fail depending on
the order the macros are selected to be excecuted. This is why I need to
initilize my variable before any macro is excecuted so regardless or the
sequence my progam will work ok.

my variable is like this
Public wHide, wHide2, wHide3 As Worksheet

and I assign different worksheets to the variables during my progams and
then I make them visible tru o false... but if I execute the

Sheets(wHide).Visible = False
before it is assigned to a sheet then my progma fail so I want to initialy
ie to any sheet so it will not fail

when I do
Private Sub UserForm_Initialize()
......
End Sub

in a user form it initialize some of my variables and I do nto need to run
this Sub it just does it... how can I do the initialization in a module.
 
R

Ryan H

All userforms run an Intialize Event when the userform is first loaded into
memory. If you have code in it then the code will be ran, if not then it
continues on. You can set each of your worksheet variables at the beginning
of each macro like this

Set wHide = Sheets("Sheet1")

I did notice one thing in your previous post that may be giving you some
problems.

You use this to declare your worksheets. This should be in a standard
module. Which is correct!

Public wHide, wHide2, wHide3 As Worksheet

But using this code to use your publically declared worksheets is incorrect.

Sheets(wHide).Visible = False
You are using wHide as a string not as a worksheet.

You should use:

wHide.Visible = False

or

Sheets(wHide.Name).Visible = False
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top