Make variable within a Sub equal to variable within a Private Sub

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Code:
Private Sub cmdbtnSubmit_Click()    Dim myWrkBk As Workbook
    Dim mySheet As Worksheet
    Dim textValUp As Integer
    Dim textValDown As Integer
    Dim numLenA As Integer
    Dim numLenF As Integer
    Dim startNum As Integer
    
    textValUp = (CInt(txtbxdz.Value) / txtDz / txtCs) + 0.5 - 1E-16
    textValDown = (CInt(txtbxdz.Value) / txtDz / txtCs) - 0.5 + 1E-16
    startNum = 1


    Set myWrkBk = ActiveWorkbook
    Set mySheet = myWrkBk.Sheets("Finished Goods Summary")
    Do
        If textValUp >= 30 Then
            numLenA = 30
        Else
            numLenA = textValUp
        End If
        If textvaldwn >= 30 Then
            numLenF = 30
        Else
            numLenF = textValDown
        End If
        
        With mySheet
            .Range("A9:A38").ClearContents
            .Range("E9:E38").ClearContents
            .Range("F9:F38").ClearContents
            .Range("G9:I38").ClearContents
            .Range("J9:K38").ClearContents


            If numLenA > 0 Then
                .Cells(9, "A") = startNum
                .Range(.Cells(9, "A"), .Cells(numLenA + 8, "A")).DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1
                .Range(.Cells(9, "E"), .Cells(numLenA + 8, "E")).Value = txtUOM
                .Range(.Cells(9, "G"), .Cells(numLenA + 8, "G")).Value = txtDz
            End If
            If numLenF > 0 Then
                .Range(.Cells(9, "F"), .Cells(numLenF + 8, "F")).Value = txtCs
                .Range(.Cells(9, "J"), .Cells(numLenF + 8, "J")).Value = txtCs * txtDz
            End If
        End With
        textValUp = textValUp - 30
        textValDown = textValDown - 30
        startNum = startNum + 30
        
    Loop While textValUp > 0
End Sub


Code:
Private Sub cmdbtnPrint_Click()
    Call IncrementPrint
End Sub


Code:
'Courtesy of www.extendoffice.com
Sub IncrementPrint()
    Dim xCount As Variant
    Dim xScreen As Boolean
    Dim I As Long
    On Error Resume Next
lInput:
    ' xCount = Application.InputBox("Please enter the number of Copies you want to print:", "Chattem Form")
    If TypeName(xCount) = "Boolean" Then Exit Sub
    If (xCount = "") Or (Not IsNumeric(xCount)) Or (xCount < 1) Then
        MsgBox "Error Entered, Please enter again", vbInformation, "Chattem Error Form"
        GoTo lInput
    Else
        xScreen = Application.ScreenUpdating
        Application.ScreenUpdating = False
        For I = 1 To xCount
            Range("'Placard'!$E$10").Value = I
            Sheets("Placard").PrintOut
            'ActiveSheet.Range("'Placard'!$E$10").Value = I
            'ActiveSheet.PrintOut
        Next
        ActiveSheet.Range("'Placard'!$E$10").ClearContents
        Application.ScreenUpdating = xScreen
    End If
    If textVal > 0 Then
        Sheets("Finished Goods Summary").PrintOut Copies:=1
    End If
    Application.Quit
End Sub
I have a submit and print buttons on my form. One of these are within a Private Sub. Unfortunately, I need to set the xCount within the Sub IncrementPrint() equal to the textValUp which is located within the Private Sub cmdbtnSubmit_Click(). So when it prints, it will print up to the value of textValUp. I am doing this so I don't have to change my code a whole lot. I admit it, I'm trying to take the easier route here. Or so I think is the easier route. Is this possible since one of the variables is within a Private Sub? Do I need to make the Private Sub into a Public Sub. If so how? I hope this makes sense. Thank you.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Best practice would be to pass the count as an argument, i.e.

Code:
Sub IncrementPrint(xCount As Long)

Then you can get rid of the InputBox block of code.

Your calling Sub will then look like:

Code:
Private Sub cmdbtnPrint_Click()
    
    Dim MyCount As Long
    
    MyCount = ....
    Call IncrementPrint(MyCount)

End Sub

I'm not totally clear how you want to calculate MyCount, because in your Sub cmdbtnSubmit_Click() its value changes inside a loop. Perhaps it's as simple as:

MyCount = (CInt(txtbxdz.Value) / txtDz / txtCs) + 0.5 - 1E-16 ?
 
Last edited:
Upvote 0
That is the correct formula to be used. So I would also need to include this formula inside the "Sub IncrementPrint"? Thank you for your help.
 
Upvote 0
So I would also need to include this formula inside the "Sub IncrementPrint"?

No, that's the point of passing the count as an argument:

Code:
Private Sub cmdbtnPrint_Click()
    
    Dim MyCount As Long
    
    '....
    MyCount = (CInt(txtbxdz.Value) / txtDz / txtCs) + 0.5 - 1E-16
    '....
    Call IncrementPrint(MyCount)

End Sub
Sub IncrementPrint(xCount As Long)
    
    '...
    MsgBox "The value of xCount passed to this sub is: " & xCount
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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