Error if without endif

I am very new to VBA. I tried to calculate median for a vector. The following code keeps getting warning regarding "Block if without End if". I tried to change the place of "End IF", but it resulte...

I am very new to VBA. I tried to calculate median for a vector. The following code keeps getting warning regarding «Block if without End if». I tried to change the place of «End IF», but it resulted in another warning «Block end if without if». Your input would be appreciated. Thanks.

Sub CalculateMedian()

    DoCmd.SetWarnings False

    Dim db As DAO.Database
    Dim onet As DAO.Recordset

    Dim Ocode As String
    Dim ag As DAO.Recordset
    Dim agMedian As Integer

    Set db = CurrentDb

    'select one variable in current database
    Set onet = db.OpenRecordset("SELECT DISTINCT ONetCode FROM Single WHERE LEN(ONetCode)>8")

    Do While Not onet.EOF

        'assigning value to a variable does not need a "SET"
        Ocode = onet.Fields("ONetCode")
        'any data meet the criterion--&Ocode& can vary
        Set ag = db.OpenRecordset("SELECT AG FROM Single WHERE ONetCode='" & Ocode & "' ORDER BY AG")

        'using .recordcount needs to use .movelast first
        ag.MoveLast
        ag.MoveFirst
        If ag.RecordCount Mod 2 = 1 Then
            agMedian = ((ag.RecordCount + 1) / 2)
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    'inset the result into a new table, and need to create a new table in advance
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ag("AG") & ");")
                    Exit Do
        End If

       If ag.RecordCount Mod 2 = 0 Then
            agMedian = ag.RecordCount / 2
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    m1 = ag("AG")
                ElseIf thecount = agMedian + 1 Then
                    m2 = ag("AG")
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ((m1 + m2) / 2) & ");")
                    Exit Do
        End If

    Loop

    DoCmd.SetWarnings True

End Sub

HansUp's user avatar

HansUp

95.4k11 gold badges75 silver badges135 bronze badges

asked Aug 16, 2013 at 16:53

lucyh's user avatar

1

The code was missing more than one End If. And there were 2 missing Loop statements as well.

When the code is complex enough that sorting out the block end statements becomes challenging, make a copy of the procedure and throw away basically everything other than the block control statements. That method leaves this from your current code.

    Do While Not onet.EOF
        If ag.RecordCount Mod 2 = 1 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
        End If
        If ag.RecordCount Mod 2 = 0 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
                ElseIf thecount = agMedian + 1 Then
        End If
    Loop

And here is my best guess for what you need instead. I appended comments to several of those statements because it helps me match them up properly.

    Do While Not onet.EOF
        If ag.RecordCount Mod 2 = 1 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
                End If ' thecount
            Loop ' Not ag.EOF
        End If ' ag.RecordCount Mod 2 = 1
        If ag.RecordCount Mod 2 = 0 Then
            Do While Not ag.EOF
                If thecount = agMedian Then
                ElseIf thecount = agMedian + 1 Then
                End If ' thecount
            Loop ' Not ag.EOF
        End If ' ag.RecordCount Mod 2 = 0
    Loop ' Not onet.EOF

answered Aug 16, 2013 at 21:08

HansUp's user avatar

HansUpHansUp

95.4k11 gold badges75 silver badges135 bronze badges

0

it appears you’re missing an end if after the exit do in the first block of code. There should be 2 there, one to close out the last if statement, and one to close out the first block.

Sub CalculateMedian()

    DoCmd.SetWarnings False

    Dim db As DAO.Database
    Dim onet As DAO.Recordset

    Dim Ocode As String
    Dim ag As DAO.Recordset
    Dim agMedian As Integer

    Set db = CurrentDb

    'select one variable in current database
    Set onet = db.OpenRecordset("SELECT DISTINCT ONetCode FROM Single WHERE LEN(ONetCode)>8")

    Do While Not onet.EOF

        'assigning value to a variable does not need a "SET"
        Ocode = onet.Fields("ONetCode")
        'any data meet the criterion--&Ocode& can vary
        Set ag = db.OpenRecordset("SELECT AG FROM Single WHERE ONetCode='" & Ocode & "' ORDER BY AG")

        'using .recordcount needs to use .movelast first
        ag.MoveLast
        ag.MoveFirst
        If ag.RecordCount Mod 2 = 1 Then
            agMedian = ((ag.RecordCount + 1) / 2)
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    'inset the result into a new table, and need to create a new table in advance
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ag("AG") & ");")


                   End If 'ends the If thecount = agMedian if statement -- will continue to iterate until EOF

                    Exit Do 'EOF hit.


        End If 'ends the If ag.RecordCount Mod 2 = 1 block

       If ag.RecordCount Mod 2 = 0 Then
            agMedian = ag.RecordCount / 2
            thecount = 0
            Do While Not ag.EOF
                thecount = thecount + 1
                If thecount = agMedian Then
                    m1 = ag("AG")
                ElseIf thecount = agMedian + 1 Then
                    m2 = ag("AG")
                    DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" & Ocode & "'," & ((m1 + m2) / 2) & ");")
                    Exit Do
        End If 'thecount = agMedian if statement
        End If 'end ag.RecordCount Mod 2 = 0

    Loop

    DoCmd.SetWarnings True

End Sub

answered Aug 16, 2013 at 17:00

user2366842's user avatar

user2366842user2366842

1,23114 silver badges23 bronze badges

4

Today I’ll show you how to resolve the error “End If without block If” in VBA. But first, you have to understand the “If” statement in order to fix the issue.

The IF statement and its various forms

The If statement is a conditional clause that helps us to run code using a condition that is decided during runtime. You might wonder, “What is the need to decide the condition during runtime? Can’t we decide that earlier?” In reality, there are many situations where an action needs to be performed only if certain criteria are met or a condition is fulfilled. Sometimes this check might even depend on the user’s input value.

For example, let us imagine that a bank offers 8% ROI on fixed deposit accounts if the customer is a senior citizen and only 6% ROI for other customers. In this case, the code that calculates the interest and maturity amount should both a) consider the age of the customer and b) use a condition to use different values for senior and non-senior citizens. This is where an “If conditional statement” steps in.

Now let’s see the code for the above scenario assuming that one must be 60 years old to be called a senior citizen.

Sub sample_coding()
'declaration of variables
Dim matamt, prinamt, roi, term, custage
‘ receive input from user
custage = InputBox("Enter the age of the customer")
‘ assign some values
prinamt = 10000 ' Principal amount
term = 2 ' 2 years
' decide the roi value
If custage < 60 Then
    roi = 6
Else
    roi = 8
