Userform Input as global variable that would be used in another module

lyly25

New Member
Joined
Feb 14, 2019
Messages
1
I am fairly new at VBA and have been working on this for the past couple of days. I am having trouble using the values submitted through a UserForm. I would like to use the values (rows, columns, x distance and y distance) in another module to set the number of loops needed to populate my data as well as the value needed to add to each data when populating them. The values that o store in nRow, nCol... could not be retrieved in the module.

UserForm code:
Code:
Private Sub CheckBox1_Click()    Dirt = 2
End Sub


Private Sub cmdbtn_cancel_Click()
Unload Me
End Sub


Private Sub cmdbtn_enter_Click()
    
    nRow = DataInput.txtRow
    nCol = DataInput.txtCol.Value
    xCoord = DataInput.txtx.Value
    yCoord = DataInput.txty.Value
    DataInput.Hide
    Unload DataInput
    
End Sub




Private Sub txtCol_Change()


End Sub


Private Sub UserForm_Initialize()
    Dirt = 1
    txtRow.SetFocus
End Sub

Module Code:
Code:
Public nRow As Integer
Public nCol As Integer


Public xCoord As Long
Public yCoord As Long


Public Dirt As Long


Public Sub TestDir()


DataInput.Show

Range("A13").Select
 ' start of data table
 ActiveCell.Offset(1, 4).Select
 ' go to first value of x-ref
 Range(ActiveCell, ActiveCell.End(xlToRight).End(xlDown)).Select
 ' select all values of x-ref and y-ref


 For Each c In Selection
 c.Activate
 ActiveCell.FormulaR1C1 = "= " & ActiveCell.Formula
 Next c

If Dirt = 1 Then
 
     Do While cntCol <= nCol
     
        If cntCol = nCol And cntRow = nRow Then
            ActiveCell.CurrentRegion.Select
            Selection.End(xlToRight).Select
            Selection.End(xlDown).Select
            ActiveCell.FormulaR1C1 = "'" & Num
        Else
            Num = Num + 1
            ActiveCell.CurrentRegion.Select
            Selection.End(xlToRight).Select
            Selection.End(xlDown).Select
            ActiveCell.FormulaR1C1 = "'" & Num
        End If
        
        If cntRow <= nRow Then
        
            cntRow = cntRow + 1
            
            If cntRow = nRow + 1 And cntCol = 1 Then
               If cntCol Mod 2 <> 0 Then
                  Paste_Sub_Y (yCoord)
               Else
                  Paste_Add_Y (yCoord)
               End If
            
            ElseIf cntRow <= nRow Then
               If cntCol Mod 2 <> 0 Then
                  Paste_Sub_Y (yCoord)
               Else
                  Paste_Add_Y (yCoord)
               End If
            End If
         

'<------------Other codes------------->

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You must indicate the module where you have the code, for example, if you have it in module1:

Code:
Private Sub CheckBox1_Click()
    Module1.Dirt = 2
End Sub


Private Sub cmdbtn_cancel_Click()
    Unload Me
End Sub


Private Sub cmdbtn_enter_Click()
    Module1.nRow = DataInput.txtRow.Value
    Module1.nCol = DataInput.txtCol.Value
    Module1.xCoord = DataInput.txtx.Value
    Module1.yCoord = DataInput.txty.Value
    DataInput.Hide
    Unload DataInput
End Sub


Private Sub UserForm_Initialize()
    Module1.Dirt = 1
    txtRow.SetFocus
End Sub

Note: The value in txtRow, txtCol, etc must be numeric values, because in your declaration you have them as integer
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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