Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
 
Posts: n/a
Default Compile Error: Block If without End if

This is my first time using a Microsoft Excel Macro and I'm trying to
run the following code and keep getting the:

Compile Error: Block If without End if message and I can't figure out
why

Sub P()
'
' Macro1 Macro
' Changing FedEx Descriptions
'
' Keyboard Shortcut: Ctrl+Shift+X
'
If Range("K2:K999").Select = "02" Then

Range("M2:M999").Select = "FedEx Ground"
Else

If Range("K2:K999").Select = "03" Then
Range("M2:M999").Select = "FedEx 2Day"

Else

If Range("K2:K999").Select = "" Then
Range("M2:M999").Select = ""


End If
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave O
 
Posts: n/a
Default Compile Error: Block If without End if

Whenever you have an "If" line that ends in "Then" you need an End If.
The exception to this rule is, when there is only 1 instruction to
perform when the If is "true" you can group the If and the instruction
on a single line. So your code might look like this:

Sub P()
If Range("K2:K999").Select = "02" Then Range("M2:M999").Select =
"FedEx Ground"
If Range("K2:K999").Select = "03" Then Range("M2:M999").Select =
"FedEx 2Day"
If Range("K2:K999").Select = "" Then Range("M2:M999").Select = ""
End Sub

Or, you could use Else If, like this:

Sub P()
If Range("K2:K999").Select = "02" Then
Range("M2:M999").Select = "FedEx Ground"
ElseIf Range("K2:K999").Select = "03" Then
Range("M2:M999").Select = "FedEx 2Day"
ElseIf Range("K2:K999").Select = "" Then
Range("M2:M999").Select = ""
End If
End Sub

Or, just because there are numerous ways to skin this cat, you could
use a CASE structure.

  #3   Report Post  
Posted to microsoft.public.excel.misc
Doug Kanter
 
Posts: n/a
Default Compile Error: Block If without End if

I see 3 IF statements, but only one END IF.

wrote in message
oups.com...
This is my first time using a Microsoft Excel Macro and I'm trying to
run the following code and keep getting the:

Compile Error: Block If without End if message and I can't figure out
why

Sub P()
'
' Macro1 Macro
' Changing FedEx Descriptions
'
' Keyboard Shortcut: Ctrl+Shift+X
'
If Range("K2:K999").Select = "02" Then

Range("M2:M999").Select = "FedEx Ground"
Else

If Range("K2:K999").Select = "03" Then
Range("M2:M999").Select = "FedEx 2Day"

Else

If Range("K2:K999").Select = "" Then
Range("M2:M999").Select = ""


End If
End Sub



  #4   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Compile Error: Block If without End if

It looks like you want to check each of those values in column K.


Option Explicit
Sub P2()

dim LastRow as long
dim iRow as long

with activesheet
lastrow = .cells(.rows.count,"K").end(xlup).row

for irow = 2 to lastrow
if .range("K" & irow).value = "02" then
.range("M" & irow).value = "FedEx Ground"
elseif .range("K" & irow).value = "03" then
.range("M" & irow).value = "FedEx 2Day"
elseif .range("K" & irow).value = "" then
.range("M" & irow).value = ""
end if
next irow
end with
End Sub

This actually looks for the Text "02" in the cell--not the number formatted to
have leading 0's.

That could be very important.

You may want to change:
if .range("K" & irow).value = "02" then
to
if .range("K" & irow).value = 2 then
(and same with "03" and 3)


wrote:

This is my first time using a Microsoft Excel Macro and I'm trying to
run the following code and keep getting the:

Compile Error: Block If without End if message and I can't figure out
why

Sub P()
'
' Macro1 Macro
' Changing FedEx Descriptions
'
' Keyboard Shortcut: Ctrl+Shift+X
'
If Range("K2:K999").Select = "02" Then

Range("M2:M999").Select = "FedEx Ground"
Else

If Range("K2:K999").Select = "03" Then
Range("M2:M999").Select = "FedEx 2Day"

Else

If Range("K2:K999").Select = "" Then
Range("M2:M999").Select = ""


End If
End Sub


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Compile Error: Block If without End if

But it's not nice to multipost to separate newsgroups. It can potentially waste
the time of the responders.

And it means that you have to check each forum for replies. And even worse, you
don't get the added benefit of one responder enhancing another responder's
reply.

If you really think you have to post to multiple newsgroups (I don't think you
do), you should post one message, but include all the newsgroups in the header.

wrote:

Horrrrrrraaaaaaaaayyyyyy!!!!!!! Out of all the different forums I put
this question in, it seems your the only one with an answer that really
works. Thanks so much. I might be able to go home early today. Muchos
Gracias senoir!!!!!!!!!!


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
excel compile error. Only some computers? Bob Aylward Excel Discussion (Misc queries) 0 December 13th 05 07:16 PM
compile error in hidden module: autoexecnew - how do I get rid? Joespo Charts and Charting in Excel 1 November 10th 05 03:41 PM
Compile error in hidden module arjay Excel Discussion (Misc queries) 5 September 16th 05 12:47 AM
Compile Errors with Different versions of Excel PStephens Excel Discussion (Misc queries) 2 March 14th 05 04:27 PM


All times are GMT +1. The time now is 06:37 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"