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.
Contents
- The IF statement and its various forms
- The Compile Error “End If without Block If:
- Rule 1: End If with single line statement
- Rule 2: Extra End If statements
- Rule 3: Forgetting part of your deleted code
- Summary
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.
- A simple If Block
- An If – Else block
- An Else-If block
- 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;lt;condition&amp;gt; Then &amp;lt;code&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;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;lt; 60 Then roi = 6 If strgen = "Female" And custage &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 am currently running the below loop to pull out information from another spreadsheet, but keep getting the following error message Compile error: End If without block If, at
ElseIf cel.Offset(0, 8).Value = "" Then wshT.Cells(r, 14).Value = "Physical"
What could be causing it and how do I remediate it? My code below:
' Loop though cells in column A on main.xlsm
For r = 1 To m
' Can we find the value in column A
Set cel = wshS.Columns(3).Find(What:=wshT.Cells(r, 1).Value, _
LookAt:=xlWhole, MatchCase:=False)
If Not cel Is Nothing Then
If cel.Offset(0, 8).Value = "Yes" Then wshT.Cells(r, 14).Value = "Virtual"
ElseIf cel.Offset(0, 8).Value = "" Then wshT.Cells(r, 14).Value = "Physical"
Else: End If
End If
Next r
asked Mar 22, 2014 at 16:46
methuselahmethuselah
12.5k43 gold badges158 silver badges301 bronze badges
2
Further to my comments above, change your code to
If Not cel Is Nothing Then
If cel.Offset(0, 8).Value = "Yes" Then wshT.Cells(r, 14).Value = "Virtual"
If cel.Offset(0, 8).Value = "" Then wshT.Cells(r, 14).Value = "Physical"
End If
or to this
If Not cel Is Nothing Then
If cel.Offset(0, 8).Value = "Yes" Then
wshT.Cells(r, 14).Value = "Virtual"
ElseIf cel.Offset(0, 8).Value = "" Then
wshT.Cells(r, 14).Value = "Physical"
End If
End If
For the syntax of IF/EndIf
, see the below
'~~> Multiple-line syntax:
If condition [ Then ]
[ statements ]
[ ElseIf elseifcondition [ Then ]
[ elseifstatements ] ]
[ Else
[ elsestatements ] ]
End If
'~~> Single-line syntax:
If Condition Then [ statements ] [ Else [ elsestatements ] ]
answered Mar 22, 2014 at 16:50
Siddharth RoutSiddharth Rout
146k17 gold badges206 silver badges250 bronze badges
0
Содержание
- 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
- The IF statement and its various forms
- The Compile Error “End If without Block If:
- Rule 1: End If with single line statement
- Rule 2: Extra End If statements
- Rule 3: Forgetting part of your deleted code
- Summary
- Compile error end if without block if
- Asked by:
- Question
- Compile Error: Block If without End If .
- APPPRO
- Excel Facts
- DonkeyOte
- Thread: compile error: Block If without End If? in VB6
- compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- Re: compile error: Block If without End If? in VB6
- 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.
- A simple If Block
- An If – Else block
- An Else-If block
- 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 .
Источник
Hi Guys — not sure i have put this in the correct forum but we shall give it a go
I am working on a master excel spreadsheet that identifies its own file path, searches for a folder called EO_121DATA in that file path and then takes a specific sheet (121 Data) from each workbook in the folder and create a copy of it in the master, labelling
it the name of person which is in cell B2.
It will be updated every month and it needs to remove sheets when files are deleted and add sheets when new ones are created.
I think I have written something that will work, and once I know the script is fine I will create a button to initiate it.
When I run the script I get the error: Compile Error Block If without End If and it highlights the End Sub command line at the bottom.
Here’s what I’ve written so far, when its in use it will be running on Excel 2010, though I am currently editing it at home on 2007.
Sub Import_Team() Dim wsSrc As Worksheet ' Source Sheet Dim wbSrc As Workbook ' Source Workbook Dim wbDst As Workbook ' Destination Workbook Dim shDst As Worksheet ' Destination Worksheet Dim useWS As Boolean ' Used to indicate Start and End Sheets Dim ManagerPath As String ' Find manager workbook file path Dim OfficerPath As String ' Find folder EO_121DATA in team folder for the manager Dim srcFilename As String ' Finds workbooks in EO_121Data 'define the file paths ManagerPath = Application.ThisWorkbook.Path ' Finds the filepath of the workbook running the macro OfficerPath = Dir(ManagerPath & "EO_121Data") ' Adds the EO_121DATA to the above filepath, as long as the manager file is kept in the folder that contains EO_121DATA it will find the relevent files srcFilename = Dir(OfficerPath & "*.xls*", vbNormal) ' Finds any excel workbooks in the EO_121DATA file, the asterisk are wildcards so any excel format and sheet name will be found useWS = False If OfficerPath = "" Then MsgBox "There is currently no folder containing Enquiry Officer documents. Please create a file called EO_121DATA in the same file as your manager sheet and copy your teams files into it.", vbOKOnly, "No Destination File" Exit Sub ' If there is no EO_121DATA folder the manager will be told and the sub will stop. Do Until OfficerPath <> "" If srcFilename = "" Then MsgBox "There are currently no team data sheets saved in the folder EO_121DATA.", vbOKOnly, "No Data Available" Exit Sub ' If there are no files in EO_121DATA it will stop the macro and do nothing, a message box advising no files were found in the specified folder will show. Do Until srcFilename <> "" ' Stop the screen from showing each workbook being opened checked and closed Application.DisplayAlerts = False Application.EnableEvents = False Application.ScreenUpdating = False For Each shDst In ActiveWorkbook.Sheets ' Clears old worksheets in the book If shDst.Name = "End" Then useWS = False ' Turns off sheet processing End If If shDst.Name = "Start" Then useWS = True ' Turns on sheet processing End If If useWS = True And shDst.Name <> "Start" Then shDst.Delete ' Deletes old sheets ready to be updated, run the macro each month as officer sheets are updated End If Next shDst ' Loop through the source files in EO_121DATA While srcFilename <> "" TabName = Replace(srcFilename, "Tracker.xlsm", "") ' Creates a tab Worksheets.Add Before:=Sheets("End") ActiveSheet.Name = TabName Set shDst = ActiveSheet ' Copy from source workbook to destination workbook Set wbSrc = Workbooks.Open(srcFilename) Set shSrc = wbSrc.Sheets("121 Data") shDst.Name = shSrc.Range("$B$2") shSrc.Cells.Copy shDst.Range("A1").PasteSpecial xlPasteValues shDst.Columns.AutoFit wbSrc.Close savechanges:=False ' Get the next file srcFilename = Dir() Wend Loop Sheets("Team Average").Select ' re-enables the screen so it shows files being opened and closed Application.DisplayAlerts = True Application.EnableEvents = True Application.ScreenUpdating = True End Sub
Thanks in advance for your help.
-
Moved by
Friday, June 16, 2017 8:25 AM
Moved from VB.NET
-
#2
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 A=a then a1=a
If the THEN and subsequent statement are on one line you do not need an End If.
dk
MrExcel MVP
-
#3
Each time you use an If statement that over more than one line, you must terminate it with an End If.
There are several ways you could change your code:-
First, terminate each individual If statement with an End If.
Code:
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
End If
If Sheets("ENTRY FORM").Range("k73") = 0 Then
Sheets("data").Rows("14:14").Clear
End If
If Sheets("ENTRY FORM").Range("k72") = 0 Then
Sheets("data").Rows("13:13").Clear
End If
If Sheets("ENTRY FORM").Range("k71") = 0 Then
Sheets("data").Rows("12:12").Clear
End If
If Sheets("ENTRY FORM").Range("k70") = 0 Then
Sheets("data").Rows("11:11").Clear
End If
If Sheets("ENTRY FORM").Range("k69") = 0 Then
Sheets("data").Rows("10:10").Clear
End If
If Sheets("ENTRY FORM").Range("k68") = 0 Then
Sheets("data").Rows("9:9").Clear
End If
If Sheets("ENTRY FORM").Range("k67") = 0 Then
Sheets("data").Rows("8:8").Clear
End If
If Sheets("ENTRY FORM").Range("k66") = 0 Then
Sheets("data").Rows("7:7").Clear
End If
End Sub
Alternatively, you could just put each If statement on one line:-
Code:
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 Sub
The _ character means that VBA treats those lines as one, but allows you to spread long lines over more than one row. In this case, you don’t need an End If at all.
_________________<font face=»Impact»>Hope this helps,
Dan</font>
This message was edited by dk on 2002-10-21 09:21
-
#4
Hi APPPRO,
The reason for the error is that the If statements should be in one line each, no return after the THEN statement.You can also delete the End If statement before the End Sub
Regards,
Bill
-
#5
Or another way of achieving your goal:
Code:
Sub Test()
Dim c As Range
Dim x As Integer
For Each c In Sheets("ENTRY FORM").Range("K66:K75")
If c.Value = 0 Then
x = c.Row - 59
Sheets("data").Rows(x).Clear
End If
Next c
End Sub
-
#6
BILL:
That did it thanks.
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
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
asked May 7, 2019 at 15:18
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
@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 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