VBA Code Error

PHIL.Pearce84

Board Regular
Joined
May 16, 2011
Messages
152
Office Version
  1. 365
Platform
  1. Windows
Evening,

I've been creating a VBA form to add data to a sheet, it was working until I added in the OptionButton part to populate "Provided" and"Reconciled" in column F and now I get a compile error that says:

"Block if without End If"

But I cant work it out, can anyone help please?


Private Sub CommandButton1_Click()

'''''''''' Validation ''''''''''

If Me.TextBox1.Value = "" Then
MsgBox "Please Enter a Valid Forename"
Exit Sub
End If

If Me.TextBox2.Value = "" Then
MsgBox "Please Enter a Valid Surname"
Exit Sub
End If

If Me.TextBox3.Value = "" Then
MsgBox "Please Enter a Address"
Exit Sub
End If

If VBA.IsNumeric(Me.TextBox4.Value) = False Then
MsgBox "Please Enter a Valid Amount"
Exit Sub

If Me.OptionButton1.Value = False And Me.OptionButton2.Value = False Then
MsgBox "Please valid status"
Exit Sub

End If

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("sheet3")
Dim n As Long

n = sh.Range("A" & Application.Rows.Count).End(xlUp).Row

sh.Unprotect "1234"

sh.Range("A" & n + 1).Value = Me.TextBox1.Value
sh.Range("B" & n + 1).Value = Me.TextBox2.Value
sh.Range("C" & n + 1).Value = Me.TextBox3.Value
sh.Range("D" & n + 1).Value = Me.TextBox4.Value
If Me.OptionButton1.Value = True Then sh.Range("E" & n + 1).Value = "Provided"
If Me.OptionButton2.Value = True Then sh.Range("E" & n + 1).Value = "Reconciled"

sh.Protect "1234"

Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""

Me.OptionButton1.Value = False
Me.OptionButton2.Value = False

MsgBox "New Record Has Been Added", vbInformation

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Evening,

I've been creating a VBA form to add data to a sheet, it was working until I added in the OptionButton part to populate "Provided" and"Reconciled" in column F and now I get a compile error that says:

"Block if without End If"

But I cant work it out, can anyone help please?


Private Sub CommandButton1_Click()

'''''''''' Validation ''''''''''

If Me.TextBox1.Value = "" Then
MsgBox "Please Enter a Valid Forename"
Exit Sub
End If

If Me.TextBox2.Value = "" Then
MsgBox "Please Enter a Valid Surname"
Exit Sub
End If

If Me.TextBox3.Value = "" Then
MsgBox "Please Enter a Address"
Exit Sub
End If

If VBA.IsNumeric(Me.TextBox4.Value) = False Then
MsgBox "Please Enter a Valid Amount"
Exit Sub

If Me.OptionButton1.Value = False And Me.OptionButton2.Value = False Then
MsgBox "Please valid status"
Exit Sub

End If

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("sheet3")
Dim n As Long

n = sh.Range("A" & Application.Rows.Count).End(xlUp).Row

sh.Unprotect "1234"

sh.Range("A" & n + 1).Value = Me.TextBox1.Value
sh.Range("B" & n + 1).Value = Me.TextBox2.Value
sh.Range("C" & n + 1).Value = Me.TextBox3.Value
sh.Range("D" & n + 1).Value = Me.TextBox4.Value
If Me.OptionButton1.Value = True Then sh.Range("E" & n + 1).Value = "Provided"
If Me.OptionButton2.Value = True Then sh.Range("E" & n + 1).Value = "Reconciled"

sh.Protect "1234"

Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""

Me.OptionButton1.Value = False
Me.OptionButton2.Value = False

MsgBox "New Record Has Been Added", vbInformation

End Sub
See the highlighted section, missing "End If".
 
Upvote 0

Forum statistics

Threads
1,224,851
Messages
6,181,395
Members
453,034
Latest member
mikdadhussain

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