Msgbox yes no not working correctly for me

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Hi,

Confused myself with these Yes No msgboxes.
Code in use shown below

This is how it should work,

I type a value in TextBox1 & results shown in Listbox1.
I make a listbox selection
I see the msgbox Are you sre you wish to delete customer ?
Selecting No should just stop & close down the userform
Selecting Yes should delete that customers row on my worksheet

Selecting No goes straight to msgbox Delete another customer ?




Rich (BB code):
Private Sub ListBox1_Click()
  Dim answer As Integer
  
  Range("A" & ListBox1.List(ListBox1.ListIndex, 1)).Select
  
   Dim c As Range
   With Sheets("DATABASE")
    Set c = .Range("A:A").Find(What:=ListBox1.Value, _
                        After:=.Range("A5"), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
End With
  If Not c Is Nothing Then
    If MsgBox("ARE YOU SURE YOU WISH TO DELETE CUSTOMER " & ListBox1.Text & "?", vbYesNo + vbInformation, "DELETE CUSTOMER FROM DATABASE") = vbYes Then
        Rows(c.Row).EntireRow.Delete
    
        MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " HAS NOW BEEN DELETED", vbInformation, "CUSTOMER DELETED MESSAGE"
    End If
    
    answer = MsgBox("DELETE ANOTHER CUSTOMER ?", vbYesNo + vbQuestion)
    If answer = vbYes Then
          TextBox1.Value = ""
    End If
        
    Else
        MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " WAS NOT DELETED", vbInformation, "CUSTOMER WAS NOT DELETED MEAASGE"
        Range("A6").Select
    End If
    
    Set c = Nothing
    
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Selecting No should just stop & close down the userform
Why do you think this code would make that happen?

Your If checks if the answer is yes, and the action is limited to deleting a row and giving a notification. After that End If, the code just keeps going to whatever is next. In this case you ask they want to delete another customer.

Based on your question, I think your If structure does not reflect what you actually want to do. I have an idea of how to restructure this but I don't want to cause even more confusion by solving the wrong problem. Please give a more complete description of the logic you want to follow when this listbox is clicked and we can revise the code. For example, no idea what you are doing with TextBox1.

Also your code indentation is incorrect and may also be causing some confusion about the If structure. I have shown a correctly indented copy of the same code below.

Also there is nothing at all in this code anywhere that closes the UserForm.

Here is your code unchanged except for indentation and some commentary.
VBA Code:
Private Sub ListBox1_Click()

   Dim answer As Integer
  
   Range("A" & ListBox1.List(ListBox1.ListIndex, 1)).Select
  
   Dim c As Range
  
   With Sheets("DATABASE")
      Set c = .Range("A:A").Find(What:=ListBox1.Value, _
                                 After:=.Range("A5"), _
                                 LookIn:=xlValues, _
                                 LookAt:=xlWhole, _
                                 SearchOrder:=xlByRows, _
                                 SearchDirection:=xlNext, _
                                 MatchCase:=False)
   End With
  
   If Not c Is Nothing Then
      If MsgBox("ARE YOU SURE YOU WISH TO DELETE CUSTOMER " & ListBox1.Text & "?", vbYesNo + vbInformation, "DELETE CUSTOMER FROM DATABASE") = vbYes Then
         Rows(c.Row).EntireRow.Delete
        
         MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " HAS NOW BEEN DELETED", vbInformation, "CUSTOMER DELETED MESSAGE"
      End If
      ''''' code continues here no matter whether user clicked Yes or No                                    ''''''''
      ''''' if you want this to only happen on Yes then this code must be moved up into the prior If/End If ''''''''
      answer = MsgBox("DELETE ANOTHER CUSTOMER ?", vbYesNo + vbQuestion)
      If answer = vbYes Then
         TextBox1.Value = ""
      End If
      '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  
   Else
      MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " WAS NOT DELETED", vbInformation, "CUSTOMER WAS NOT DELETED MEAASGE"
      Range("A6").Select
   End If
  
   Set c = Nothing
   
End Sub
 
Upvote 0
OK this is what im looking for.

MsgBox ARE YOU SURE YOU WISH TO DELETE CUSTOMER
No would just Unload userform & Msgbox CUSTOMER WAS NOT DELETED MESSAGE
Yes would delete customers row on worksheet.

If Yes was selected & customers info deleted then show
MsgBox DELETE ANOTHER CUSTOMER
No would just Unload userform
Yes would clear TextBox1 & ListBox1

The code i have in use runs out of synce.
 
Upvote 0
This is the code i have in use now & pretty much done all part from the below issue

DELETE ANOTHER CUSTOMER MESSAGE
I select NO & the userform closes, this is correct
BUT

I select YES & the userform close, this is incorrect
When i select YES Textbox1 & Listbox1 should clear

Rich (BB code):
Private Sub ListBox1_Click()
  Dim answer As Integer
  
  Range("A" & ListBox1.List(ListBox1.ListIndex, 1)).Select
  
   Dim c As Range
   With Sheets("DATABASE")
    Set c = .Range("A:A").Find(What:=ListBox1.Value, _
                        After:=.Range("A5"), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
End With
  If Not c Is Nothing Then
    If MsgBox("ARE YOU SURE YOU WISH TO DELETE CUSTOMER " & ListBox1.Text & "?", vbYesNo + vbInformation, "DELETE CUSTOMER FROM DATABASE") = vbYes Then
        Rows(c.Row).EntireRow.Delete
    
        MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " HAS NOW BEEN DELETED", vbInformation, "CUSTOMER DELETED MESSAGE"
        
        MsgBox "DELETE ANOTHER CUSTOMER ?", vbYesNo + vbQuestion
        If vbNo Then
        Unload DatabaseDeleteCustomer

        Else
             TextBox1.Value = ""
             TextBox1.SetFocus
           
    End If
    Else
        MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " WAS NOT DELETED", vbInformation, "CUSTOMER WAS NOT DELETED MESSAGE"
        Unload DatabaseDeleteCustomer
        Range("A6").Select
    
    End If
    

    
    Set c = Nothing
    End If
End Sub
 
Upvote 0
This worked for me


Rich (BB code):
Private Sub ListBox1_Click()
  Dim answer As Integer
  
  Range("A" & ListBox1.List(ListBox1.ListIndex, 1)).Select
  
   Dim c As Range
   With Sheets("DATABASE")
    Set c = .Range("A:A").Find(What:=ListBox1.Value, _
                        After:=.Range("A5"), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
End With
  If Not c Is Nothing Then
    If MsgBox("ARE YOU SURE YOU WISH TO DELETE CUSTOMER " & ListBox1.Text & "?", vbYesNo + vbInformation, "DELETE CUSTOMER FROM DATABASE") = vbYes Then
        Rows(c.Row).EntireRow.Delete
    
        MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " HAS NOW BEEN DELETED", vbInformation, "CUSTOMER DELETED MESSAGE"
        
    answer = MsgBox("DELETE ANOTHER CUSTOMER ?", vbYesNo + vbQuestion)
    If answer = vbNo Then
        Unload DatabaseDeleteCustomer

        ElseIf answer = vbYes Then
             TextBox1.Value = ""
             TextBox1.SetFocus
           
    End If
    
    Else
        MsgBox "THE CUSTOMER " & Me.ListBox1.Value & " WAS NOT DELETED", vbInformation, "CUSTOMER WAS NOT DELETED MESSAGE"
        Unload DatabaseDeleteCustomer
        Range("A6").Select
    
    End If
    

    
    Set c = Nothing
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,846
Members
449,194
Latest member
HellScout

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