close

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(quot;K2:K999quot;).Select = quot;02quot; Then

Range(quot;M2:M999quot;).Select = quot;FedEx Groundquot;
Else

If Range(quot;K2:K999quot;).Select = quot;03quot; Then
Range(quot;M2:M999quot;).Select = quot;FedEx 2Dayquot;

Else

If Range(quot;K2:K999quot;).Select = quot;quot; Then
Range(quot;M2:M999quot;).Select = quot;quot;End If
End SubWhenever you have an quot;Ifquot; line that ends in quot;Thenquot; you need an End If.
The exception to this rule is, when there is only 1 instruction to
perform when the If is quot;truequot; you can group the If and the instruction
on a single line. So your code might look like this:

Sub P()
If Range(quot;K2:K999quot;).Select = quot;02quot; Then Range(quot;M2:M999quot;).Select =
quot;FedEx Groundquot;
If Range(quot;K2:K999quot;).Select = quot;03quot; Then Range(quot;M2:M999quot;).Select =
quot;FedEx 2Dayquot;
If Range(quot;K2:K999quot;).Select = quot;quot; Then Range(quot;M2:M999quot;).Select = quot;quot;
End Sub

Or, you could use Else If, like this:

Sub P()
If Range(quot;K2:K999quot;).Select = quot;02quot; Then
Range(quot;M2:M999quot;).Select = quot;FedEx Groundquot;
ElseIf Range(quot;K2:K999quot;).Select = quot;03quot; Then
Range(quot;M2:M999quot;).Select = quot;FedEx 2Dayquot;
ElseIf Range(quot;K2:K999quot;).Select = quot;quot; Then
Range(quot;M2:M999quot;).Select = quot;quot;
End If
End Sub

Or, just because there are numerous ways to skin this cat, you could
use a CASE structure.I see 3 IF statements, but only one END IF.

gt; wrote in message oups.com...
gt; This is my first time using a Microsoft Excel Macro and I'm trying to
gt; run the following code and keep getting the:
gt;
gt; Compile Error: Block If without End if message and I can't figure out
gt; why
gt;
gt; Sub P()
gt; '
gt; ' Macro1 Macro
gt; ' Changing FedEx Descriptions
gt; '
gt; ' Keyboard Shortcut: Ctrl Shift X
gt; '
gt; If Range(quot;K2:K999quot;).Select = quot;02quot; Then
gt;
gt; Range(quot;M2:M999quot;).Select = quot;FedEx Groundquot;
gt; Else
gt;
gt; If Range(quot;K2:K999quot;).Select = quot;03quot; Then
gt; Range(quot;M2:M999quot;).Select = quot;FedEx 2Dayquot;
gt;
gt; Else
gt;
gt; If Range(quot;K2:K999quot;).Select = quot;quot; Then
gt; Range(quot;M2:M999quot;).Select = quot;quot;
gt;
gt;
gt; End If
gt; End Sub
gt;
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,quot;Kquot;).end(xlup).row

for irow = 2 to lastrow
if .range(quot;Kquot; amp; irow).value = quot;02quot; then
.range(quot;Mquot; amp; irow).value = quot;FedEx Groundquot;
elseif .range(quot;Kquot; amp; irow).value = quot;03quot; then
.range(quot;Mquot; amp; irow).value = quot;FedEx 2Dayquot;
elseif .range(quot;Kquot; amp; irow).value = quot;quot; then
.range(quot;Mquot; amp; irow).value = quot;quot;
end if
next irow
end with
End Sub

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

That could be very important.

You may want to change:
if .range(quot;Kquot; amp; irow).value = quot;02quot; then
to
if .range(quot;Kquot; amp; irow).value = 2 then
(and same with quot;03quot; and 3)

wrote:
gt;
gt; This is my first time using a Microsoft Excel Macro and I'm trying to
gt; run the following code and keep getting the:
gt;
gt; Compile Error: Block If without End if message and I can't figure out
gt; why
gt;
gt; Sub P()
gt; '
gt; ' Macro1 Macro
gt; ' Changing FedEx Descriptions
gt; '
gt; ' Keyboard Shortcut: Ctrl Shift X
gt; '
gt; If Range(quot;K2:K999quot;).Select = quot;02quot; Then
gt;
gt; Range(quot;M2:M999quot;).Select = quot;FedEx Groundquot;
gt; Else
gt;
gt; If Range(quot;K2:K999quot;).Select = quot;03quot; Then
gt; Range(quot;M2:M999quot;).Select = quot;FedEx 2Dayquot;
gt;
gt; Else
gt;
gt; If Range(quot;K2:K999quot;).Select = quot;quot; Then
gt; Range(quot;M2:M999quot;).Select = quot;quot;
gt;
gt;
gt; End If
gt; End Sub

--

Dave Peterson

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:
gt;
gt; Horrrrrrraaaaaaaaayyyyyy!!!!!!! Out of all the different forums I put
gt; this question in, it seems your the only one with an answer that really
gt; works. Thanks so much. I might be able to go home early today. Muchos
gt; Gracias senoir!!!!!!!!!!

--

Dave Peterson

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 software 的頭像
    software

    software

    software 發表在 痞客邦 留言(0) 人氣()