End If
' formula to calculate the FD maturity amount.
matamt = prinamt + (prinamt * roi * term / 100)
‘ printing the output
Debug.Print matamt
End Sub

Looking at the example above, we see that the syntax for using a simple If statement is

If <condition> Then

<code>

End If

But the same conditional statement has different forms  as listed below.

  1. A simple If Block
  2. An If – Else block
  3. An Else-If block
  4. Nested If block

The Compile Error “End If without Block If:

This is a simple compile time error that’s thrown when the code containing any If blocks do not comply with the syntax (or) such a statement does not exist.

Here are some instances where this error might occur

Rule 1: End If with single line statement

If the single line of code to be executed is placed in the same line as the “If – then” statement, then the “End If” statement needs to be omitted. In other words, the If statement is considered complete without an “End If” statement in cases where the conditional code is placed in the same line.

If &amp;amp;lt;condition&amp;amp;gt; Then &amp;amp;lt;code&amp;amp;gt;

For example:

The If condition in the above code can be rewritten using this rule to avoid the compile error “End if without block If”.

' Fix an roi in common
	roi = 8 
'Change the value for non-senior citizens alone using the rule 1
If custage &amp;amp;lt; 60 Then roi = 6
' comment or remove the end if statement to fix the error.
'End If 

According to Rule 1, if “End If” is used in the above code, you will encounter the error “End If without block If”. So, do not forget to remove it.

If you’re using nested if conditions, ensure that every “If” statement that has been opened, has a corresponding “End If” statement. This is in addition to Rule 1 above.

Example 1

If custage &amp;amp;lt; 60 Then
    roi = 6
    If strgen = "Female" And custage &amp;amp;gt; 57 Then roi = 8
    End If '********Line is explained below*********
Else
    roi = 8
End If

In this piece of code,

  • The inner “If” condition follows Rule 1 (i.e. code is placed in the same statement after “Then” keyword). Therefore, this statement is a standalone statement that does not require “End If”.
  • But since we have an “End If” statement , it will be considered to be the corresponding “End “ of the outer if statement (Line 1).
  • This leads to the “Else” keyword in the fifth line looking for its corresponding “If statement”. In turn, we end up with the error “Else without If” which is similar to “End if without block If”.
  • The solution to this problem is to remove the unnecessary “End if” in line 4 or place the code “roi=8” in the next line i.e between the IF… then and the End if statements.

Example 2

If apple = "sweet" Then
    If mango = "sweet" Then Debug.Print "Fruits are sweet"
    End If
End If

In this example,

  • Here since line 2 is already complete without “End if “, line 3 is automatically matched with the If statement of line number 1.
  • So, the “End If” in line 4 searches for its pair of “If statement” and leads to the compile error “End if without block If”.
  • The solution to this is to remove line 3 or place the “Debug.Print” statement in a separate line before the “End If” statement in line no 3.

Rule 3: Forgetting part of your deleted code

Ensure that there is no “End if” statement left behind without an “If” statement in your code. This might happen when you maintain code or change your logic after a long period of time.

For example, you might think that an “If – End if “ block of code might not be required in a certain place. And after you delete that “If block”, you may forget to delete its “End If” statement. This again causes the same compile error we keep seeing, “End if without block If”.

For Example:

If apple = "sweet" Then
    End If
End If

Imagine that you wanted to delete the inner If block in the above example. While doing so, you forgot to delete the “End If” statement. Then, you are sure to encounter the compile error “End If without block If”.

Here is a video that explains everything outlined above with sample code.  The code is explained and executed line by line, so you can completely understand what causes the error “End if without block If”.

Summary

Basically, when you look at the error statement, it is clear that it is thrown if there are any unnecessary ‘End If’ statements. The only solution is to trace the code and remove the erroneous statement after confirming that it does not affect the rest of the code in any way.

The other compile error “Else without If”, for which there is an example in Rule 2, is related to this error. It is thrown when there is an “Else <some code> End If” statement or just an “Else” statement without an “If <condition>  Then” statement. In general, for any error , it is wise and time saving to check the syntax first and then proceed with troubleshooting.

I have a combobox in Word that is supposed to populate the termShorthand text field based on the selection from the termWritten array. I am receiving the Block If without End If compile error even though I have it after my If statements.

Private Sub termWritten_DropButtonClick()
    termWritten.List = Array("first", "second", "third", "final")
End Sub

Private Sub termWritten_Change()
    If termWritten.Value = "first" Then
        termShorthand.Value = "three (3)"
    Else
        If termWritten.Value = "second" Then
            termShorthand.Value = "two (2)"
        Else
            If termWritten.Value = "third" Then
                termShorthand.Value = "one (1)"
            Else
                If termWritten.Value = "final" Then
                    termShorthand.Value = "no"
                End If
End Sub

I say Reinstate Monica's user avatar

asked May 7, 2019 at 15:18

Kyle Underhill's user avatar

Kyle UnderhillKyle Underhill

1151 gold badge2 silver badges10 bronze badges

You need an End If statement for each If statement, like this:

Private Sub termWritten_Change()
    If termWritten.Value = "first" Then
        termShorthand.Value = "three (3)"
    Else
        If termWritten.Value = "second" Then
            termShorthand.Value = "two (2)"
        Else
            If termWritten.Value = "third" Then
                termShorthand.Value = "one (1)"
            Else
                If termWritten.Value = "final" Then
                    termShorthand.Value = "no"
                End If 'final
            End If 'third
        End If 'second
    End If 'first
End Sub

You can learn more about the If…Then…Else statement on Microsoft Docs.

answered May 7, 2019 at 15:27

I say Reinstate Monica's user avatar

@twisty impersonator’s correct regarding the syntax for if/then/else, but your code would be simpler to follow and update if you used Select Case instead:

Private Sub termWritten_Change()

Select Case termWritten.Value
   Case Is = "first
      termShorthand.Value = "three (3)"
   Case Is = "second"
      termShorthand.Value = "two (2)"
   ' and so on, adding another Case Is = "xyz" for each value
   ' you want to test for.  At the end, it's usually a good idea to
   ' include
   Case Else
     ' This runs if no other conditions are met
     ' Use it to set an error code, supply a default value, etc.
End Select

End Sub

And following twisty’s example, I’m adding a link to MS’ documentation for Select Case:

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/select-case-statement

answered May 7, 2019 at 15:54

Steve Rindsberg's user avatar

Steve RindsbergSteve Rindsberg

5,4711 gold badge15 silver badges17 bronze badges

While Select Case is probably better it’s worth mentioning that you could have used ElseIf and then only one End If would be required

