why message box shows in all of cases despite contain condition ?

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi
I face problem is not clear for me . I put message in beggining of the code when textbox2 is empty , then pop up message "PLEASEE WRITE THE CODE" and if it's not .should continue the procedure and show the next message "Are you sure you want to delete variation?", vbCritical + vbYesNo , but the first message shows whether textbox2 is empty or not .

VBA Code:
Private Sub CommandButton4_Click()
Dim lReply As Long
Dim ws As Worksheet
Dim strFind As String
If strFind = "" Then MsgBox "PLEASEE WRITE  THE CODE", vbExclamation
Exit Sub
 lReply = MsgBox("Are you sure you want to delete variation?", vbCritical + vbYesNo)
 If lReply = vbNo Then Exit Sub
 Set ws = Worksheets("sheet1")
 strFind = Me.TextBox2.Value
 
 With ws.UsedRange.Columns(2)
 If WorksheetFunction.CountIf(.Cells, strFind) <> 0 Then
 .Cells.Find(What:=strFind, After:=.Cells(1, 1), MatchCase:=True).EntireRow.Delete
 Else
 MsgBox "Could not find " & strFind & " on " & ws.Name, vbCritical
 Exit Sub
 End If
 End With
 
 
 strFind = "VO" & strFind
 On Error Resume Next
 Application.DisplayAlerts = False
 Sheets(strFind).Delete
 Application.DisplayAlerts = True
 On Error GoTo 0
 
 'Close Form
 Unload Me
End Sub
any help to fix it , please ?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Replace this part ...
VBA Code:
Dim strFind As String
If strFind = "" Then MsgBox "PLEASEE WRITE  THE CODE", vbExclamation

with this part:
VBA Code:
Dim strFind As String
strFind = Me.TextBox2.Value
If strFind = "" Then MsgBox "PLEASEE WRITE  THE CODE", vbExclamation
 
Upvote 0
then you should remove the next Exit Sub
Rich (BB code):
Private Sub CommandButton4_Click()
Dim lReply As Long
Dim ws As Worksheet
Dim strFind As String
strFind = Me.TextBox2.Value
If strFind = "" Then MsgBox "PLEASEE WRITE  THE CODE", vbExclamation
Exit Sub
 lReply = MsgBox("Are you sure you want to delete variation?", vbCritical + vbYesNo)
 
Upvote 0
hi
I face problem is not clear for me . I put message in beggining of the code when textbox2 is empty , then pop up message "PLEASEE WRITE THE CODE" and if it's not but the first message shows whether textbox2 is empty or not .


any help to fix it , please ?

Hi,
try this update to your code & see if does what you want

VBA Code:
Private Sub CommandButton4_Click()
    Dim lReply      As VbMsgBoxResult
    Dim ws          As Worksheet
    Dim strFind     As String
    
    strFind = Me.TextBox2.Value
    
    If Len(strFind) = 0 Then
        MsgBox "PLEASE WRITE  THE CODE", vbExclamation, "Entry Required"
        Me.TextBox2.SetFocus
        Exit Sub
    Else
        lReply = MsgBox("Are you sure you want To delete variation?", vbCritical + vbYesNo, "Confirm")
        If lReply = vbNo Then Exit Sub
    End If
    
    Set ws = Worksheets("sheet1")
    
    With ws.UsedRange.Columns(2)
        If WorksheetFunction.CountIf(.Cells, strFind) <> 0 Then
            .Cells.Find(What:=strFind, After:=.Cells(1, 1), MatchCase:=True).EntireRow.Delete
        Else
            MsgBox "Could Not find " & strFind & " On " & ws.Name, vbCritical, "Not Found"
            Exit Sub
        End If
    End With
    
    strFind = "VO" & strFind
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets(strFind).Delete
    Application.DisplayAlerts = True
    On Error GoTo 0
    
    'Close Form
    Unload Me
End Sub

Dave
 
Upvote 0
Solution
if I delete the exit sub will move to the next message . it doesn't make sense . because when the textbox2 is empty shows the first message and should exit sub (shouldn't happen any thing .it doesn't supposses to move next message) and if the textbox2 is not empty then should show the next message and continue the procedure .
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

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