Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile Error: Block If without End If 3

Status
Not open for further replies.

RP1America

Technical User
Aug 17, 2009
221
US
While using the Compile Project option, I ran into the error: "Compile Error: Block If without End If" with the "End Sub" highlighted.

Any ideas as to why this may be occuring? I cannot figure it out. All other debugging within this procedure has been accounted for.

Thanks!!


Code:
Private Sub beneinfcont_click()

    Dim strperiod As String
    If period = "1" Then strperiod = "One"
    If period = "2" Then strperiod = "Two"
    If period = "3" Then strperiod = "Three"
    If period = "4" Then strperiod = "Four"
    If period = "5" Then strperiod = "Five"
    If period = "6" Then strperiod = "Six"
    If period = "7" Then strperiod = "Seven"
    If period = "8" Then strperiod = "Eight"
    If period = "9" Then strperiod = "Nine"
    If period = "10" Then strperiod = "Ten"
    If period = "11" Then strperiod = "Eleven"
    If period = "12" Then strperiod = "Twelve"
    If period = "13" Then strperiod = "Thirteen"
    If period = "14" Then strperiod = "Fourteen"
    If period = "15" Then strperiod = "Fifteen"
    If period = "16" Then strperiod = "Sixteen"
    If period = "17" Then strperiod = "Seventeen"
    If period = "18" Then strperiod = "Eighteen"
    If period = "19" Then strperiod = "Nineteen"
    If period = "20" Then strperiod = "Twenty"
    If period = "21" Then strperiod = "Twenty-One"
    If period = "22" Then strperiod = "Twenty-Two"
    If period = "23" Then strperiod = "Twenty-Three"
    If period = "24" Then strperiod = "Twenty-Four"
    If period = "25" Then strperiod = "Twenty-Five"
    If period = "26" Then strperiod = "Twenty-Six"
    If period = "27" Then strperiod = "Twenty-Seven"
    If period = "28" Then strperiod = "Twenty-Eight"
    If period = "29" Then strperiod = "Twenty-Nine"
    If period = "30" Then strperiod = "Thirty"
    If period = "31" Then strperiod = "Thirty-One"
    If period = "32" Then strperiod = "Thirty-Two"
    If period = "33" Then strperiod = "Thirty-Three"
    If period = "34" Then strperiod = "Thirty-Four"
    If period = "35" Then strperiod = "Thirty-Five"
    If period = "36" Then strperiod = "Thirty-Six"
    If period = "37" Then strperiod = "Thirty-Seven"
    If period = "38" Then strperiod = "Thirty-Eight"
    If period = "39" Then strperiod = "Thirty-Nine"
    If period = "40" Then strperiod = "Fourty"
    If period = "41" Then strperiod = "Fourty-One"
    If period = "42" Then strperiod = "Fourty-Two"
    If period = "43" Then strperiod = "Fourty-Three"
    If period = "44" Then strperiod = "Fourty-Four"
    If period = "45" Then strperiod = "Fourty-Five"
    If period = "46" Then strperiod = "Fourty-Six"
    If period = "47" Then strperiod = "Fourty-Seven"
    If period = "48" Then strperiod = "Fourty-Eight"
    If period = "49" Then strperiod = "Fourty-Nine"
    If period = "50" Then strperiod = "Fifty"
    If period >= "50" Then strperiod = period
    
    Dim strperiodmys As String
    If annuityinfo.periodmonths = True Then strperiodmys = " months"
    If annuityinfo.periodyears = True Then strperiodmys = " years"
    
    Dim strsex2 As String
    If annuitantinfo.annuitantsal.Value = "Mr." Then strsex2 = "his"
    If annuitantinfo.annuitantsal.Value = "Mrs." Then strsex2 = "her"
    If annuitantinfo.annuitantsal.Value = "Ms." Then strsex2 = "her"
    
    Dim strnumbenes As String
    If Not bene1first.Value = "" And Not bene2first.Value = "" Then strnumbenes = "beneficiaries"
    If Not bene1first.Value = "" And bene2first.Value = "" Then strnumbenes = "beneficiary"
    
    Dim strisare As String
    If Not bene1first.Value = "" And Not bene2first.Value = "" Then strisare = "are"
    If Not bene1first.Value = "" And bene2first.Value = "" Then strisare = "is"
    
    Dim stryou2 As String
    If addresseeinfo.addresseefirst.Value = bene1first.Value And addresseeinfo.addresseelast.Value = bene1last.Value Then
    stryou2 = "you, "
    If addresseeinfo.addresseefirst.Value = Not bene1first.Value And addresseeinfo.addresseelast.Value = Not bene1last.Value Then
    stryou2 = ""
    
    Dim strbenenames As String
    If ssayes = True And Not bene1first.Value = "" And Not bene2first.Value = "" And Not bene3first.Value = "" _
    And Not bene4first.Value = "" And bene5first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value + ", " + bene2first.Value + " " + bene2last.Value _
    + ", " + bene3first.Value + " " + bene3last.Value + ", " + bene4first.Value + " " + bene4last.Value + ", and " + _
    bene5first.Value + " " + bene5last.Value + " to share equally"
    If ssayes = True And Not bene1first.Value = "" And Not bene2first.Value = "" And Not bene3first.Value = "" _
    And Not bene4first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value + ", " + bene2first.Value + " " + bene2last.Value + _
    ", " + bene3first.Value + " " + bene3last.Value + ", and " + bene4first.Value + " " + bene4last.Value + " to share equally"
    If ssayes = True And Not bene1first.Value = "" And Not bene2first.Value = "" And Not bene3first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value + ", " + bene2first.Value + " " + bene2last.Value + _
    ", and " + bene3first.Value + " " + bene3last.Value + " to share equally"
    If ssayes = True And Not bene1first.Value = "" And Not bene2first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value + ", and " + bene2first.Value + " " + bene2last.Value + _
    " to share equally"
    If ssayes = True And Not bene1first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value
    If ssano = True And Not bene1first.Value = "" And Not bene2first.Value = "" And Not bene3first.Value = "" _
    And Not bene4first.Value = "" And bene5first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value + " - " + bene1per.Value + ", " + bene2first.Value + _
    " " + bene2last.Value + " - " + bene2per.Value + ", " + bene3first.Value + " " + bene3last.Value + " - " + bene3per.Value + _
    ", " + bene4first.Value + " " + bene4last.Value + " - " + bene4per.Value + ", and " + bene5first.Value + " " + _
    bene5last.Value + " - " + bene5per.Value
    If ssano = True And Not bene1first.Value = "" And Not bene2first.Value = "" And Not bene3first.Value = "" _
    And Not bene4first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value + " - " + bene1per.Value + ", " + bene2first.Value + _
    " " + bene2last.Value + " - " + bene2per.Value + ", " + bene3first.Value + " " + bene3last.Value + " - " + bene3per.Value + _
    ", and " + bene4first.Value + " " + bene4last.Value + " - " + bene4per.Value
    If ssano = True And Not bene1first.Value = "" And Not bene2first.Value = "" And Not bene3first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value + " - " + bene1per.Value + ", " + bene2first.Value + _
    " " + bene2last.Value + " - " + bene2per.Value + ", and " + bene3first.Value + " " + bene3last.Value + " - " + bene3per.Value
    If ssano = True And Not bene1first.Value = "" And Not bene2first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value + " - " + bene1per.Value + ", and " + bene2first.Value + _
    " " + bene2last.Value + " - " + bene1per.Value
    If ssano = True And Not bene1first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value
    
    Dim strnumregpyts As String
    If annuityinfo.annoption = "Fixed Amount" And annuityinfo.benefityes = True Then
    strnumregpyts = (annuityinfo.numpytsduebene.Value - 1)
    If annuityinfo.annoption = "Installment Refund" And annuityinfo.benefityes = True Then
    strnumregpyts = (annuityinfo.numpytsduebene.Value - 1)
    
    Dim strjscalcpercent As String
    If annuityinfo.jspercent = "50%" Then strjscalcpercent = Format$(annuityinfo.gross * 0.5, "$#,##0.00")
    If annuityinfo.jspercent = "66%" Then strjscalcpercent = Format$(annuityinfo.gross * 0.666, "$#,##0.00")
    If annuityinfo.jspercent = "75%" Then strjscalcpercent = Format$(annuityinfo.gross * 0.75, "$#,##0.00")
    If annuityinfo.jspercent = "100%" Then strjscalcpercent = Format$(annuityinfo.gross * 1, "$#,##0.00")

    With ActiveDocument
    