Private Sub termWritten_DropButtonClick()

   termWritten.List = Array("first", "second", "third", "final")

End Sub

Private Sub termWritten_Change()

   If termWritten.Value = "first" Then

       termShorthand.Value = "three (3)"

   ElseIf termWritten.Value = "second" Then

       termShorthand.Value = "two (2)"

   ElseIf termWritten.Value = "third" Then

       termShorthand.Value = "one (1)"

   ElseIf termWritten.Value = "final" Then

       termShorthand.Value = "no"

   End If

End Sub

answered Jun 5, 2019 at 21:26

Steven Martin's user avatar

Содержание

  1. Compile error end if without block if
  2. VBA and VB.Net Tutorials, Education and Programming Services
  3. How to Fix the “End If without block If” Error
  4. The IF statement and its various forms
  5. The Compile Error “End If without Block If:
  6. Rule 1: End If with single line statement
  7. Rule 2: Extra End If statements
  8. Rule 3: Forgetting part of your deleted code
  9. Summary
  10. Compile error end if without block if
  11. Asked by:
  12. Question
  13. Compile Error: Block If without End If .
  14. APPPRO
  15. Excel Facts
  16. DonkeyOte
  17. Thread: compile error: Block If without End If? in VB6
  18. compile error: Block If without End If? in VB6
  19. Re: compile error: Block If without End If? in VB6
  20. Re: compile error: Block If without End If? in VB6
  21. Re: compile error: Block If without End If? in VB6
  22. Re: compile error: Block If without End If? in VB6
  23. Re: compile error: Block If without End If? in VB6
  24. Re: compile error: Block If without End If? in VB6
  25. Re: compile error: Block If without End If? in VB6
  26. Re: compile error: Block If without End If? in VB6
  27. Re: compile error: Block If without End If? in VB6
  28. Re: compile error: Block If without End If? in VB6
  29. Re: compile error: Block If without End If? in VB6
  30. Re: compile error: Block If without End If? in VB6
  31. Re: compile error: Block If without End If? in VB6
  32. Re: compile error: Block If without End If? in VB6
  33. Re: compile error: Block If without End If? in VB6
  34. Re: compile error: Block If without End If? in VB6
  35. Re: compile error: Block If without End If? in VB6
  36. Re: compile error: Block If without End If? in VB6
  37. Re: compile error: Block If without End If? in VB6
  38. Re: compile error: Block If without End If? in VB6
  39. Re: compile error: Block If without End If? in VB6
  40. Re: compile error: Block If without End If? in VB6
  41. Re: compile error: Block If without End If? in VB6
  42. Re: compile error: Block If without End If? in VB6
  43. Re: compile error: Block If without End If? in VB6
  44. Re: compile error: Block If without End If? in VB6
  45. Re: compile error: Block If without End If? in VB6
  46. Re: compile error: Block If without End If? in VB6
  47. Re: compile error: Block If without End If? in VB6
  48. Re: compile error: Block If without End If? in VB6
  49. Re: compile error: Block If without End If? in VB6
  50. Re: compile error: Block If without End If? in VB6
  51. Re: compile error: Block If without End If? in VB6
  52. Re: compile error: Block If without End If? in VB6
  53. Re: compile error: Block If without End If? in VB6
  54. Re: compile error: Block If without End If? in VB6
  55. Re: compile error: Block If without End If? in VB6
  56. Re: compile error: Block If without End If? in VB6
  57. Re: compile error: Block If without End If? in VB6

Compile error end if without block if

VBA and VB.Net Tutorials, Education and Programming Services

How to Fix the “End If without block If” Error

Today I’ll show you how to resolve the error “End If without block If” in VBA. But first, you have to understand the “If” statement in order to fix the issue.

The IF statement and its various forms

The If statement is a conditional clause that helps us to run code using a condition that is decided during runtime. You might wonder, “What is the need to decide the condition during runtime? Can’t we decide that earlier?” In reality, there are many situations where an action needs to be performed only if certain criteria are met or a condition is fulfilled. Sometimes this check might even depend on the user’s input value.

For example, let us imagine that a bank offers 8% ROI on fixed deposit accounts if the customer is a senior citizen and only 6% ROI for other customers. In this case, the code that calculates the interest and maturity amount should both a) consider the age of the customer and b) use a condition to use different values for senior and non-senior citizens. This is where an “If conditional statement” steps in.

Now let’s see the code for the above scenario assuming that one must be 60 years old to be called a senior citizen.

Looking at the example above, we see that the syntax for using a simple If statement is

But the same conditional statement has different forms as listed below.

  1. A simple If Block
  2. An If – Else block
  3. An Else-If block
  4. Nested If block

The Compile Error “End If without Block If:

This is a simple compile time error that’s thrown when the code containing any If blocks do not comply with the syntax (or) such a statement does not exist.

Here are some instances where this error might occur

Rule 1: End If with single line statement

If the single line of code to be executed is placed in the same line as the “If – then” statement, then the “End If” statement needs to be omitted. In other words, the If statement is considered complete without an “End If” statement in cases where the conditional code is placed in the same line.

For example:

The If condition in the above code can be rewritten using this rule to avoid the compile error “End if without block If”.

According to Rule 1, if “End If” is used in the above code, you will encounter the error “End If without block If”. So, do not forget to remove it.

If you’re using nested if conditions, ensure that every “If” statement that has been opened, has a corresponding “End If” statement. This is in addition to Rule 1 above.

Example 1

In this piece of code,

  • The inner “If” condition follows Rule 1 (i.e. code is placed in the same statement after “Then” keyword). Therefore, this statement is a standalone statement that does not require “End If”.
  • But since we have an “End If” statement , it will be considered to be the corresponding “End “ of the outer if statement (Line 1).
  • This leads to the “Else” keyword in the fifth line looking for its corresponding “If statement”. In turn, we end up with the error “Else without If” which is similar to “End if without block If”.
  • The solution to this problem is to remove the unnecessary “End if” in line 4 or place the code “ roi=8 ” in the next line i.e between the IF… then and the End if statements.

Example 2

In this example,

  • Here since line 2 is already complete without “End if “, line 3 is automatically matched with the If statement of line number 1.
  • So, the “End If” in line 4 searches for its pair of “If statement” and leads to the compile error “End if without block If”.
  • The solution to this is to remove line 3 or place the “ Debug.Print ” statement in a separate line before the “End If” statement in line no 3.

Rule 3: Forgetting part of your deleted code

Ensure that there is no “End if” statement left behind without an “If” statement in your code. This might happen when you maintain code or change your logic after a long period of time.

