Passing variables between a form and macro

  • Thread starter Thread starter David
  • Start date Start date
D

David

Hi

I have a working Excel VBA macro and would like to use a form for all the
inputs. I have the form and the code behind the text/combo boxes etc. I just
don't know how to call the form so that the data collected is passed to the
variables in the main macro. I searched for this information but can only
find info on creating the form rather than having it run with a macro. I'd
appreciate any info, links etc. It would be great if you could point me to
an example form with all the suurounding code, declarations etc. Can the
form be used for the output of the macro?

Thanks
David
 
Create public variables store them in the macro module in the declarations,
before the macro, and load them from within the form.
 
Thank you for your response. I'm being a bit thick here and need to ask what
may be obvious. I used the word Public instead of Dim at the beginning of
the macro, where I declare the variables but get 'Invalid attribte in Sub or
Function'. I'm not sure how to create public variables in the macro and how
to load them from within the form. I have a simple form with Initialise and
Click subroutines. I'm trying to gather data from the form to use in the
macro.

Thanks
David
 
I repeat

.... Create public variables store them in the macro module in the
declarations, before the macro ...

the variable should be defined before the macro not within it when you make
it public.

To load them in the form just means assign a value to that variable.

Public myVar as String

Sub Macro1()

Msgbox myVar

End Sub

Private Sub CommandButton1_Click()
myVar = "testing"
Unload Me
End Sub
 
Bob

Thanks for your patience. Just wanted to let you know that your help has
resolved my problem - very much appreciated.

David
 
Back
Top