Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 425
Default ELSEIF for multiple if ??

I have this code that worked fine. Just needed to add a few more
conditions.
It was suggested that I use the code (2nd one below) with the elseif
statements, however that didnt quite work for me. I broke the elseif
statements down and ran them individually inside the same code (placed
a ' infront of the other lines and changed them to If statements) and
was successful.

Any suggestions....?

/////////////////////ORIGINAL CODE////////////////

Sub macro200()
Dim c As Range
Dim rng As Range
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Set rng = ws.Range("h3:h" & ws.Cells(ws.Rows.Count,
"h").End(xlUp).Row)
For Each c In rng
If c.Value = Range("ao2").Value Then
c.Offset(0, 1).Value = c.Offset(0, 33).Value
End If
Next c
End Sub


//////////SUGGESTED CODE ///////////////


Sub macro200()
Dim c As Range
Dim rng As Range
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Set rng = ws.Range("h3:h" & ws.Cells(ws.Rows.Count,
"h").End(xlUp).Row)
For Each c In rng
If c.Value = Range("ao2").Value Then
c.Offset(0, 1).Value = c.Offset(0, 33).Value
ElseIf c.Value = Range("ap2").Value Then
c.Offset(0, 2).Value = c.Offset(0, 34).Value
ElseIf c.Value = Range("aq2").Value Then
c.Offset(0, 3).Value = c.Offset(0, 35).Value
End If
Next c
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 638
Default ELSEIF for multiple if ??

I'm the one that provided you with that code. We need more
information on why it didn't work for you. There is nothing wrong
with the code itself, so it's failure could be caused by faulty data.
We need a little more info in order to provide more detailed code.
J.W. Aldridge wrote:
I have this code that worked fine. Just needed to add a few more
conditions.
It was suggested that I use the code (2nd one below) with the elseif
statements, however that didnt quite work for me. I broke the elseif
statements down and ran them individually inside the same code (placed
a ' infront of the other lines and changed them to If statements) and
was successful.

Any suggestions....?

/////////////////////ORIGINAL CODE////////////////

Sub macro200()
Dim c As Range
Dim rng As Range
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Set rng = ws.Range("h3:h" & ws.Cells(ws.Rows.Count,
"h").End(xlUp).Row)
For Each c In rng
If c.Value = Range("ao2").Value Then
c.Offset(0, 1).Value = c.Offset(0, 33).Value
End If
Next c
End Sub


//////////SUGGESTED CODE ///////////////


Sub macro200()
Dim c As Range
Dim rng As Range
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Set rng = ws.Range("h3:h" & ws.Cells(ws.Rows.Count,
"h").End(xlUp).Row)
For Each c In rng
If c.Value = Range("ao2").Value Then
c.Offset(0, 1).Value = c.Offset(0, 33).Value
ElseIf c.Value = Range("ap2").Value Then
c.Offset(0, 2).Value = c.Offset(0, 34).Value
ElseIf c.Value = Range("aq2").Value Then
c.Offset(0, 3).Value = c.Offset(0, 35).Value
End If
Next c
End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default ELSEIF for multiple if ??

The If-Then-ElseIf construction will execute only **one** of the conditions
(the first one that evaluates to True). You description makes it sound like
each condition can occur at the same time, so the ElseIf construct is not
appropriate construct for you to use... breaking into separate If-Then
blocks (as you did) allows each block's code to execute independently of the
other If-Then blocks.

Rick


"J.W. Aldridge" wrote in message
ups.com...
I have this code that worked fine. Just needed to add a few more
conditions.
It was suggested that I use the code (2nd one below) with the elseif
statements, however that didnt quite work for me. I broke the elseif
statements down and ran them individually inside the same code (placed
a ' infront of the other lines and changed them to If statements) and
was successful.

Any suggestions....?

/////////////////////ORIGINAL CODE////////////////

Sub macro200()
Dim c As Range
Dim rng As Range
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Set rng = ws.Range("h3:h" & ws.Cells(ws.Rows.Count,
"h").End(xlUp).Row)
For Each c In rng
If c.Value = Range("ao2").Value Then
c.Offset(0, 1).Value = c.Offset(0, 33).Value
End If
Next c
End Sub


//////////SUGGESTED CODE ///////////////


Sub macro200()
Dim c As Range
Dim rng As Range
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Set rng = ws.Range("h3:h" & ws.Cells(ws.Rows.Count,
"h").End(xlUp).Row)
For Each c In rng
If c.Value = Range("ao2").Value Then
c.Offset(0, 1).Value = c.Offset(0, 33).Value
ElseIf c.Value = Range("ap2").Value Then
c.Offset(0, 2).Value = c.Offset(0, 34).Value
ElseIf c.Value = Range("aq2").Value Then
c.Offset(0, 3).Value = c.Offset(0, 35).Value
End If
Next c
End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 425
Default ELSEIF for multiple if ??

Correct. More than one can be true. Any clue as to how to re-construct
it? I placed an ' in front of the codes to ensure my cell references
were correct, and they were (as it worked). So, now I am just looking
to get the code to work with multiple criteria.

Thanx

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default ELSEIF for multiple if ??

Correct. More than one can be true. Any clue as to how to re-construct
it? I placed an ' in front of the codes to ensure my cell references
were correct, and they were (as it worked). So, now I am just looking
to get the code to work with multiple criteria.


Assuming the set up code you posted is correct for you situation, just
change the For-Next loop to this...

For Each c In rng
If c.Value = Range("ao2").Value Then
c.Offset(0, 1).Value = c.Offset(0, 33).Value
End If
If c.Value = Range("ap2").Value Then
c.Offset(0, 2).Value = c.Offset(0, 34).Value
End If
If c.Value = Range("aq2").Value Then
c.Offset(0, 3).Value = c.Offset(0, 35).Value
End If
Next c

Rick



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 425
Default ELSEIF for multiple if ??



PURRRRFECTO!!!

Thanx!

(You guys are great!)

....when I get my first million, I lookin everyone up and buying them a
krystal burger

But yours Sir.... Is with Cheese!

: )

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
elseif Curt Excel Programming 0 March 28th 07 01:13 AM
IF..Then..ELSE.. ELSEIF ole_ Excel Programming 4 April 20th 05 03:59 PM
If, ElseIf mast Excel Programming 1 January 26th 05 12:11 PM
ElseIf tom1646 Excel Programming 4 October 19th 04 02:09 PM
If...Elseif...End If javab98 Excel Programming 2 July 19th 04 07:23 PM


All times are GMT +1. The time now is 03:28 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"