For example, you might think that an “If – End if “ block of code might not be required in a certain place. And after you delete that “If block”, you may forget to delete its “End If” statement. This again causes the same compile error we keep seeing, “End if without block If”.

For Example:

Imagine that you wanted to delete the inner If block in the above example. While doing so, you forgot to delete the “End If” statement. Then, you are sure to encounter the compile error “End If without block If”.

Here is a video that explains everything outlined above with sample code. The code is explained and executed line by line, so you can completely understand what causes the error “End if without block If”.

Summary

Basically, when you look at the error statement, it is clear that it is thrown if there are any unnecessary ‘End If’ statements. The only solution is to trace the code and remove the erroneous statement after confirming that it does not affect the rest of the code in any way.

The other compile error “Else without If”, for which there is an example in Rule 2, is related to this error. It is thrown when there is an “Else End If” statement or just an “Else” statement without an “If Then” statement. In general, for any error , it is wise and time saving to check the syntax first and then proceed with troubleshooting.

Источник

Compile error end if without block if

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Asked by:

Question

This is module files

Sub Additem()
Dim ItemRow As Long, AvailRow As Long
With Sheet1
If .Range(«B5»).Value = Empty Then Exit Sub
On Error Resume Next
.Shapes(«itempic»).Delete
On Error GoTo 0
ItemRow = .Range(«B5»).Value ‘item row
AvailRow = .Range(«K999»).End(xlUp).Row + 1 ‘firs avail row
.Range(«B6»).Value = AvailRow ‘Set Receipt Row
.Range(«E3»).Value = Sheet2.Range(«B» & ItemRow).Value ‘Item Name
.Range(«F6»).Value = Sheet2.Range(«D» & ItemRow).Value ‘Item Price
.Range(«F8»).Value = 1 ‘Default Item Qty To 1

‘Add Item Detail to receipt
.Range(«K» & AvailRow).Value = .Range(«E3»).Value ‘Item Name
.Range(«L» & AvailRow).Value = .Range(«F8»).Value ‘Item Qty
.Range(«M» & AvailRow).Value = .Range(«f6»).Value ‘Item Price
.Range(«N» & AvailRow).Value = «=L» & AvailRow & «*M» & AvailRow ‘Total Price formula

‘On Error Resume Next
If Dir(Sheet2.Range(«E» & ItemRow).Value, vbDirectory) <> «» Then
With .Pictures.Insert(Sheet2.Range(«E» & ItemRow).Value)
With .ShapeRange
.LockAspectRatio = msoTrue
.Height = 45
.Name = «ItemPic»
End With
End With
With .Shapes(«ItemPic»)
.Left = Sheet1.Range(«D6»).Left
.Top = Sheet1.Range(«D6»).Top
.Visible = msoCTrue
End With
End If
‘On Error Goto 0
.Range(«E10:F10»).ClearContents ‘Clear Iteam Iteam
.Range(«E10»).Select
End With
End Sub

This is sheet 1 code

Private Sub Worksheet_Change(ByVal Target As Range)
‘on change of item, if row found and add to receipt
If Not Intersect(Target, Range(«E10»)) Is Nothing And Range(«E10»).Value <> Empty Then Additem

Источник

Compile Error: Block If without End If .

APPPRO

Active Member

I’ve wrtitten the following macro to Clear certain Rows on a sheet if certain criteria are met on another sheet.
I keep getting a Compile Error: Block If without End If. The End Sub is highlighted. WHY? And how do I fix it?

Sub CLEAR_ROW()

‘ IF Sheets(«ENTRY FORM»).Range(«k75») = 0 Then
Sheets(«data»).Rows(«16:16»).CLEAR
If Sheets(«ENTRY FORM»).Range(«k74») = 0 Then
Sheets(«data»).Rows(«15:15»).CLEAR
If Sheets(«ENTRY FORM»).Range(«k73») = 0 Then
Sheets(«data»).Rows(«14:14»).CLEAR
If Sheets(«ENTRY FORM»).Range(«k72») = 0 Then
Sheets(«data»).Rows(«13:13»).CLEAR
If Sheets(«ENTRY FORM»).Range(«k71») = 0 Then
Sheets(«data»).Rows(«12:12»).CLEAR
If Sheets(«ENTRY FORM»).Range(«k70») = 0 Then
Sheets(«data»).Rows(«11:11»).CLEAR
If Sheets(«ENTRY FORM»).Range(«k69») = 0 Then
Sheets(«data»).Rows(«10:10»).CLEAR
If Sheets(«ENTRY FORM»).Range(«k68») = 0 Then
Sheets(«data»).Rows(«9:9»).CLEAR
If Sheets(«ENTRY FORM»).Range(«k67») = 0 Then
Sheets(«data»).Rows(«8:8»).CLEAR
If Sheets(«ENTRY FORM»).Range(«k66») = 0 Then
Sheets(«data»).Rows(«7:7»).CLEAR
End If
End Sub

Excel Facts

DonkeyOte

MrExcel MVP

If you do THEN and return you must have an End If — e.g

IF a=a then
a1 = a
End if

Or change your code

If the THEN and subsequent statement are on one line you do not need an End If.

Источник

Thread: compile error: Block If without End If? in VB6

Thread Tools
Display

compile error: Block If without End If? in VB6

hi, i am getting an error, when i am running the program.
compile error:Block If wihtout End If
what does this mean?

i have three if then else statements in my program and i tried to end them by adding end if at the end simulataneously and individually to each but it is stil coming up . i dont knw what to do..any advice

Re: compile error: Block If without End If? in VB6

Show us the code so we can help you fix it.

Re: compile error: Block If without End If? in VB6

Private Sub Command1_Click()

