Not able to pass variable between Modules

jakeman

Active Member
Joined
Apr 29, 2008
Messages
325
Office Version
  1. 365
Platform
  1. Windows
Hi guys - I'm trying to store a variable in one module that is created by a user input box and retrieve that variable in another module, but it is not working properly. I've verified that my module is global, not specific to a sheet.

The findString variable is not passing for some reason, it keeps showing up as empty when I run Sub FindNextString.
Here is my code:

Code:
Option Explicit
Public findString, findString2 As String
Public Sub FindFirstString()
Dim findString, findString2 As String
Dim rng As Range
    
findString = InputBox("Enter the prefix of the part # you're looking for: AA or BB")
findString2 = findString & "*"

    If Trim(findString2) <> "" Then
        With Sheets("Parts").Range("A2:G500")
            Set rng = .Find(What:=findString2, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not rng Is Nothing Then
                Application.Goto rng, True
                MsgBox "Value has been found in cell: " & ActiveCell.Address
                
            Else
                MsgBox "Nothing found"
            End If
        End With
    End If

End Sub
Sub FindNextString()
Dim rng As Range

findString2 = findString & "*"

    If findString2 <> "" Then
        With Sheets("Parts").Range("A2:G500")
            Set rng = .Find(What:=findString2, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not rng Is Nothing Then
                Application.Goto rng, True
                MsgBox "Value has been found in cell: " & ActiveCell.Address
                
            Else
                MsgBox "Nothing found"
            End If
        End With
    End If

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You've defined them twice; once as global and once local:

Code:
Option Explicit
Public findString, findString2 As String ' - this is global
Public Sub FindFirstString()
Dim findString, findString2 As String ' - this is local

Suggest you remove the Dim of those variables.

WBD
 
Upvote 0
Try removing
Code:
Dim findString, findString2 As String
Also on both your declarations findString is variant not string, as you have not assigned it.
Make your public like this
Code:
Public findString as String, findString2 As String
 
Upvote 0
It "worked" for me.

1. Variable types are set each, not by the last one set in a Dim line.
Code:
Public findString$, findString2$
2. You Dimmed the two variables twice. It is redundant.
3. FindNext does not find the next one. As coded, it finds the first one.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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