VBA Integer Returning to 0

nmbc99

New Member
Joined
Apr 28, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hello, I am trying to use a variable from a module into a sheet function. The sheet is able to call the function in the module, but the data entered into the module doesn't stick when I try to pull it back. I really thought I had these setup correctly to share variables. Can someone help?

FYI! A Row in columns N, S, and U all need to have data in them. However, to get the code to work either enter data into column N last or double click into a cell in N with data already in it and hit enter. Like this:

1664919892759.png


Code In Sheet2 (There is a LOT more code to this, but I cut it down for ease of use):
VBA Code:
Public r As Long
Public BiggerR As Integer
Public SmallerR As Integer


Public Sub Worksheet_Change(ByVal Target As Range)
        Dim lastCol As Variant
        Dim lastRow As Variant
        Dim cel As Variant
        Dim test As Variant
        Dim iNum As Integer
        Dim Questions As Integer
        Dim Insulation As Integer
        Dim InsType As Integer


If Not Intersect(Target, Range("N9:N16000")) Is Nothing Then
    r = Target.Row
    
    If IsEmpty(Sheet2.Range("S" & Sheet2.r).Value) = False And IsEmpty(Sheet2.Range("N" & Sheet2.r).Value) = False And IsEmpty(Sheet2.Range("U" & Sheet2.r).Value) = False And IsEmpty(Sheet2.Range("AG" & Sheet2.r).Value) = False Then
Checky = Application.InputBox("Do you want to update the component description?" & vbNewLine _
            & "1:   Yes I want to update the component description" & vbNewLine _
            & "2:   No, I don't want to update the component description", "Obtain Range Object", Type:=1)
            
            Select Case Checky
Case 1
    'MsgBox "this is a placeholder to leave the select"
    GoTo MainBadie:
Case 2
'MsgBox "that's cool fam"
End Select

Else
GoTo MainBadie:

End If
Exit Sub
MainBadie:
    If IsEmpty(Range("S" & r).Value) = False And IsEmpty(Range("U" & r).Value) = False Then
        
        
        If Sheet2.Range("S" & Sheet2.r) Like "*Weld*" Then

    PrePWHT = Application.InputBox("Does the component need any pre-weld heat treatment or post-weld heat treatment?" & vbNewLine _
            & "1:   Yes" & vbNewLine _
            & "2:   No", "Obtain Range Object", Type:=1)
            
            Select Case PrePWHT
                Case 1
                Sheet2.Cells(Sheet2.r, 33).Value = "Y"
                Case 2
                Sheet2.Cells(Sheet2.r, 33).Value = "N"
                
            End Select
            
            Welding = Application.InputBox("Type how many welding spots there are for this size of pipe", "Obtain Range Object", Type:=1)
            Sheet2.Cells(Sheet2.r, 25).Value = Welding
            Sheet2.Cells(Sheet2.r, 26).Value = Welding * Sheet2.Cells(Sheet2.r, 21)
            Sheet2.Cells(Sheet2.r, 23).Value = Welding * Sheet2.Cells(Sheet2.r, 21)
            Sheet2.Cells(Sheet2.r, 24).Value = "DI"
        Else
    
    If Sheet2.Range("S" & Sheet2.r) Like "*Testing*" Then
            GoTo TESTING_Call:
            End If
    
       'Sheet8.Activate
       
       'On Error GoTo DumbErr:
        
        'Set ItemRange = Range(Sheet8.Cells(2, 16).Value)

        With ItemRange
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''This is where I call the function in the Module        
        Call Reducey
        
        MsgBox Module2.SmallerR
        

    

End With

    End If
    
    
End If
End If

Exit Sub
TESTING_Call:

Exit Sub
DumbErr:
Sheet2.Activate
MsgBox "You either canceled, or did not put a range in"

End Sub

Code In Module 2 (What I am calling into Sheet2):

VBA Code:
Public BiggerR As Integer
Public SmallerR As Integer

Public Sub Reducey()
Dim BiggerR As Integer
Dim SmallerR As Integer
Reduce = Application.InputBox("Does this component reduce/increase in size?" & vbNewLine _
            & "1:   Yes " & vbNewLine _
            & "2:   No", "Obtain Range Object", Type:=1)

            Select Case Reduce
                Case 1
                    BiggerR = Application.InputBox("Type Bigger Size in inches", "Obtain Range Object", Type:=1)
                    SmallerR = Application.InputBox("Type Smaller Size in inches", "Obtain Range Object", Type:=1)
                    MsgBox BiggerR
                    MsgBox SmallerR
                Case 2
                    Exit Sub
            End Select
End Sub
 

Attachments

  • 1664919877775.png
    1664919877775.png
    29.4 KB · Views: 8

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Haha.... so I figured it out. I just needed to have the "Dim X As X" in the sheet rather than the module. Sorry for the post 😅
 
Upvote 0
Haha.... so I figured it out. I just needed to have the "Dim X As X" in the sheet rather than the module. Sorry for the post 😅
Oh and I need to type the module: Module2.X would bring in the value
 
Upvote 0
Solution

Forum statistics

Threads
1,224,813
Messages
6,181,112
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