Dim d As Double
Dim dtm As Double
Dim dlm As Double
Dim fi As Double
Dim hte As Double
Dim hre As Double
Dim hm As Double
Dim tou As Double
Dim u1 As Double
Dim u2 As Double
Dim u3 As Double
Dim u4 As Double
Dim b0 As Double
Dim alpha As Double
Dim beta As Double
Dim di As Double
Dim t As Double
Dim p As Double
Dim e As Double
Dim pol As String
Dim h As Double
Dim v As Double
Dim ae As Double
Dim ap As Double
d = Val(Text1.Text)
dtm = Val(Text2.Text)
fi = Val(Text3.Text)
hte = Val(Text4.Text)
hre = Val(Text5.Text)
hm = Val(Text6.Text)
pol = Val(Text7.Text)
ae = 6370
‘defining the first equation as tou
If (pol = h) Then
tou = 0
ElseIf (pol = v) Then
tou = 90
u1 = ((10 ^ (-dtm / 16 — (6.6 * tou)) + (10 ^ -(0.496 + 0.354 * tou)) ^ 5)) ^ 0.2
If (fi 70) Then
u4 = 10 ^ (0.3 * (Log(u1) / Log(10)))
‘now defining the b0
If (fi 70) Then
b0 = 4.17 * u1 * u4
‘now defining u2 and u3
alpha = -0.6 — 3.5 * (10 ^ -9) * (d ^ 3.1) * tou
u2 = ((500 / ae) * ((d ^ 2) / ((Sqr(hte) + Sqr(hre)) ^ 2)) ^ alpha)
‘now u3
If (hm 10) Then
u3 = Exp(-4.6 * (10 ^ -5) * (hm — 10) * (43))
‘now defining beta
beta = b0 * u2 * u3
‘now defining t
t = (1.076 / (2.0058 — Log(beta) ^ 1.012)) * Exp(-(9.51 — 4.8 * Log(beta) + 0.198 * Log(beta) ^ 2) * 10 ^ -6 * d * 1.13)
‘the final equation for time percentage (cumulative distribution)
ap = -12 + (1.2 + 3.7 * 10 ^ -3 * d) * Log(p / beta) + 12 * (p / beta) ^ t
result = ap
result = result.Caption = CStr(result)

Re: compile error: Block If without End If? in VB6

You dont have any End If’s to close your If’s

I think you need 2 End Ifs at the end of your code. before End Sub

and next time remember to put code under the CODE tag or VBCode tags.. for easier viewing on our side!

_____________________________________________________________________

—-If this post has helped you. Please take time to Rate it.
—-If you’ve solved your problem, then please mark it as RESOLVED from Thread Tools .

Re: compile error: Block If without End If? in VB6

Looks like you left out four End If statements:

Re: compile error: Block If without End If? in VB6

Indenting would help you and others to understand your code. Here’s a start:

Re: compile error: Block If without End If? in VB6

four. I found four unclosed If statements.
As to where they go, you’ll need to work that out on your own. I don’t know if those if statements are to be 4 separate ones, or if they are supposed to be nested inside each other.

Re: compile error: Block If without End If? in VB6

You also don’t need to do things like

You can use Val(Text7.Text) directly in your formulas. Even if you don’t it would be a good idea to give your textboxes (and other controls) meaningful names like txtPol.

Re: compile error: Block If without End If? in VB6

Note also that your code can also be simplified a bit:

Re: compile error: Block If without End If? in VB6

IMO you should never jam two lines of code together on one line with code like the following:

Else: u4 = 10 ^ (0.3 * (Log(u1) / Log(10)))

Re: compile error: Block If without End If? in VB6

four. I found four unclosed If statements.
As to where they go, you’ll need to work that out on your own. I don’t know if those if statements are to be 4 separate ones, or if they are supposed to be nested inside each other.

+1! They could be nested. I removed the nesting, and that may not be what OP wants.

Re: compile error: Block If without End If? in VB6

first of all. i dont think i can use nesting of if stamnts bcz this is the flow of equaiton to find a final value «ap» (which is below 100 and in terms of percentage)
secondly, after i added the end if statements i got error as Run time error 5: «invalid procedure call or argument» at u4 in the code with yellow colour highlighted. what shall i do now?

Last edited by hadimohammed365; Apr 29th, 2009 at 03:51 PM .

Re: compile error: Block If without End If? in VB6

Then u1 will be zero. Is Log(0) a problem?

Re: compile error: Block If without End If? in VB6

Apparently Log(0) is undefined or -infinity so either would definitely be a problem.

Re: compile error: Block If without End If? in VB6

IMO you should never jam two lines of code together on one line with code like the following:

Else: u4 = 10 ^ (0.3 * (Log(u1) / Log(10)))

Once in a while, a colon does not bother me. In fact, there are times when it can make the code clearer than a hard return. «Never» is a very powerful word.

Re: compile error: Block If without End If? in VB6

well, how u1 comes down to zero. can u pls explain me.
actually just now i worked out in a paper now, i got different answer
if pol = h;dtm = 350 and tou = 0 i got u1 as 0.31 (but not exact zero)
if pol = v; dtm = 350 and tou = 90 i got u1 as 1.31 (again not zero)
anyhow log(0) is invalid function gives output as «infinity» and stops the whole operation in code but when it is greater than zero as like 0.31 it doesnt matter bcz it returns log(0.31) as -0.50 output.

so i dont knw hwo to sort out the run time error 5:invalid procedure call or argument

thanks for your time

Re: compile error: Block If without End If? in VB6

well, how u1 comes down to zero. can u pls explain me.
actually just now i worked out in a paper now, i got different answer
if pol = h;dtm = 350 and tou = 0 i got u1 as 0.31 (but not exact zero)
if pol = v; dtm = 350 and tou = 90 i got u1 as 1.31 (again not zero)
anyhow log(0) is invalid function gives output as «infinity» and stops the whole operation in code but when it is greater than zero as like 0.31 it doesnt matter bcz it returns log(0.31) as -0.50 output.

so i dont knw hwo to sort out the run time error 5:invalid procedure call or argument( what is the meaning of this error?)

thanks for your time

Re: compile error: Block If without End If? in VB6

hi everyone. anybody got any idea how to sort this out. it is actually very important..the final value from «ap» should be a number less than 100.
pls dont ignore.
thanks

Re: compile error: Block If without End If? in VB6

Give me your most recent code, and I’ll take a look at it.

Re: compile error: Block If without End If? in VB6

well, how u1 comes down to zero. can u pls explain me.
actually just now i worked out in a paper now, i got different answer
if pol = h;dtm = 350 and tou = 0 i got u1 as 0.31 (but not exact zero)
if pol = v; dtm = 350 and tou = 90 i got u1 as 1.31 (again not zero)
anyhow log(0) is invalid function gives output as «infinity» and stops the whole operation in code but when it is greater than zero as like 0.31 it doesnt matter bcz it returns log(0.31) as -0.50 output.

so i dont knw hwo to sort out the run time error 5:invalid procedure call or argument

thanks for your time

then if pol = h, u1 is never calculated so it will be zero.

Re: compile error: Block If without End If? in VB6

this is my recent code i made few changes in it, pls have a look
thanks

Re: compile error: Block If without End If? in VB6

but you still have a problem if pol is not equal to h or v.

Re: compile error: Block If without End If? in VB6

ITS WORKING IN BOTH WAYS. I CAN SAY THIS BCZ ITS NOT SHOWING ANY ERROR NOW ON THAT POINT, BUT IT IS GIVING THE SAME ERROR AT THIS POINT.

SAME RUN TIME ERROR 5: INVALID CALL PROCEDURE OR ARGUMENT

