Why is dynamic public variable returning blank?

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Title

I'm trying to use MyUser and MyDate in many modules but it just returns blank.

Code:
Public MyUser As String
Public MyDate As String


Sub GetMyUser()


    Dim MyUser As String
    
    MyUser = Application.WorksheetFunction.Proper(Application.UserName)


End Sub


Sub GetMyDate()


    Dim MyDate As String
    
    MyDate = Format(Now(), "DD-MM-YYYY HH:MM")


End Sub


Sub Test()


    MsgBox MyDate
    MsgBox MyUser


End Sub
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Both the subs that set the variables have declared their own local variables, so the public variables are never set, even if you have called them before the Test routine.
 
Upvote 0
Beaten2it
 
Last edited:
Upvote 0
Remove the the Dim lines from the subs that set the values.
 
Upvote 0
Remove the lines :
Dim MyUser As String and Dim MyDate as String.

Also, make you sure the Public Module variables are declared in a Standard Module.

Yes they are in a standard module, I removed the Dim statements but still returning blanks in a messagebox.

If I call the procedures inside sub then it works fine, is it possible to not call the procedure but use the variable set?

Code:
Public MyUser As String
Public MyDate As String


Sub GetMyUser()


    MyUser = Application.WorksheetFunction.Proper(Application.UserName)
    
End Sub


Sub GetMyDate()
    
    MyDate = Format(Now(), "DD-MM-YYYY HH:MM")


End Sub


Sub Test()

    MsgBox MyDate
    MsgBox MyUser

End Sub
 
Last edited:
Upvote 0
No. It sounds like you actually want functions to me:

Code:
Function GetMyUser() As String

    GetMyUser = Application.WorksheetFunction.Proper(Application.UserName)
    
End Sub


Function GetMyDate() As String
    
    GetMyDate = Format(Now(), "DD-MM-YYYY HH:MM")


End Sub


Sub Test()

    MsgBox GetMyDate
    MsgBox GetMyUser

End Sub
 
Upvote 0
Yes they are in a standard module, I removed the Dim statements but still returning blanks in a messagebox.

If I call the procedures inside sub then it works fine, is it possible to not call the procedure but use the variable set?

Code:
Public MyUser As String
Public MyDate As String


Sub GetMyUser()


    MyUser = Application.WorksheetFunction.Proper(Application.UserName)
    
End Sub


Sub GetMyDate()
    
    MyDate = Format(Now(), "DD-MM-YYYY HH:MM")


End Sub


Sub Test()

    MsgBox MyDate
    MsgBox MyUser

End Sub

You need to call the 2 procedures to set the variables values .. How are you expecting the variables to be set ?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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