How can I carry a definition of a Variant from userform to worksheet?

laxcat73

Board Regular
Joined
Aug 5, 2011
Messages
143
This is my userform code:

Code:
Public Sub cmdOK_Click()
    Dim myUserName As Variant
    Dim myPassword As Variant
    Dim WB As Workbook
    Dim WS As Worksheet
    Set WB = ThisWorkbook
    Set WS = Worksheets("Essbase TCP")
        If frmEssb.txtUN.Value = "" Then
            frmEssb.txtUN.SetFocus
            MsgBox "Please enter your Username"
        End If
        
        If frmEssb.txtPW.Value = "" Then
            frmEssb.txtPW.SetFocus
            MsgBox "Please enter your Password"
        End If
    
    myUserName = Me.txtUN.Value
    myPassword = Me.txtPW.Value
    Unload Me
    
End Sub

I am trying to carry the "myUserName" and "myPassword" over to a sheet that's running an Essbase pull. I tried using the following in place of "myUserName = Me.txtUN.Value, myPassword = Me.txtPW.Value" but it does not allow this argument.

Code:
        WS("myUserName") = Me.txtUN.Value
        WS("myPassword") = Me.txtPW.Value



Is there any way to define one of these inputs as "myUsername" in my worksheet which is defined
Code:
Dim myUsername AsVariant
and used in this process
Code:
        x = EssVConnect(mySheetname, myUserName, myPassword, myServer, myApp, myDB)
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Andrew's not taking any prisoners today :rolleyes:

Normally you define your variables within a procedure and so they are only available to that procedure.

You can also define them as Public variables OUTSIDE of a procedure and then they are available to all procedures in the workbook

instead of

Code:
Private Sub myMacro()
 
  Dim myVariable as Variant
  myVariable = Cstr(format(now(),"dd/MM/YYYY"))
 
End Sub

do


Code:
Public myVariable as Variant
 
Private Sub myMacro()
 
  myVariable = Cstr(format(now(),"dd/MM/YYYY")) 
 
End Sub
 
 
Private sub myMacro2()
 msgbox myVariable
 
End Sub
 
Upvote 0
How do you close frmEssb?

If you don't unload it you could add this after frmEssb.Show.
Code:
myUserName = frmEssb.txtUN.Value
myPassWord = frmEssb.txtPW.Value
That will put the values in the variables and then you can unload the form like this:
Code:
Unload frmEssb
 
Upvote 0
You don't need public variables for this.:)
 
Upvote 0

Forum statistics

Threads
1,224,878
Messages
6,181,528
Members
453,053
Latest member
DavidKele

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