Message Box after a condition is met

Akw47

Board Regular
Joined
Nov 6, 2020
Messages
90
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello so this is the code I currently have
Private Sub TextBox1_Change()
With ThisWorkbook.ActiveSheet
With .Range("D1", .Cells(.rows.Count, "D").End(xlUp))
If InStr(UCase(Me.TextBox1.Value), "/" Or ",") > 0 Then
MsgBox("Please confirm", vbYesNo, "Double Check") = vbAbort
End If
End Sub


Could someone help me edit it such that, in column D, if the cell has a "/" or "," the message box will pop out. If not nothing will happen thanks.
I will be typing something in the textbox eg. 12345. If it match and there is a "/" or "," with that number in the cell, the above MsgBox should be activated.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try it this way

VBA Code:
Private Sub TextBox1_Change()
With ThisWorkbook.ActiveSheet
With .Range("D1", .Cells(.Rows.Count, "D").End(xlUp))
If InStr(UCase(Me.TextBox1.Value), "/") > 0 Or InStr(UCase(Me.TextBox1.Value), ",") > 0 Then
MsgBox("Please confirm", vbYesNo, "Double Check") = vbAbort
End If
End Sub
 
Upvote 0
Try it this way

VBA Code:
Private Sub TextBox1_Change()
With ThisWorkbook.ActiveSheet
With .Range("D1", .Cells(.Rows.Count, "D").End(xlUp))
If InStr(UCase(Me.TextBox1.Value), "/") > 0 Or InStr(UCase(Me.TextBox1.Value), ",") > 0 Then
MsgBox("Please confirm", vbYesNo, "Double Check") = vbAbort
End If
End Sub
There seems to be a compile error in the code above :(
 
Upvote 0
Let's try removing the Ucase command....

VBA Code:
Private Sub TextBox1_Change()
With ThisWorkbook.ActiveSheet
With .Range("D1", .Cells(.Rows.Count, "D").End(xlUp))
If InStr(Me.TextBox1.Value, "/") > 0 Or InStr(Me.TextBox1.Value, ",") > 0 Then
    MsgBox("Please confirm", vbYesNo, "Double Check") = vbAbort
End If
End Sub
 
Upvote 0
Due to the "= vbAbort" part VBE refuses to compile, so drop it ...
Rich (BB code):
MsgBox("Please confirm", vbYesNo, "Double Check") = vbAbort
 
Upvote 0
Due to the "= vbAbort" part VBE refuses to compile, so drop it ...
Rich (BB code):
MsgBox("Please confirm", vbYesNo, "Double Check") = vbAbort
Hi, now there seems to be a syntax error in the code.
 
Upvote 0
I actually kind of solved the error. But now my code is such that I need to type "12345 ," in textbox.1 for the MsgBox to pop out.
Instead I want to be typing 12345, if there is a "," or "/" in cell. MsgBox will pop out.

VBA Code:
Private Sub TextBox1_Change()
Dim m As String

With ThisWorkbook.ActiveSheet
With .Range("D1", .Cells(.rows.Count, "D").End(xlUp))
If (InStr(Me.TextBox1.Value, "/") > 0 Or InStr(Me.TextBox1.Value, ",") > 0) Then
m = MsgBox("Please confirm", vbYesNo, "Double Check")
        End If
    End With
End With
End Sub
 
Upvote 0
I'm stumped, too. Unless there is a space with the comma in the code, it doesn't make sense
 
Upvote 0
I feel like you're looking for something like this ...

VBA Code:
Private Sub TextBox1_Change()
    Dim c As Range
    With ThisWorkbook.ActiveSheet
        For Each c In .Range("D1", .Cells(.Rows.Count, "D").End(xlUp))
            If InStr(c, "/") > 0 Or InStr(c, ",") > 0 Then
                If MsgBox("Please confirm cell " & c.Address, vbYesNo, "Double Check") = vbNo Then Exit Sub
            End If
        Next c
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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