VBA pop up using multiple ranges/rows

Mattl1976

New Member
Joined
Jan 31, 2016
Messages
14
Hi I am currently using vba for a pop up message from column A depending on whether a certain value is shown as per below.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, r As Range
Set A = Range("A:A")
If Intersect(Target, A) Is Nothing Then Exit Sub
For Each r In Target
If r.Value = "2001708" Then
MsgBox "2 required per pump "
ElseIf r.Value = "10804" Then
MsgBox "use 2001708 for 220v pumps "
End If
Next r
End Sub


My question is how do I add another pop up if column B contains the text "rubber .25" with the pop up message saying "dont use rubber hose"

Thanks
 
Just in case, in the Immediate window, type:
VBA Code:
Application.EnableEvents = True
and press Enter. Now try if the code works.

Artik
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Test the code out on a blank workbook. Does that work?
OK so the code works on a blank workbook when I type in the Rubber .25 however in my main spreadhseet the rubber .25 fills in column B as a result of an Xlookup. Does this make a difference?
 
Upvote 0
OK so the code works on a blank workbook when I type in the Rubber .25 however in my main spreadhseet the rubber .25 fills in column B as a result of an Xlookup. Does this make a difference?
Yes, there's a difference in how the code is triggered. With WorksheetChange, it's only triggered on manual change. For formula, you'd need WorksheetCalculate.
VBA Code:
Private Sub Worksheet_Calculate()
    Dim r As Range
    
    On Error GoTo Cleanup
    Application.EnableEvents = False
    
    For Each r In Me.Columns("B").Cells
        If InStr(1, r.Value, "rubber .25", vbTextCompare) > 0 Then
            MsgBox "don't use rubber hose"
        End If
    Next r

Cleanup:
    Application.EnableEvents = True
End Sub
 
Upvote 0
Yes, there's a difference in how the code is triggered. With WorksheetChange, it's only triggered on manual change. For formula, you'd need WorksheetCalculate.
VBA Code:
Private Sub Worksheet_Calculate()
    Dim r As Range
   
    On Error GoTo Cleanup
    Application.EnableEvents = False
   
    For Each r In Me.Columns("B").Cells
        If InStr(1, r.Value, "rubber .25", vbTextCompare) > 0 Then
            MsgBox "don't use rubber hose"
        End If
    Next r

Cleanup:
    Application.EnableEvents = True
End Sub
ok so do I put this as a new code or in the middle of the original code you wrote above?
 
Upvote 0
Break it up like this.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Range
   
    On Error GoTo Cleanup
    Application.EnableEvents = False
   
    If Not Intersect(Target, Me.Columns("A")) Is Nothing Then
        For Each r In Intersect(Target, Me.Columns("A"))
            Select Case r.Value
                Case "2001708"
                    MsgBox "2 required per pump"
                Case "10804"
                    MsgBox "use 2001708 for 220v pumps"
            End Select
        Next r
    End If

Cleanup:
    Application.EnableEvents = True
End Sub

Private Sub Worksheet_Calculate()
    Dim r As Range
    Dim lastRow As Long
    
    On Error GoTo Cleanup
    Application.EnableEvents = False
    
    ' Find the last used row in column B
    lastRow = Me.Cells(Me.Rows.Count, "B").End(xlUp).Row
    
    ' Loop through each cell in column B up to the last used row
    For Each r In Me.Range("B1:B" & lastRow)
        If InStr(1, r.Value, "rubber .25", vbTextCompare) > 0 Then
            MsgBox "don't use rubber hose"
        End If
    Next r

Cleanup:
    Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
Break it up like this.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Range
  
    On Error GoTo Cleanup
    Application.EnableEvents = False
  
    If Not Intersect(Target, Me.Columns("A")) Is Nothing Then
        For Each r In Intersect(Target, Me.Columns("A"))
            Select Case r.Value
                Case "2001708"
                    MsgBox "2 required per pump"
                Case "10804"
                    MsgBox "use 2001708 for 220v pumps"
            End Select
        Next r
    End If

Cleanup:
    Application.EnableEvents = True
End Sub

Private Sub Worksheet_Calculate()
    Dim r As Range
    Dim lastRow As Long
   
    On Error GoTo Cleanup
    Application.EnableEvents = False
   
    ' Find the last used row in column B
    lastRow = Me.Cells(Me.Rows.Count, "B").End(xlUp).Row
   
    ' Loop through each cell in column B up to the last used row
    For Each r In Me.Range("B1:B" & lastRow)
        If InStr(1, r.Value, "rubber .25", vbTextCompare) > 0 Then
            MsgBox "don't use rubber hose"
        End If
    Next r

Cleanup:
    Application.EnableEvents = True
End Sub
I copied and uploaded as the above.

As per below the 10804 and 2001708 pop ups work byt the top line does not pick up the rubber .25 pop up.
1716248522663.png
 
Upvote 0
It works fine for me when I change the value in A1 from 2 to 1. Again, test on blank workbook.
Book1
ABCDEF
11hose and rubber .251hose and rubber .25
22nothing
3
4
Master
Cell Formulas
RangeFormula
B1B1=XLOOKUP(A1,D1:D2,E1:E2)

Screen Shot 2024-05-20 at 7.19.23 PM.png
 
Upvote 0
Try running this macro first then test again.
VBA Code:
Sub t()
    Application.EnableEvents = True
End Sub

If it still doesn't work I don't know why. You might need to upload your sheet onto a DropBox and upload it here.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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