+ Reply to Thread
Results 1 to 5 of 5

Passing Values from User Form

Hybrid View

  1. #1
    Registered User
    Join Date
    07-10-2012
    Location
    Pune, India
    MS-Off Ver
    Excel 2010
    Posts
    28

    Passing Values from User Form

    Hi ,

    I have a need of using Userforms, through which i need to get information or data. The gathered data from userform shall be further used for the calculation in the main sub or other sub ..

    so request you for the information ,

    I think , the information is need in two fronts,
    1. passing variables from one sub to another
    2. passing data (i.e., from ComboBox to Main Sub) from userform

    I have simplified the sheet for understanding, I shall attach herewith,

    Thanks in adv for the help !!

    Regards,
    Sri
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Passing Values from User Form

    I'm sorry can you explain a little more what you are trying to do? From what I see yu have accomplishes passing the values selected in the combobox to the second worksheet. I'm not sure why you need to pass a variable but the way to do it is to call the variable outside of the Sub procedures and define it in one of the sub procedures.

    For example:

    Dim x As Long   'variable can be defined anywhere and passed anywhere
                    'so in the example you can define x in the userform intitialize event
                    'and then call it in the commandbutton1 click event. Notice x is defined outside of the private sub's
    
    Private Sub UserForm_Initialize()
    Label1.Caption = ThisWorkbook.Sheets("Sheet2").Range("S10").Value
    
    x = 3   'defining x on userform open
    End Sub
    
    
    Private Sub CommandButton1_Click()
    Dim VarT As String
    
    MsgBox x 'giving user msgbox with x which would be 3 defined in the initialize
    If ComboBox1.Value <> BLANK Then
    VarT = ComboBox1.Value
    ThisWorkbook.Sheets("Sheet2").Range("A10").Value = VarT
    Me.Hide
    End If
    Exit Sub
    End Sub
    To pass a variable into a macro from a userform you would use the Global variable
    For example in the Userform

    Private Sub UserForm_Initialize()
    Label1.Caption = ThisWorkbook.Sheets("Sheet2").Range("S10").Value
    
    x = 3   'defining x on userform open
    End Sub
    In Module1
    Global x As Long 'define x as a global variables outside of the macro 
    
    Sub macro1()
    MsgBox x 'will give message box with 3 as x is defined in the Userform
    End Sub

  3. #3
    Registered User
    Join Date
    07-10-2012
    Location
    Pune, India
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Passing Values from User Form

    Thank you for the reply,
    I updated the sheet/ code with global variables, even then , i could not able to get the desired output.

    Here i put this way, Say I have a Product ABC with many variants, in which, i have placed 2 variants , Var1 and Var2. If Variant 1 is needed , then user need to click Question1 YES, so that variant 1 can be selected. Similar with Question2 (Variant 2)

    I want to keep a single userform to all the options, so i am changing label by keeping the content in some cell , and copying it from there -- which is also sometimes not working

    If User need variant 1 to be included in the product ABC, he clicks Option button1, then Userform appear with this code.

    Global Variable:
    Dim VarT As Integer, Var1 As Integer, Var2 As Integer
    Userform Initialization Code
    Private Sub UserForm_Initialize()
    Label1.Caption = ThisWorkbook.Sheets("Sheet2").Range("S10").Value 'At times this operation is not getting executed, all the time it is showing the option 1 label
    End Sub
    which is somtimes not working !! it is showing the same option1 label!!

    Userform OK button code:
    Private Sub CommandButton1_Click()
    If ComboBox1.Value <> BLANK Then
    VarT = ComboBox1.Value
    Me.Hide
    End If
    Exit Sub
    End Sub
    Variant 1 code : which has to save the VarT content into some other variable as VarT is dynamically changing variable.
    Public Sub OptionButton1_Click() 'This is a option in the costing whether to include the price of this the variant !
    Dim Var1 As Integer
    ThisWorkbook.Sheets("Sheet2").Range("S10").Value = "Do you want to include Option 1 value in the price:"
    UserForm1.Show
    Var1 = VarT
    End Sub
    The main code which has to sum up the variants with the base main cost of the product ABC,
    Sub Main()
    Dim FinalVal As Integer
    'MsgBox VarT
    FinalValue = 3 + Var1 + Var2 ' It has to display the cost of the product with variants that calculated
    MsgBox "The final value is : " & FinalVal ' Instead it is desplaying 0 :(
    
    End Sub
    The problems that i am facing are :

    1. The label is sometimes not changing to option 2 content , it is still says Option 1
    2. The answer is not desired , answer is coming to 0 , which should be minimum 3 if no variants there !!

    Please help me!!

    Thanks !

    WR
    Sri

  4. #4
    Registered User
    Join Date
    07-10-2012
    Location
    Pune, India
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: Passing Values from User Form

    Sry i did not attach the excel sheet ,

    Please find the attached excel sheet with code !!
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Passing Values from User Form

    Hello there,

    Alright, try the attached revision.

    Thanks!
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1