Combining Select Case as Cell and Range in Worksheet_Change

klove112

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
Vityata

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exclude Cell Range from Worksheet_Change function

From Dev

Combining 2 "Private Sub Worksheet_Change(ByVal Target As Range)" into 1

From Dev

Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

From Dev

Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

From Dev

Combining multiple macros (worksheet_change)

From Dev

Combining multiple Worksheet_Change macros

From Dev

Worksheet_Change setting target range is slow

From Dev

Expanding Worksheet_Change to Numerous Range Values

From Dev

create new worksheet after cell (in a range) change

From Dev

Excel Macro, Combining two Private Sub worksheet_change

From Dev

Excel VBA combining Worksheet_Change codes for 2 target addresses

From Dev

How to manually modify a cell that is being edited in a worksheet_change event?

From Dev

Multiple worksheet_change with same macro with different reference cell

From Dev

Worksheet_Change(ByVal Target As Range), Target always equals Nothing

From Dev

Excel VBA Worksheet change, entering a range instead of one cell

From Dev

How to change cell value using Worksheet_change event without triggering a second call

From Dev

Excel VBA to select a range based on cell value

From Dev

Select a cell value from a range based on criteria

From Dev

How to select the last cell in a range in Excel

From Dev

When finished "looping" Select first cell in range

From Dev

Select a specific cell within the range of columns

From Dev

How to select the last cell in a range in Excel

From Dev

Combining Select values from MYSQL then refining that fall within a range

From Dev

How to select the cell when case is true

From Dev

Worksheet_change not working when cell content changes via VBA but does manually

From Dev

Worksheet_change event handler changes a cell; how to stop it triggering itself?

From Dev

Worksheet_change event handler changes a cell; how to stop it triggering itself?

From Dev

Worksheet_change not working when cell content changes via VBA but does manually

From Dev

Difficulty merging 2 x Private Sub Worksheet_Change (ByVal Target As Range) in one Excel sheet

Related Related

  1. 1

    Exclude Cell Range from Worksheet_Change function

  2. 2

    Combining 2 "Private Sub Worksheet_Change(ByVal Target As Range)" into 1

  3. 3

    Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

  4. 4

    Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

  5. 5

    Combining multiple macros (worksheet_change)

  6. 6

    Combining multiple Worksheet_Change macros

  7. 7

    Worksheet_Change setting target range is slow

  8. 8

    Expanding Worksheet_Change to Numerous Range Values

  9. 9

    create new worksheet after cell (in a range) change

  10. 10

    Excel Macro, Combining two Private Sub worksheet_change

  11. 11

    Excel VBA combining Worksheet_Change codes for 2 target addresses

  12. 12

    How to manually modify a cell that is being edited in a worksheet_change event?

  13. 13

    Multiple worksheet_change with same macro with different reference cell

  14. 14

    Worksheet_Change(ByVal Target As Range), Target always equals Nothing

  15. 15

    Excel VBA Worksheet change, entering a range instead of one cell

  16. 16

    How to change cell value using Worksheet_change event without triggering a second call

  17. 17

    Excel VBA to select a range based on cell value

  18. 18

    Select a cell value from a range based on criteria

  19. 19

    How to select the last cell in a range in Excel

  20. 20

    When finished "looping" Select first cell in range

  21. 21

    Select a specific cell within the range of columns

  22. 22

    How to select the last cell in a range in Excel

  23. 23

    Combining Select values from MYSQL then refining that fall within a range

  24. 24

    How to select the cell when case is true

  25. 25

    Worksheet_change not working when cell content changes via VBA but does manually

  26. 26

    Worksheet_change event handler changes a cell; how to stop it triggering itself?

  27. 27

    Worksheet_change event handler changes a cell; how to stop it triggering itself?

  28. 28

    Worksheet_change not working when cell content changes via VBA but does manually

  29. 29

    Difficulty merging 2 x Private Sub Worksheet_Change (ByVal Target As Range) in one Excel sheet

HotTag

Archive