Pass variable from Private Sub Worksheet_SelectionChange to a Module ?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Hi,
having trouble figuring this out, i have the following sheet code:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Not Intersect(Target, Range("Z2:Z50")) Is Nothing Then
            If Target.Offset(0, 1).Value = Target.Offset(0, 2).Value Then
                'do nothing
            Else
                myvar1 = target.value
                call Macro2
            End If
        End If
End Sub

and i want to pass the myvar1 to a macro but unsure how

so i can then use myvar1 like this example

Code:
Sub Macro2()
If myvar1 > 50 then
msgbox (myvar1 & "Greater than 50")
End If
End Sub

Appreciate any help
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I think I can just use:

myvar1 = active cell.value
At top of Macro2

Still curious about the variable though if it can be passed from sheet to module
 
Upvote 0
Try making the procedure a part of the sheet code, not a standard procedure. Copy and paste this to the sheet (right-click sheet tab>View Code and paste):
Code:
Dim myVar1
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set Target = Target(1)
If Not Intersect(Target, Range("Z2:Z50")) Is Nothing Then
    myVar1 = Target.Value
    Call myMacro
End If
End Sub
Sub myMacro()
x = myVar1
MsgBox "Called from worksheet selection_change event code" & vbNewLine & "myvar1 = " & x
End Sub
 
Upvote 0
Try changing Macro2 like this.
Code:
Sub Macro2(theVar)
    If theVar > 50 Then
        MsgBox theVar & " - Greater than 50")
    End If
End Sub
You can then call it from the SelectionChange event like this.
Code:
myvar1 = Target.Value
Call Macro2(myvar1)
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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