Passing data from one userform to a formula on another userform

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
In the code below, I declared lDz and lCs as public variables so I can use them in a formula located on another user form. Unfortunately the values will not pass to the formula and I get a Run-time error '11': Division by zero. I even created invisible text boxes and tried to assign the values to it. That didn't work either and I didn't want to do that anyway. Is there a simple method or explanation that can be given that explains the best way to pass values between forms that are not used in text boxes but in formulas that will always work. Thank you.

Code:
Option Explicit 
Public sUOM As String
Public lDz As Long
Public lCs As Long


Private Sub cmdbtnAddItem_Click()
    
    Dim LastRow As Long
    Dim cnt As Integer
    Dim ctl As Control
    
    Worksheets(Chattfrm.cmbSDPFLine.Value).Select
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row + 1
    
    If rngItem Is Nothing Then
       Set rngItem = Cells(LastRow, "B")
    End If
    If rngItem.Offset(, -1).Value = "" Then
        Cells(LastRow, 1).Value = Len(Me.txtbxDescription.Value)
    Else
        rngItem.Offset(, -1).Value = Len(Me.txtbxDescription.Value)
    End If
    
    If rngItem.Offset(, 0).Value = "" Then
        Cells(LastRow, 2).Value = Me.txtbxPrdctCde.Value
    Else
        rngItem.Offset(, 0).Value = Me.txtbxPrdctCde.Value
    End If
        
    If rngItem.Offset(, 1).Value = "" Then
        Cells(LastRow, 3).Value = Application.Proper(Me.txtbxDescription.Value)
    Else
        rngItem.Offset(, 1).Value = Application.Proper(Me.txtbxDescription.Value)
    End If
            
    If rngItem.Offset(, 2).Value = Me.txtbxDzPrCs.Value Then
        Cells(LastRow, 4).Value = Me.txtbxDzPrCs.Value
    Else
        rngItem.Offset(, 2).Value = Me.txtbxDzPrCs.Value
    End If
    
    If frmAddProduct.txtbxDzPrCs.Value = "" Then
        MsgBox "Please enter dozens per case.", vbCritical + vbDefaultButton1 + vbOKOnly, "Missing: Dozens per case"
        Me.txtbxDzPrCs.SetFocus
        Exit Sub
    Else
        rngItem.Offset(, 2).Value = Me.txtbxDzPrCs.Value
    End If
    
    If frmAddProduct.txtbxCsPerPal.Value = "" Then
        MsgBox "Please enter cases per pallet.", vbCritical + vbDefaultButton1 + vbOKOnly, "Missing: Cases per pallet"
        Me.txtbxCsPerPal.SetFocus
        Exit Sub
    Else
        rngItem.Offset(, 3).Value = Me.txtbxCsPerPal.Value
    End If
    
    For Each ctl In Me.Controls
        If TypeName(ctl) = "OptionButton" Then
            If ctl.Value = True Then
                cnt = cnt + 1
                Debug.Print "You have selected " & ctl.Caption
                rngItem.Offset(, 4).Value = ctl.Caption
                sUOM = ctl.Caption
            End If
        End If
    Next ctl
    If cnt = 0 Then
        MsgBox "Please select a unit of measure (UOM).", vbCritical + vbDefaultButton1 + vbOKOnly, "Missing: Unit of Measure"
        frmAddProduct.optEa.SetFocus
        Exit Sub
    End If
    
    Unload Me
    Chattfrm.Show
    lDz = Me.txtbxDzPrCs.Value
    lCs = Me.txtbxCsPerPal.Value
End Sub
Formula variables are to be used in.
Code:
textValUp = ((txtbxdz.Value) / lDz / lCs) + 0.5 - 1E-16
 

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.
Move the declarations to a standard code module.
 
Upvote 0
First let me say that I am still trying to learn and understand how use VBA, so please excuse some of my simple questions. So are you saying I need to create a module and place the code in it like this:
Code:
Sub variables()    lDz = frmAddProduct.txtbxDzPrCs.Value
    lCs = frmAddProduct.txtbxCsPerPal.Value
End Sub
If I am wrong, can you please give an example. Thank You
 
Upvote 0
No.

Insert a standard code module and put this in it:

Code:
Option Explicit 
Public sUOM As String
Public lDz As Long
Public lCs As Long

Then remove the declarations from any Forms modules.
 
Last edited:
Upvote 0
After I deleted the declarations you suggested, I only had to adjust a few things and it worked great. Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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