Correct sytax for yes / no Msgbox

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
On my userform i have a command button that will place a value in the comboBox field if its not in the list.
Once done the user will complete the other fields & then use another command button to send all the values to my worksheet.
Im interested to put the two codes together.

It would work like this.
Use command button to add value if not in list.
Complete all the other fields.
Run command button that sends values to worksheet.
MsgBox Was the value in the lisr ?
Yes then run code example 1 below.
No then run code example 2 below.
The userform then closes.



CODE 1
VBA Code:
Private Sub SendToWorksheet_Click()
       
         ActiveSheet.ListObjects("Table42").ListRows.Add 1, True
         ActiveSheet.ListObjects("Table42").DataBodyRange.RowHeight = 25
         ThisWorkbook.Worksheets("QUOTES").Range("D2") = Me.ComboBox1.Text ' VEHICLE
         ThisWorkbook.Worksheets("QUOTES").Range("H2") = Me.ComboBox2.Text ' DESCRIPTION OF JOB
         ThisWorkbook.Worksheets("QUOTES").Range("K2") = Me.ComboBox3.Text ' PAYMENT
         ThisWorkbook.Worksheets("QUOTES").Range("I2") = Me.ComboBox4.Text ' MILEAGE THERE & BACK
         ThisWorkbook.Worksheets("QUOTES").Range("A2") = Me.TextBox1.Text ' NAME
         ThisWorkbook.Worksheets("QUOTES").Range("B2") = Me.TextBox2.Text ' TELEPHONE
         ThisWorkbook.Worksheets("QUOTES").Range("C2") = Me.TextBox3.Text ' POST CODE
         ThisWorkbook.Worksheets("QUOTES").Range("E2") = Me.TextBox4.Text ' VEHICLE REG
         ThisWorkbook.Worksheets("QUOTES").Range("F2") = Me.TextBox5.Text ' QUOTED
         ThisWorkbook.Worksheets("QUOTES").Range("G2") = Me.TextBox6.Text ' DATE OF QUOTE
         ThisWorkbook.Worksheets("QUOTES").Range("J2") = Me.TextBox8.Text ' VIN
         Unload QuotesForm
         Range("A1").Select
         ActiveWorkbook.Save
      Exit Sub
End Sub

CODE 2
Code:
Private Sub AddNewVehicle_Click()
      Dim LastBlankRow As Long
      Sheets("INFO").Select
      LastBlankRow = Cells(Rows.count, 2).End(xlUp).Row + 1
      Cells(LastBlankRow, 2).Select
      ActiveCell.Value = ComboBox1.Value
     
      Sheets("QUOTES").Select
End Sub


My problem is the following im being asked twice the same Msg question. when using the following & selecting NO to Msg question

Code:
Private Sub SendToWorksheet_Click()
       
      Dim answer As Integer
          answer = MsgBox("WAS THE CAR IN THE VEHICLE LIST", vbCritical + vbYesNo + vbDefaultButton2, "ADD NEW CAR MESSAGE")
      If answer = vbYes Then
         ActiveSheet.ListObjects("Table42").ListRows.Add 1, True
         ActiveSheet.ListObjects("Table42").DataBodyRange.RowHeight = 25
         ThisWorkbook.Worksheets("QUOTES").Range("D2") = Me.ComboBox1.Text ' VEHICLE
         ThisWorkbook.Worksheets("QUOTES").Range("H2") = Me.ComboBox2.Text ' DESCRIPTION OF JOB
         ThisWorkbook.Worksheets("QUOTES").Range("K2") = Me.ComboBox3.Text ' PAYMENT
         ThisWorkbook.Worksheets("QUOTES").Range("I2") = Me.ComboBox4.Text ' MILEAGE THERE & BACK
         ThisWorkbook.Worksheets("QUOTES").Range("A2") = Me.TextBox1.Text ' NAME
         ThisWorkbook.Worksheets("QUOTES").Range("B2") = Me.TextBox2.Text ' TELEPHONE
         ThisWorkbook.Worksheets("QUOTES").Range("C2") = Me.TextBox3.Text ' POST CODE
         ThisWorkbook.Worksheets("QUOTES").Range("E2") = Me.TextBox4.Text ' VEHICLE REG
         ThisWorkbook.Worksheets("QUOTES").Range("F2") = Me.TextBox5.Text ' QUOTED
         ThisWorkbook.Worksheets("QUOTES").Range("G2") = Me.TextBox6.Text ' DATE OF QUOTE
         ThisWorkbook.Worksheets("QUOTES").Range("J2") = Me.TextBox8.Text ' VIN
 
         Unload QuotesForm
         Range("A1").Select
         ActiveWorkbook.Save
      Else
      Dim LastBlankRow As Long
         Sheets("INFO").Select
         LastBlankRow = Cells(Rows.count, 2).End(xlUp).Row + 1
         Cells(LastBlankRow, 2).Select
         ActiveCell.Value = ComboBox1.Value
         Sheets("QUOTES").Select
       
         answer = MsgBox("WAS THE CAR IN THE VEHICLE LIST", vbCritical + vbYesNo + vbDefaultButton2, "ADD NEW CAR MESSAGE")
      If answer = vbYes Then
         ActiveSheet.ListObjects("Table42").ListRows.Add 1, True
         ActiveSheet.ListObjects("Table42").DataBodyRange.RowHeight = 25
         ThisWorkbook.Worksheets("QUOTES").Range("D2") = Me.ComboBox1.Text ' VEHICLE
         ThisWorkbook.Worksheets("QUOTES").Range("H2") = Me.ComboBox2.Text ' DESCRIPTION OF JOB
         ThisWorkbook.Worksheets("QUOTES").Range("K2") = Me.ComboBox3.Text ' PAYMENT
         ThisWorkbook.Worksheets("QUOTES").Range("I2") = Me.ComboBox4.Text ' MILEAGE THERE & BACK
         ThisWorkbook.Worksheets("QUOTES").Range("A2") = Me.TextBox1.Text ' NAME
         ThisWorkbook.Worksheets("QUOTES").Range("B2") = Me.TextBox2.Text ' TELEPHONE
         ThisWorkbook.Worksheets("QUOTES").Range("C2") = Me.TextBox3.Text ' POST CODE
         ThisWorkbook.Worksheets("QUOTES").Range("E2") = Me.TextBox4.Text ' VEHICLE REG
         ThisWorkbook.Worksheets("QUOTES").Range("F2") = Me.TextBox5.Text ' QUOTED
         ThisWorkbook.Worksheets("QUOTES").Range("G2") = Me.TextBox6.Text ' DATE OF QUOTE
         ThisWorkbook.Worksheets("QUOTES").Range("J2") = Me.TextBox8.Text ' VIN
 
         Unload QuotesForm
         Range("A1").Select
         ActiveWorkbook.Save
      End If
      End If
      Exit Sub

End Sub
 
And why do you use:
ThisWorkbook

Unless you have more than one workbook open you do not need this.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,224,833
Messages
6,181,240
Members
453,026
Latest member
cknader

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