Hi everyone,
I have the following code in the After Update event of my combo box and the On Current event of my main form.
If I try to open the main form, I get an error message that reads: "Compile error, Block If without End If" (it also highlights the End Sub in blue). If I take the code out of the main form, I still get the same message after an update to my combo box. What have I done wrong?
Here is my code:
Thanks in advanceCode:Private Sub cboresponsetype_AfterUpdate() If Me.cboresponsetype = "A" Then Me.sbfAAdd.Visible = True Me.sbfBAdd.Visible = False Me.sbfCAdd.Visible = False Else If Me.cboresponsetype = "B" Then Me.sbfAAdd.Visible = False Me.sbfBAdd.Visible = True Me.sbfCAdd.Visible = False Else If Me.cboresponsetype = "C" Then Me.sbfAAdd.Visible = False Me.sbfBAdd.Visible = False Me.sbfCAdd.Visible = True End If End Sub
ElseIf rather than Else <BR> If
Code:Private Sub cboresponsetype_AfterUpdate() If Me.cboresponsetype = "A" Then Me.sbfAAdd.Visible = True Me.sbfBAdd.Visible = False Me.sbfCAdd.Visible = False ElseIf Me.cboresponsetype = "B" Then Me.sbfAAdd.Visible = False Me.sbfBAdd.Visible = True Me.sbfCAdd.Visible = False ElseIf Me.cboresponsetype = "C" Then Me.sbfAAdd.Visible = False Me.sbfBAdd.Visible = False Me.sbfCAdd.Visible = True End If End Sub
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
Hi DonkeyOte,
Thanks for your reply but It is showing me syntax error.why this happens?
It is occuring because you have 3 If statements and only 1 end if statement
You need an End if for every If statement
By using ElseIf it continues instead of If you are continuing the command from the 1st If statement and not starting 2nd & 3rd If statements - This is what DonkeyOte is showing you in the code he posted
You do not need to use the Else statement
As you are testing the same item for multiple results, it is more efficent to use Select Case
Code:Private Sub cboresponsetype_AfterUpdate() Select Case Me.cboresponsetype Case "A" Me.sbfAAdd.Visible = True Me.sbfBAdd.Visible = False Me.sbfCAdd.Visible = False Case "B" Me.sbfAAdd.Visible = False Me.sbfBAdd.Visible = True Me.sbfCAdd.Visible = False Case "C" Me.sbfAAdd.Visible = False Me.sbfBAdd.Visible = False Me.sbfCAdd.Visible = True End Select End Sub
Please Read Forum Rules Before Posting
Wrap VBA code by selecting the code and clicking the # icon or Read This
How To Cross Post politely
Top Excel links for beginners to Experts
If you are pleased with a member's answer then use the Scales icon to rate it
If my reply has assistedor failed to assist you
I welcome your Feedback.
I think you could probably go one step further...
Code:Private Sub cboresponsetype_AfterUpdate() Me.sbfAAdd.Visible = Me.cboresponsetype = "A" Me.sbfBAdd.Visible = Me.cboresponsetype = "B" Me.sbfCAdd.Visible = Me.cboresponsetype = "C" End Sub
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks