Consulting

Results 1 to 5 of 5

Thread: Public Variable won't pass

  1. #1
    VBAX Newbie
    Joined
    Feb 2018
    Posts
    2
    Location

    Question Public Variable won't pass

    Hi, I am new to the forum but hope that maybe some of you smart people might know the answer to my problem? I attached a test.xlsm file in which I
    - first run the userform1 in which I declare a public variable and pass a value to it.
    - then close that userform1
    - and then want to display that variable again when I push the BTN2 button that is on Sheet1 of the excel file

    Something goes wrong because the value of the public variable is not shown?
    thanx is you can help!!

    code for the userform:
    Public chosendate As Date
    
    
    Private Sub CommandButton1_Click()
    chosendate = DTPicker1.Value
    MsgBox chosendate
    Exit Sub
    End Sub
    code for the button on Sheet1
    Private Sub CommandButton1_Click()
    MsgBox "the date = " & chosendate
    End Sub
    Attached Files Attached Files

  2. #2
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    U need to put the variable in a module. Declaring at the top of the userform only makes it available to all procedures in the userform. HTH. Dave

  3. #3
    VBAX Newbie
    Joined
    Feb 2018
    Posts
    2
    Location
    thanx a lot Dave!

  4. #4
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    You are welcome. Thanks for posting your outcome. Dave

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    code for the button on Sheet1
    Private Sub CommandButton1_Click()
    MsgBox "the date = " & chosendate
    End Sub

    Private Sub CommandButton1_Click()
    
    If UserForm1 Is Nothing Then Exit Sub
    If UserForm1.chosendate = 0 then exit sub
    
    MsgBox "the date = " & Format(UserForm1.chosendate, "dd/mm/yyyy")
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •