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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,215,125
Messages
6,123,193
Members
449,090
Latest member
bes000

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