Re: compile error: Block If without End If? in VB6

I THINK ITS WORKING BCZ ITS NOT SHOWING ANY ERROR AT THIS TIME AT THAT POINT, BUT THE SAME ERROR IS COMING AT THIS POINT IN CODE

SAME RUN TIME ERROR 5: INVALD CALL PROCEDURE OR ARGUMENT
AS LIKE BEFORE

Re: compile error: Block If without End If? in VB6

Well I don’t think it’s the problem but you never calculate d.

You might want to take a look at my VB6 Debug Tutorial and then look at the values of the variables in that line.

Re: compile error: Block If without End If? in VB6

As Marty says, «. you still have a problem if pol is not equal to h or v.»

I don’t see where h or v are ever assigned a value, so they must be zero. If they are zero, «pol» will not equal either h or v, right?

If that’s the case, u1 never gets a value assigned to it.

I forced «pol to equal h» and «pol to equal v» to test it, then my «invalid procedure» moved to a different spot (the statement assigning a value to «t,» as you point out below. When I break down that statement into smaller chunks, I get the error here: «Log(beta) ^ 1.012.» If I try a statement like «-101 ^ 1.012» I do not get an error. I don’t have any clue why this should be the case.

Re: compile error: Block If without End If? in VB6

As Marty says, «. you still have a problem if pol is not equal to h or v.»

I don’t see where h or v are ever assigned a value, so they must be zero. If they are zero, «pol» will not equal either h or v, right?

If that’s the case, u1 never gets a value assigned to it.

I forced «pol to equal h» and «pol to equal v» to test it, then my «invalid procedure» moved to a different spot (the statement assigning a value to «t,» as you point out below. When I break down that statement into smaller chunks, I get the error here: «Log(beta) ^ 1.012.» If I try a statement like «-101 ^ 1.012» I do not get an error. I don’t have any clue why this should be the case.

The problem with «Log(beta) ^ 1.012.» is most likely that beta contains a too big or too small or otherwise invalid value.

Re: compile error: Block If without End If? in VB6

yes, value of d is not needed to be calculated becz its value is given by the user in a textbox which is distance in Km. all the value user enters is numbers.and i defined them as doubles initially,
except for pol (which is polarization) here user enters a letter h or v and i defined it as string. is this right?
anyhow i am at library now, i wil check this code tomarow and b back.
thanks everyone for your time.

Re: compile error: Block If without End If? in VB6

and so unless you have a statement someplace in this sub like d = txtDistance.Text then d will always be zero.

Re: compile error: Block If without End If? in VB6

yes, i have defined it later for d
like intially dim d as double
later afttr defining , i did u can chk it in code, as
d = val(text1.text)

Re: compile error: Block If without End If? in VB6

Please show us your current code.

Re: compile error: Block If without End If? in VB6

this is my recent code, again error occurs at «t» (run time error 5).
and, yes you are right that beta contains a very small value bcz b0, u2, u3 contains very small value n when they get multiplied it returns small value (would this be a problem?). as that value is being used in the following equations of «ap» i dont think it should be a problem.
anyways, thanks for your time marty.

Re: compile error: Block If without End If? in VB6

i have sorted it out, now i am getting a right result . thank you for your help evryone.

Re: compile error: Block If without End If? in VB6

i am back with bit more errors, actually when i am entering pol in textbox as h or v it is not taking its value which is 0 or 90 depending on the entered function of h or v by the user can anyone suggest how to overcome this and i am going wrong anywhere did i defined it in right way as

can anyone give a right way to get the right answer when user enters h or v (which shouldn’t be case sensitive that is it can also be H or V in capital letters) in the if then else loop. in my code

my code contains 8 textboxes (as user going to enter 8 values) one commmand button and one final result label.

Re: compile error: Block If without End If? in VB6

Your statement «If pol = h Then. «

Since you are declaring a variable «h» as a double, I assumed that’s what this If>Then statement was referring to.

Do you actually mean «If pol = ‘h’ then. » instead, where «h» is what the user entered in textbox 7?

Re: compile error: Block If without End If? in VB6

This assumes that h and v are letters that are entered in the pol textbox (and if they are you don’t need the h and v variables you defined).

Also if the value is limited to h or v it would be better to have two radio buttons instead of the textbox.

Re: compile error: Block If without End If? in VB6

i tried your code, but it is still giving a msgbox that i need to enter h or v eventhough when i type in h or v in the textbox, and by the way what is the radio buttons ..i dont knw abt them.would that be helpful? how to do that?
thank you

Re: compile error: Block If without End If? in VB6

Start a new project, add a command button and a textbox named pol and add this code and you’ll see that it works just like I said it would.

I’ll post a radio button example in a few minutes.

Re: compile error: Block If without End If? in VB6

Here’s the radio button example.

Re: compile error: Block If without End If? in VB6

hi, thanks lot for for ur help marty, its workig now, and i hope it shouldnt be giving any problem in the future.
well one more thing i would like to ask at the end is, as u knw that i have 8 text boxes (in which i am varying values to see output), in wich the last one is in terms of p( u can see p from the equation «ap» in code at the end of code which is in terms of percentage) and in that text box i am varying a value of «p» from 0.001, 0.01, 0.1, 50 (which is in terms of percentage «p») so by varying those values i am getting a differnt numbers of output.
so first of all now what i want to do is to, display a graph on a new window, which should show on x-axis p values eventhough it is not entered by the user (which is from 0.001,0.01,0.1,50) and on y-axis from -200 to +500 (where output will lie somewhere between those values).
so, now if the user enters input in textbox 8 as 0.001 keeping other values constant (that is , the values in other textboxes) he gets the output like -6.78 for example and so on by changing p he gets output (frm negetive value of output to positive value) so i want a curve on a graphto show the variation of changing of values from 0.001 to 50 he gets different output as user keep changing the value of p as stated above from 0.001 to 50 the output varies. (u can have a look at my recent code next page)
i hope i am not confusing u, i really appreciate for your help u did so far, and i would like to thank you for that.

Last edited by hadimohammed365; May 4th, 2009 at 03:22 AM .

Источник

Hello-
Firstly, let me say that I am new to Visual Basic and Macros, so keep that in mind… Wink 

I am trying to write a Macro to run on a MS Outlook form.  I want to add a number of week days to the «Start Date» of a project depending on the project’s «Priority» (High Priority = 1 business day, Normal Priority = 2 business days, Low Priority = 5 business days).  I tried initially with the following «framework», but am repeatedly getting a ‘Block If Without End If’ Error when I compile (It highlights the ‘End Sub’ line).  I can’t Find it…….

Sub PriorityDueDate()
‘=================================================================
‘ Date : 1/17/2008
‘ Purpose : Update my due dates based on priority, keeping number
‘ of days restricted to working days (excluding Holidays).
‘=================================================================

Dim oMyTaskItem As TaskItem
Dim strPriority As String
Dim iNumber As Integer
Dim DaysBetween
Dim WeekendCount As Integer
Dim DayCount As Integer
Dim FinalHours
Dim EndHour
Dim StartHour
Dim StartStamp
Dim BHStart
Dim BHEnd
Dim x As Integer

On Error GoTo PriorityDueDate_Error

Set oMyTaskItem = Application.ActiveInspector.CurrentItem
StartStamp = oMyTaskItem.CreationTime   ‘Set start date to today
iNumber = Weekday(StartStamp, vbMonday)
strPriority = oMyTaskItem.Importance   ‘Set case for amount of days to push out the due date based on priority

‘Fix Start Date on weekends to Follwoing Monday
If iNumber = 6 Then
    StartDate = DateAdd(«d», 2, StartStamp)
End If
If iNumber = 7 Then
    StartDate = DateAdd(«d», 1, StartStamp)
End If
If iNumber < 6 Then
    StartDate = DateAdd(«d», 0, StartDate)
End If

Set BHStart = TimeSerial(8, 0, 0)
Set BHEnd = TimeSerial(17, 0, 0)

‘starting hour Adjustment
StartHour = StartStamp — Int(StartStamp) ‘Grabs the hour portion of the date
        If StartHour < BHStart Then ‘If before start time, convert to start hour
           StartDate = Int(StartStamp) + BHStart
        Else
        If StartHour > BHEnd Then    ‘Otherwise, convert to end hour
           StartDate = Int(StartStamp) + BHEnd
        Else
          StartDate = (StartStamp — 0)
       End If

‘Check for «None» due date
If oMyTaskItem.StartDate = «1/1/4501» Then
    MsgBox «You need to enter a valid Start Date before continuing»
Else

If iNumber = 1 Then
    Select Case strPriority
        Case olImportanceHigh
            oMyTaskItem.DueDate = DateAdd(«h», 24, oMyTaskItem.StartDate)
        Case olImportanceNormal
            oMyTaskItem.DueDate = DateAdd(«d», 3, oMyTaskItem.StartDate)
        Case olImportanceLow
            oMyTaskItem.DueDate = DateAdd(«d», 4, oMyTaskItem.StartDate)
    End Select
Else

If iNumber = 2 Then
    Select Case strPriority
        Case olImportanceHigh
            oMyTaskItem.DueDate = DateAdd(«h», 24, oMyTaskItem.StartDate)
        Case olImportanceNormal
            oMyTaskItem.DueDate = DateAdd(«d», 3, oMyTaskItem.StartDate)
        Case olImportanceLow
            oMyTaskItem.DueDate = DateAdd(«d», 6, oMyTaskItem.StartDate)
    End Select
Else

If iNumber = 3 Then
    Select Case strPriority
        Case olImportanceHigh
            oMyTaskItem.DueDate = DateAdd(«h», 24, oMyTaskItem.StartDate)
        Case olImportanceNormal
            oMyTaskItem.DueDate = DateAdd(«d», 5, oMyTaskItem.StartDate)
        Case olImportanceLow
            oMyTaskItem.DueDate = DateAdd(«d», 6, oMyTaskItem.StartDate)
    End Select
Else

If iNumber = 4 Then
    Select Case strPriority
        Case olImportanceHigh
            oMyTaskItem.DueDate = DateAdd(«h», 24, oMyTaskItem.StartDate)
        Case olImportanceNormal
            oMyTaskItem.DueDate = DateAdd(«d», 5, oMyTaskItem.StartDate)
        Case olImportanceLow
            oMyTaskItem.DueDate = DateAdd(«d», 6, oMyTaskItem.StartDate)
    End Select
Else

If iNumber = 5 Then
    Select Case strPriority
        Case olImportanceHigh
            oMyTaskItem.DueDate = DateAdd(«h», 72, oMyTaskItem.StartDate)
        Case olImportanceNormal
            oMyTaskItem.DueDate = DateAdd(«d», 5, oMyTaskItem.StartDate)
        Case olImportanceLow
            oMyTaskItem.DueDate = DateAdd(«d», 6, oMyTaskItem.StartDate)
    End Select
End If

                           ‘Adds Weekend Days to «Due Date»
WeekendCount = 0
DayCount = 0

If DueDays > 0 Then
    DaysBetween = ((DueDays) — (StartStamp))

   Select Case DaysBetween
        Case 0
            DueDays = (DueDays + 0)
        Case Else
            For x = ((StartStamp) + 1) To (DueDays) Step 1
                If iNumber > 5 Then
                    WeekendCount = WeekendCount + 1
           End If
        Next x
    End Select
End If

If WeekendCount > 0 Then
    oMyTaskItem.DueDate = DateAdd(«d», WeekendCount, oMyTaskItem.DueDate)
Else
    oMyTaskItem.DueDate = (oMyTaskItem.DueDate + 0)
End If

‘set some defaults
oMyTaskItem.ReminderSet = True
oMyTaskItem.ReminderTime = oMyTaskItem.DueDate ‘set reminder to due date
oMyTaskItem.ReminderPlaySound = True

‘clear memory
Set oMyTaskItem = Nothing

On Error GoTo 0
Exit Sub

PriorityDueDate_Error:

MsgBox «Error » & Err.Number & » (» & Err.Description & «) in procedure PriorityDueDateBasedOnPriority»

End Sub

Option Explicit

Sub checking()


Dim a()
Dim b()
Dim i As Long
Dim k As Long
Dim j As Long
Dim iLastRow As Long

a = Sheets("ÎèÎ").[A1].CurrentRegion.Value
b = Sheets("ÎÑÂ").[A1].CurrentRegion.Value
ReDim c(1 To UBound(a) + UBound(b), 1 To 12)


j = 1

For i = 1 To UBound(a)
    If a(i, 7) Like "*ÎÎÎ*" Or _
              a(i, 7) Like "*ÀÎ*" Or _
              a(i, 7) Like "*""*""*" Or _
              UBound(Split(a(i, 7), " "), 1) + 1 >= 5 Then
        For k = 1 To UBound(b)

         If a(i, 7) = b(k, 1) Then
                    c(j, 1) = a(i, 1)
                    c(j, 2) = a(i, 2)
                    c(j, 3) = a(i, 3)
                    c(j, 4) = a(i, 4)
                    c(j, 5) = a(i, 5)
                    c(j, 6) = a(i, 6)
                    c(j, 7) = a(i, 7)
                    c(j,  = a(i, 
                    c(j, 9) = b(k, 1)
                    c(j, 10) = b(k, 2)
                    c(j, 11) = b(k, 3)
                    c(j, 12) = b(k, 4)
                   j = j + 1
                   
        Else

            With CreateObject("VBScript.RegExp")
            .Pattern = "[à-ÿÀ-߸¨]+s[à-ÿÀ-߸¨]{1}[.]{1}[à-ÿÀ-߸¨]{1}[.]{1}$" 
                 If .Test(a(i, 7)) Then
        
                        If a(i, 7) = b(k, 1) _
                         Or Right(a(i, 7), 4) = Right(b(k, 1), 4) _
                         Or Left(a(i, 7), InStr(a(i, 7), " ") - 2) = Left(a(k, 1), InStr(a(i, 7), " ") - 2) Then
  

                    c(j, 1) = a(i, 1)
                    c(j, 2) = a(i, 2)
                    c(j, 3) = a(i, 3)
                    c(j, 4) = a(i, 4)
                    c(j, 5) = a(i, 5)
                    c(j, 6) = a(i, 6)
                    c(j, 7) = a(i, 7)
                    c(j,  = a(i, 
                    c(j, 9) = b(k, 1)
                    c(j, 10) = b(k, 2)
                    c(j, 11) = b(k, 3)
                    c(j, 12) = b(k, 4)
                   j = j + 1
   
End If
End If
End If

Next k
             Next i

Sheets("Èòîã").[A1].Resize(UBound(c), 12) = c


End Sub

  • #2

Put End If just before End Sub

  • #3

Hi and welcome to the Board
You are missing an End IF after the second line here

Rich (BB code):

Columns(lColNo - 1).Resize(, 2).Hidden = False
Columns(lColNo + 2).Hidden = False
 End If
End Sub

These mistakes will be easier to find if you Indent your code

  • #4

Note how the code is easier to follow with indentation..
Also try to use code tags when posting

Code:

Sub KS3_NewTask()
Dim lColNo As Long, blast As Boolean, bFound As Boolean
    For lColNo = 1 To 150
        If Columns(lColNo).Hidden Then
            If blast Then
                '---1ColNo is Col 2 of 4 for Task
                bFound = True
                Exit For
            End If
        End If
        blast = Columns(lColNo).Hidden
    Next lColNo
    If bFound Then
        '---unhide Cols 1,2,4 of Task
        Columns(lColNo - 1).Resize(, 2).Hidden = False
        Columns(lColNo + 2).Hidden = False
    End If
End Sub

  • #5

Thank you to everyone who replied. Something so simple has solved my problem :)

  • #6

Hi, I’m new to using VBA and hoping someone can show me the errors of my way.

I’m trying to create a function that will evaluate 2 cells (value1, value2) and whichever IS NOT BLANK it will do a vlookup to return an answer based on whichever cell (value1 or value2) is not empty. I wrote the following and keep getting Compile Error: Block If without End If and my Function line is highlighted in yellow.

Function LookupNotBlank(value1, value2)

If Not IsEmpty(value1.Value) Then​

LookupNotBlank = Application.worksheetfuction.vlookup(value1, Range(«‘Transition Proj’!$A$2:$B$10»), 2, 0)​

Else​

If Not IsEmpty(value2.Value) Then​

LookupNotBlank = Application.WorksheetFunction.vlookup(value2, Range(«‘Transition Proj’!$A$2:$B$10»), 2, 0)​

End If​

End Function

Last edited: Mar 4, 2014

RoryA

RoryA

MrExcel MVP, Moderator


  • #7

You have 2 If statements and only one closing End If:

Code:

Function LookupNotBlank(value1, value2)
If Not IsEmpty(value1.Value) Then
   LookupNotBlank = Application.worksheetfuction.vlookup(value1, Range("'Transition Proj'!$A$2:$B$10"), 2, 0)
Else
   If Not IsEmpty(value2.Value) Then
      LookupNotBlank = Application.WorksheetFunction.vlookup(value2, Range("'Transition Proj'!$A$2:$B$10"), 2, 0)
   End If
End If
End Function

  • #8

am having a problem with my coding — end if without block if error.

Private Sub CommandButton1_Click()
Dim PANo As Integer, DateRaised As Date, BriefDescription As String, DivisionorArea As String, ToAction As String, DueDate As Date, DateCompleted As Date, Comments As String
Worksheets(«sheet1»).Select
PANo = Range(«A2»)
DateRaised = Range(«A3»)
BriefDescription = Range(«A4»)
DivisionorArea = Range(«A5»)
ToAction = Range(«A6»)
DueDate = Range(«A7»)
DateCompleted = Range(«A8»)
Comments = Range(«A9»)
Worksheets(«sheet2»).Select
Worksheets(«sheet2»).Range(«A1»).Select
If Worksheets(«sheet2»).Range(«A1»).Offset(1, 0) <> «» Then
Worksheets(«sheet2»).Range(«A1»).End(xlDown).Select
End If
Acitvecell.Offset(1, 0).Select
ActiveCell.Value = PANo
Acitvecell.Offset(0, 1).Select
ActiveCell.Value = DateRaised
Acitvecell.Offset(0, 0, 1).Select
ActiveCell.Value = BriefDescription
Acitvecell.Offset(0, 0, 0, 1).Select
ActiveCell.Value = DivisionorArea
Acitvecell.Offset(0, 0, 0, 0, 1).Select
ActiveCell.Value = ToAction
Acitvecell.Offset(0, 0, 0, 0, 0, 1).Select
ActiveCell.Value = DueDate
Acitvecell.Offset(0, 0, 0, 0, 0, 0, 0, 1).Select
ActiveCell.Value = DateCompleted
Acitvecell.Offset(0, 0, 0, 0, 0, 0, 0, 0, 1).Select
ActiveCell.Value = Comments
Acitvecell.Offset(0, 0, 0, 0, 0, 0, 0, 0, 0, 1).Select
Worksheets(«sheet1»).Select
Worksheets(«sheet1»).Range(«A1»).Select

End Sub

RoryA

RoryA

MrExcel MVP, Moderator


  • #9

There is no missing End If in that code, but you have misspelled ActiveCell on several occasions.

Понравилась статья? Поделить с друзьями:
  • Error iduri pas 93 undeclared identifier tidipversion
  • Error idpixelunpackbuffer allocbufferobject allocsize 0
  • Error idnf at lba
  • Error idispatch error 3092
  • Error identifier printf is undefined