Worksheet_change with Public Variale

ROUKWA

New Member
Joined
Feb 3, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I appreciate you all in advance.
Now I'm struggle to break down VBA codes.
My demands are three.
1. Declare Public Variable
2.Use it in worksheet_change
3.Call certain procedure from worksheet_change
Thus I have three codes below, Code1 in worksheet module("Sheet1")
And Code2, 3 in standard module.
The problem is it occurs run time error5.
I googled but I cant find suitable resolve to this issue.
Any help and advice would be appreciated.

Code1.
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, myRange) Is Nothing Then
        Call ChangeColorYesNo(Target)
    End If
End Sub

Code2 , 3
VBA Code:
Option Explicit

Public myRange As Range

Sub SetMyRange()
    Set myRange = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10")
End Sub

Sub ChangeColorYesNo(ByVal Target As Range)
    Dim cell As Range
    For Each cell In Intersect(Target, myRange)
        If cell.Value = "Yes" Then
            cell.Interior.Color = vbGreen
        ElseIf cell.Value = "No" Then
            cell.Interior.Color = vbRed
        Else
            ' Do nothing
        End If
    Next cell
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,224,817
Messages
6,181,148
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