'   Benefits Due (No Contingent Benefits Available Per Annuity Type)
        If annuityinfo.annoption.Value = "Fixed Period" And annuityinfo.benefityes = True Then
        .Bookmarks("annoptionspecs").Range.Text = LCase(strperiod) + strperiodmys + ".  There are " + _
        annuityinfo.numpytsduebene.Value + " payments left on this annuity to be paid to " + strsex2 + " " + strnumbenes + _
        ", who " + strisare + " " + strbenenames + "."
        ElseIf annuityinfo.annoption.Value = "Fixed Amount" And annuityinfo.benefityes = True Then
        .Bookmarks("annoptionspecs").Range.Text = annuityinfo.instrefnumpyts + " payments and a final payment of " + _
        annuityinfo.instreffinal + ".  There are " + strnumregpyts + " payments of " + annuityinfo.gross + _
        " and a final payment of " + annuityinfo.instreffinal + " left on this annuity to be paid to " + strsex2 + " " + _
        strnumbenes + ", who " + strisare + " " + strbenenames + "."
        ElseIf annuityinfo.annoption.Value = "Certain and Life" And annuityinfo.benefityes = True Then
        .Bookmarks("annoptionspecs").Range.Text = LCase(strperiod) + strperiodmys + " and life thereafter.  There are " + _
        annuityinfo.numpytsduebene.Value + " payments left on this annuity to be paid to " + strsex2 + " " + strnumbenes + _
        ", who " + strisare + " " + strbenenames + "."
        ElseIf annuityinfo.annoption.Value = "Installment Refund" And annuityinfo.benefityes = True Then
        .Bookmarks("annoptionspecs").Range.Text = annuityinfo.instrefnumpyts + " installments and a final payment of " + _
        annuityinfo.instreffinal + " and for life thereafter.  There are " + strnumregpyts + " installments of " + _
        annuityinfo.gross + " and a final payment of " + annuityinfo.instreffinal + " left on this annuity to be paid to " + _
        strsex2 + " " + strnumbenes + ", who " + strisare + " " + strbenenames + "."
        End If
        
