stop writting to sheet after click on No on msgbox

DB73

Board Regular
Joined
Jun 7, 2022
Messages
102
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2010
  6. 2007
Platform
  1. Windows
  2. Mobile
  3. Web
i'm trying to figure out to prevent writing to my sheet after vbNo is choosen
if i click No, it still writes to my sheet, but i want to return to my userform to ajust the values i filled in on my userform

any help would be greatfull...

VBA Code:
Private Sub CommandButton5_Click()

    answer = MsgBox("Alles juis ingevuld", vbQuestion + vbYesNo) = vbYes
        If vbYes Then
   
                            'Datum
                                 Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = CDate(TheDate.Value)
                             'ziek/werkdag/feestdag
                                 Range("M" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox1.Text
                             'project
                                 Range("N" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox2.Text
                             'werk locatie
                                 Range("Q" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox3.Text
                             'normale of over uren
                                 Range("R" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox4.Text
                             'uur tarief percentage
                                 Range("S" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox5.Text
                             'starttijd
                                 Range("T" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox6.Text
                             'eindtijd
                                 Range("U" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox7.Text
                             'pauze
                                 Range("V" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox8.Text
                             'BTW verlegd ja/nee
                                 Range("Y" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox9.Text
                             'BTW heffing
                                 Range("Z" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox10.Text
                             'Rit van
                                 Range("AD" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox11.Text
                             'Rit naar
                                 Range("AH" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox12.Text
                             'Enkele reis of retour rit
                                 Range("AL" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox13.Text
                             'rede rit
                                 Range("AN" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox14.Text
                             'declaratie bedrag pkm zakelijk
                                 Range("AO" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox15.Text
                             'overige declaratie
                                 Range("AR" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox16.Text
                             'reden overige declaratie
                                 Range("AS" & Rows.Count).End(xlUp).Offset(0, 0).Value = ComboBox17.Text
                        Exit Sub
                    End If
                answer = MsgBox("Alles juis ingevuld", vbQuestion + vbYesNo) = vbYes
             If vbYes Then
        MyForm.Show
    End If
End Sub
 
Your placement of the 2nd question does not make logical sense to me, nor does your placement of "Exit sub" just before "End Sub"

VBA Code:
    Exit Sub
End Sub

Something like this would make more sense.

VBA Code:
Private Sub CommandButton5_Click()
    Dim answer As Integer

    answer = MsgBox("Alles juist ingevuld?", vbQuestion + vbYesNo)    'Everything filled in correctly?
    If answer = vbYes Then
        '
        'Your write code here (temporarily removed for clarity)
        '
        Unload Me
        answer = MsgBox("Meer items toevoegen?", vbQuestion + vbYesNo)    'Add more items?
        If answer = vbYes Then
            MyForm.Show
        End If
    End If
End Sub
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Your placement of the 2nd question does not make logical sense to me, nor does your placement of "Exit sub" just before "End Sub"

VBA Code:
    Exit Sub
End Sub

Something like this would make more sense.

VBA Code:
Private Sub CommandButton5_Click()
    Dim answer As Integer

    answer = MsgBox("Alles juist ingevuld?", vbQuestion + vbYesNo)    'Everything filled in correctly?
    If answer = vbYes Then
        '
        'Your write code here (temporarily removed for clarity)
        '
        Unload Me
        answer = MsgBox("Meer items toevoegen?", vbQuestion + vbYesNo)    'Add more items?
        If answer = vbYes Then
            MyForm.Show
        End If
    End If
End Sub
u were right about the "Your placement of the 2nd question does not make logical sense to me, nor does your placement of "Exit sub" just before "End Sub"
i removed that.
think it was because of copy paste 🤦‍♂️
and about the "Your placement of the 2nd question does not make logical sense to me, nor does your placement of "Exit sub" just before "End Sub",
it was more like, i do not want to click my "cmdbttn5"....because it asks if i want to add another item.
i know that my 2nd mgsbox, i added, isn't really necessary, because after closing my form i could add another one by clicking that cmdbttn5

but it works.

thanks for the help anyway
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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