I have a current worksheet that needs to have values from another worksheet when values from a certain range are changed. Also, I need to watch a certain cell value to execute another action, for this case, show a Msgbox.
I am usingWorksheet_Change(ByVal Target As Range)
event but the whole code does not work when I specify Select Case "$G$6"
and Case "$G$24:$H$54"
and tried Case Else
but did not work.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
Select Case Target.Address
Case "$G$6"
If InStr(1, Range("G6"), "PUMP") > 0 Then
MsgBox ("Pump")
ElseIf InStr(1, Range("G6"), "SKID") > 0 Then
MsgBox ("Skid")
End If
Case "$G$24:$H$54"
If Not Application.Intersect(Target, Range("G24:H54")) Is Nothing Then
If InStr(1, Range("G24"), "Calculate") > 0 And InStr(1, Range("G25"), "Outside Shelter") > 0 Then
Cells(19, 8).Value = Sheets("1").Cells(159, 6).Value
Cells(20, 9).Value = Sheets("1").Cells(163, 6).Value
Cells(19, 11).Value = Sheets("1").Cells(160, 6).Value
Cells(20, 10).Value = Sheets("1").Cells(164, 6).Value
ElseIf InStr(1, Range("G24"), "Calculate") > 0 And InStr(1, Range("G25"), "Inside Shelter") > 0 Then
Cells(19, 8).Value = Sheets("1").Cells(182, 6).Value
Cells(20, 9).Value = Sheets("1").Cells(187, 6).Value
Cells(19, 11).Value = Sheets("1").Cells(183, 6).Value
Cells(20, 10).Value = Sheets("1").Cells(188, 6).Value
End If
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Select
End Sub
This is a possible solution:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
Select Case True
Case Not Intersect(Target, Range("G6")) Is Nothing
If InStr(1, Range("G6"), "PUMP") > 0 Then
MsgBox ("Pump")
ElseIf InStr(1, Range("G6"), "SKID") > 0 Then
MsgBox ("Skid")
End If
Case Not Intersect(Target, Range("G24:H54")) Is Nothing
If InStr(1, Range("G24"), "Calculate") > 0 _
And InStr(1, Range("G25"), "Outside Shelter") > 0 Then
Cells(19, 8).Value = Sheets("1").Cells(159, 6).Value
Cells(20, 9).Value = Sheets("1").Cells(163, 6).Value
Cells(19, 11).Value = Sheets("1").Cells(160, 6).Value
Cells(20, 10).Value = Sheets("1").Cells(164, 6).Value
ElseIf InStr(1, Range("G24"), "Calculate") > 0 _
And InStr(1, Range("G25"), "Inside Shelter") > 0 Then
Cells(19, 8).Value = Sheets("1").Cells(182, 6).Value
Cells(20, 9).Value = Sheets("1").Cells(187, 6).Value
Cells(19, 11).Value = Sheets("1").Cells(183, 6).Value
Cells(20, 10).Value = Sheets("1").Cells(188, 6).Value
End If
End Select
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
The idea is use Select Case True
, which selects the Not Intersect(Range1, Range2) Is Nothing
. And in general, it is better to work with the Range("G6")
and compare it with Target
than with $G$6
and compare it with Target.Address
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments