Unable to pass variable between Userforms

  • Thread starter Thread starter PGM
  • Start date Start date
P

PGM

I have thoroughly searched the group for an answer to this problem, but
nothing seems to fit the bill.

I have created a userform that allows the user to select a variety of
options in order to process incoming data files. Since our group is
currently in the middle of two different processing years, I need to
have the user indicate the year in which they are working. This is
done with an Application.Inputbox just before the userform is closed.

I have declared the variable, MyYear, as Public in a general module,
but it comes up defined as Empty when the next userform appears. I
used the Application,Inputbox method so I could force the end-user to
input a year, or exit the sub if they cancelled the operation, so I had
to Dim MyYear as Variant.

Is this why the variable won't be retained by the Public declaration?
I have tried:

Public MyYear As Variant

but have no more luck than any other method.

I have other variables in the general module that are coming through
just fine, but none of them are created using an input box.

Thanks in advance for any and all suggestions (including, "stop writing
code."), and let me know if a snippet of my code would help.

Paul
 
Private Sub CommandButton1_Click()
Dim dum As Variant
dum = Application.InputBox("enter 4 digit year:", _
Default:=Year(Date), Type:=1)
If VarType(dum) = vbBoolean Then
MsgBox "No year entered, quitting"
Exit Sub
End If
MyYear = CLng(dum)
Unload Me
DoEvents
'MsgBox MyYear

End Sub

In a general module:

Public MyYear as Long



worked for me.
 

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

Back
Top