'   Benefits Due (To Contingent)
        If annuityinfo.annoption = "Joint and Survivor" And contingentbeneyes = True Then
        .Bookmarks("annoptionspecs").Range.Text = strsex2 + " lifetime.  Upon " + strsex2 + " death, " + stryou2 + _
        annuityinfo.contingentfirst + " " + annuityinfo.contingentlast + " will receive " + annuityinfo.jspercent + " of what " + _
        annuitantinfo.annuitantsal.Value + " " + annuitantinfo.annuitantlast.Value + " was receiving, which will be " + _
        strjscalcpercent + " " + LCase(annuityinfo.freq) + " for your lifetime."
        ElseIf annuityinfo.annoption = "Joint and Survivor with Certain and Life" And contingentbeneyes = True Then
        .Bookmarks("annoptionspecs").Range.Text = LCase(strperiod) + strperiodmys + " and for life thereafter.  Upon " + _
        strsex2 + " death, " + stryou + annuityinfo.contingentfirst + " " + annuityinfo.contingentlast + " will receive " + _
        annuityinfo.jspercent + " of what " + annuitantinfo.annuitantsal.Value + " " + annuitantinfo.annuitantlast.Value + _
        " was receiving, which will be " + strjscalcpercent + " " + LCase(annuityinfo.freq) + " for your lifetime."
        ElseIf annuityinfo.annoption = "Joint and Contingent" And contingentbeneyes = True Then
        .Bookmarks("annoptionspecs").Range.Text = strsex2 + " lifetime.  Upon " + strsex2 + " death, " + stryou2 + _
        annuityinfo.contingentfirst + " " + annuityinfo.contingentlast + " will receive " + annuityinfo.jspercent + " of what " + _
        annuitantinfo.annuitantsal.Value + " " + annuitantinfo.annuitantlast.Value + " was receiving, which will be " + _
        strjscalcpercent + " " + LCase(annuityinfo.freq) + " for your lifetime."
        End If
        
