declaring variables - help!!!

slim

New Member
Joined
Feb 24, 2002
Messages
42
Dear all,

I am having difficulty with the whole idea of setting variables.
I want to declare a variable in one user form that then gets used in a different user form.
I have tried making sure all subs are not private, I have tried declaring the variable in the option explicit section. I have tried declaring it at the start of a sub etc. I have been told to try dim myvariable as public, that doesn't work, I have tried public myvariable and public dim myvariable, I've tried the help files - I'm going loopy!
Each time I run the routines the variable shows up in the locals window but then later in a different form the variable when called is empty.

Can anybody explain how I keep a variable in memeory to use anywhere within a workbook/form

Many thanks

Andy
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
The approach I use with multiple forms, which works for me, but probably isn't the 'best' way, is to put all my variable handling procedures into a standard module and have events from within the form module call procedures from the standard module. In this way you can have all your variables declared in one or more standard modules which gets round the probelm of variables losing scope. Note that if you adopt this approach, from within the standard module you need to refer to controls on your userforms explicitly ie

Form1.TextBox1.Text rather than just TextBox1.Text
 
Upvote 0
Dear Mudface

thanks for both your replies, will get to work on them shortly, although I'm still not sure about the variables issue....lol
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,085
Members
448,548
Latest member
harryls

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