'   Benefits Due (NOT To Contingent)
        If annuityinfo.annoption = "Joint and Survivor with Certain and Life" And contingentbeneno = True Then
        .Bookmarks("annoptionspecs").Range.Text = LCase(strperiod) + strperiodmys + _
        " and for life thereafter.  Since the contingent annuitant, " + annuityinfo.contingentfirst + " " + _
        annuityinfo.contingentlast + ", is also deceased, there are " + annuityinfo.numpytsduebene + _
        " payments left on this annuity to be paid to " + strsex2 + " " + strnumbenes + ", who " + strisare + " " + _
        strbenenames + "."
        End If

    End With

Me.Hide
Load additionalinfo
additionalinfo.Show

End Sub
 


Hi,

All your If statements that are on TWO LINES, like this one need an End If or a Continuation. Check If...Then...Else Statement in VBA Help...
Code:
    If ssano = True And Not bene1first.Value = "" Then
    strbenenames = stryou2 + bene1first.Value + " " + bene1last.Value
    End If

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I'm sorry, but I don't understand the referance to "TWO LINES" considering that code could all be placed on one line. OR, is that what you mean by continuation?...That I need a "_" after my "Then" if I am continuing the code on a second line? (For some reason I thought you didn't have to do that with "Then".

BTW...Thanks for everything Skip, you have been a tremendous help!
 


...considering that code could all be placed on one line.
What COULD be and what IS are are not necessarily the same thing.

Read the HELP on If..Then..Else.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Okay, I have ready the HELP on If, Then, Else...

Let me see if I am understanding this correctly...

In the example that you pulled above, EVEN THOUGH I am only using one line of code for the If Then statement, because I placed that code on the next line, VB is treating it as if it were multiple lines?

Therefore, I would either need to place the code on one line, or "End If" each If statement that uses more than one line?

Thanks Skip!
 
Or use the continuation "_".

Or use Select Case.

Think I got it.

Thanks!!!
 


I am partial to Select Case.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Here's me being naive again...but I thought you couldn't use AND within a Select Case.

With a lot of my If statements, they were:
If this AND this AND this THen this

I haven't been able to locate anything HELP that shows me that you can use AND. I also came across a forum to where they eluded to the fact that it was not possible.
 



With lost of ANDS, like you have, (and without doing the anaysis) I'd be apt to at least try this sort of approch...
Code:
 If [i]expression 1[/i] Then
    If [i]expression 2[/i] Then
       If [i]expression 3[/i] Then
 
       Else

       End If
    Else

    End if
 Else

End if
which acts like multiple ANDS. Sometimes this kind of structure works better. It all depends.



Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I will repeat something I have posted to you before. Specifically the following.
Code:
    If annuitantinfo.annuitantsal.Value = "Mr." Then strsex2 = "his"
    If annuitantinfo.annuitantsal.Value = "Mrs." Then strsex2 = "her"
    If annuitantinfo.annuitantsal.Value = "Ms." Then strsex2 = "her"

versus
Code:
Select Case annuitantinfo.annuitantsal.Value
    Case "Mr."
           strsex2 = "his"
    Case "Mrs."
           strsex2 = "her"
    Case "Ms."
           strsex2 = "her"
End Select

In your multiple If...Then, if .Value = "Mr.", the instructions for "Mrs." and "Ms." will be executed as well. Using Select Case, they will not.

Use Select Case when you have a multiple values testing of ONE variable. That is what Select Case is for.

Just to shorten things.....
Code:
    If period = "1" Then strperiod = "One"
    If period = "2" Then strperiod = "Two"
    If period = "3" Then strperiod = "Three"
    If period = "4" Then strperiod = "Four"
    If period = "5" Then strperiod = "Five"
    If period = "6" Then strperiod = "Six"
    If period = "7" Then strperiod = "Seven"
    If period = "8" Then strperiod = "Eight"
    If period = "9" Then strperiod = "Nine"
    If period = "10" Then strperiod = "Ten"
    If period = "11" Then strperiod = "Eleven"
    If period = "12" Then strperiod = "Twelve"
    If period = "13" Then strperiod = "Thirteen"
    If period = "14" Then strperiod = "Fourteen"
    If period = "15" Then strperiod = "Fifteen"
    If period = "16" Then strperiod = "Sixteen"
    If period = "17" Then strperiod = "Seventeen"
    If period = "18" Then strperiod = "Eighteen"
    If period = "19" Then strperiod = "Nineteen"
Please try to understand. If period = "1", then every single one of the following If...Then instructions will be executed. In other words, with your 50 If..Then instructions, even if period = "1", 49 MORE instructions will be parsed and executed anyway.

Total instructions executed: 50
Code:
Select Case period
    Case "1"
       strperiod = "One"
    Case "2"
       strperiod = "Two"
    Case "3"
       strperiod = "Three"
    Case "4"
       strperiod = "Four"
....etc., etc.
End Select
Total instructions executed: 1

If period = "1", then none of the other conditional tests are executed.

In your code, ALL of other If...Then statements are executed.

Besides being more efficient coding, not having such a huge number of If...Then statements makes it easier to debug. It is also a best-practice to use an explicit End If for every If...Then statements. Technically, yes it is true you CAN get away with it if you are careful with using the "_" underscore character to keep things on what the VBA parser considers a single line. But, with so many If...Then, it can become easy to make a mistake. In which case, it can require a line-by-line checking.
That I need a "_" after my "Then" if I am continuing the code on a second line? (For some reason I thought you didn't have to do that with "Then".

Yes, you do need a "_" if you are continuing ANY single instruction on a second line. That is what "_" means. It tells VBA that the single instruction continues on the next line. Otherwise, it parses that next line as a separate instruction.

Bottom line though, is if you have a requirement for testing multiple values for ONE thing (your "period" say) always use Select Case. That is what it is for.


"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 



furthermore...
Code:
Select Case annuitantinfo.annuitantsal.Value
    Case "Mr."
           strsex2 = "his"
    Case "Mrs.", "Ms."
           strsex2 = "her"
End Select

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Ah that Skip. Yes of course, since both Mrs and Ms have the same result ("her"). I was not paying full attention.

<smacks self>

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Another trick to shorten things:

Instead of
Code:
Dim strperiod As String
    If period = "1" Then strperiod = "One"
    If period = "2" Then strperiod = "Two"
    If period = "3" Then strperiod = "Three"
...
...

This will also do nicely:
Code:
Dim strperiod As String, periods as Variant
periods=Array("Zero", "One", "Two", "Three", "Four", ...)

[b]strperiod = periods(period)[/b]

[pipe]

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Oopsy! "period" is a string, therefore:
Code:
strperiod = periods(CInt(period))


[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
>every single one of the following If...Then instructions will be executed

In the interests of completeness I'd just like to point out that, along with the Case statement already illustrated, this can also be happily avoided with If by using the

If ... Then
...
ElseIf ... Then
...
End If

block statement

 
Wow folks! I really appreciate the help and clarification! I am quite new to VB and plan to take all of your instruction into consideration.

Thanks!!
 
Code:
Dim strperiod As String, periods as Variant
periods=Array("Zero", "One", "Two", "Three", "Four", ...)

strperiod = periods((CInt)period)

A couple of questions about this...

1) Since the period starts with "1", do I really need "Zero" in the Array?

2) Since my final period statement currently is:
Code:
If period > "50" Then strperiod = period
...is this possible to convey within the statement you have suggested?

Thanks!
 
>1) Since the period starts with "1", do I really need "Zero" in the Array?

Nope


>2) Since my final period statement currently is:

Something like

strperiod = IIf(period > "50", period, periods(CInt(period)))

would address that
 
I incorporated the Zero because Arrays begin at 0, i.e. the first entry would be array(0).

So if you want to directly assign "1" to "one", then "one" must be array(1), i.e. the second entry.
;-)

With your concluding statement: I actually overlooked that.
You'd have to do that check first in order to avoid an error with the array:
Code:
If period > "50" Then 
   strperiod = period
Else
   strperiod = periods((CInt)period)
End If

OK?
:)

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
MakeItSo, Let me see if I am understanding what you are saying about the array (as I now have a slight bit of confusion given strongm's reply)...

...Since the first entry of an array would be (0), I need to use a placeholder of sorts as my first array entry in order for "1" to show as "One" (i.e. your original example)?

Additionally...putting the code together, would it look like this:

Code:
Dim strperiod As String, periods as Variant
If period > "50" Then 
   strperiod = period
Else
   strperiod = periods((CInt)period)
End If
periods=Array("Zero", "One", "Two", "Three", "Four", ...)

I am assuming the ordering matters here, but I am unsure if what I have is correctly ordered.

Thanks again for all your help!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top