If-Then VBE

Oaktree

MrExcel MVP
Joined
Jun 20, 2002
Messages
8,120
Office Version
  1. 365
In a VBE If-Then statement, how can I get Excel to do multiple "Thens"? In the code below, what I'm TRYING to do is, when three rows in a cell match certain criteria, copy elements of that row to a different sheet. But, I can't get the line after the "Then" to abide by the IF statement (i.e. the second line is ALWAYS copied, regardless of whether the row matches the criteria of the IF statement). Can I use "AND" or something to accomplish this?

Thanks,
Oaktree

If Sheets("Log").Range("FirstYear").Offset(Row - 3, 1).Value = Range("DOTWeek").Value _
And Sheets("Log").Range("A3").Offset(Row - 3, 0) = Range("DOTYear").Value _
And Sheets("Log").Range("A3").Offset(Row - 3, 2) = Range("DOTIsDOT").Value _
Then Sheets("Reports").Range("A8").Offset(Row - 3, 0).Value = Sheets("Log").Range("FirstYear").Offset(Row - 3, 1)
Sheets("Reports").Range("A8").Offset(Row - 3, 1).Value = Sheets("Log").Range("FirstYear").Offset(Row - 3, :cool:
This message was edited by Oaktree on 2002-06-21 10:29
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I cannot test your code, but I think if you add an "Else" condition, then everything between the "Then" and the "Else" should work according to the test.

If...Then
Your code...Else GoTo myEnd
other parts of your macro not related to the "If" test
myEnd:

End Sub

This should work. In some structures I have found that a target also helps, like:

myEnd:
End

End Sub

I hope this helps. JSW
 
Upvote 0
I'm not sure if that would work in the for loop I'm also embedded in. The whole macro thus far is:

It wouldn't compile with the else statement in there...

Thanks again,
Oaktree

Sub GetData2()
PasteRow = 3
For LookupRow = 3 To Sheets("Log").Range("Counter").Value + 2
If Sheets("Log").Range("FirstYear").Offset(LookupRow - 3, 1).Value = Range("DOTWeek").Value _
And Sheets("Log").Range("A3").Offset(LookupRow - 3, 0) = Range("DOTYear").Value _
And Sheets("Log").Range("A3").Offset(LookupRow - 3, 2) = Range("DOTIsDOT").Value _
Then Sheets("DOT & Non-DOT Reports").Range("A8").Offset(PasteRow - 3, 0).Value = Sheets("Log").Range("FirstYear").Offset(LookupRow - 3, 1)
Sheets("DOT & Non-DOT Reports").Range("A8").Offset(PasteRow - 3, 1).Value = Sheets("Log").Range("FirstYear").Offset(LookupRow - 3, :cool:
Sheets("DOT & Non-DOT Reports").Range("A8").Offset(PasteRow - 3, 2).Value = Sheets("Log").Range("FirstYear").Offset(LookupRow - 3, 6)
Sheets("DOT & Non-DOT Reports").Range("A8").Offset(PasteRow - 3, 3).Value = Sheets("Log").Range("FirstYear").Offset(LookupRow - 3, 12)
Sheets("DOT & Non-DOT Reports").Range("A8").Offset(PasteRow - 3, 4).Value = Sheets("Log").Range("FirstYear").Offset(LookupRow - 3, 3)
Sheets("DOT & Non-DOT Reports").Range("A8").Offset(PasteRow - 3, 5).Value = Sheets("Log").Range("FirstYear").Offset(LookupRow - 3, 4)
Sheets("DOT & Non-DOT Reports").Range("A8").Offset(PasteRow - 3, 6).Value = Sheets("Log").Range("FirstYear").Offset(LookupRow - 3, 13)
Sheets("DOT & Non-DOT Reports").Range("A8").Offset(PasteRow - 3, 7).Value = Sheets("Log").Range("FirstYear").Offset(LookupRow - 3, 14)
Sheets("DOT & Non-DOT Reports").Range("A8").Offset(PasteRow - 3, :cool:.Value = Sheets("Log").Range("FirstYear").Offset(LookupRow - 3, 15)
PasteRow = PasteRow + 1
Else: GoTo MyEnd
End If
MyEnd:
End

Next LookupRow
End Sub
 
Upvote 0
Re you looking to nest the statements like this?

If Sheets("Log").Range("FirstYear").Offset(Row - 3, 1).Value = Range("DOTWeek").Value Then
If Sheets("Log").Range("A3").Offset(Row - 3, 0) = Range("DOTYear").Value Then
If Sheets("Log").Range("A3").Offset(Row - 3, 2) = Range("DOTIsDOT").Value Then ' your conditions
Sheets("Reports").Range("A8").Offset(Row - 3, 0).Value = Sheets("Log").Range("FirstYear").Offset(Row - 3, 1)
Sheets("Reports").Range("A8").Offset(Row - 3, 1).Value = Sheets("Log").Range("FirstYear").Offset(Row - 3,
Else
End If
Else
End If
Else
End If


Yours in EXCELent Frustration
KniteMare
 
Upvote 0
One error is that you are using the "one-liner" version of if/then:

If This Then That

This version doesn't need an End If

What you want to use is:

If This Then
This
That
The other
End if

Your original code thinks the only thing associated with the If statement is the first line after it (since you use a continuation). The if test is finished by the time you get to the second line.

I'm also not sure why you are using an else statement just to branch out of the If. You could eliminate it and get the same results.

Next, I see an End statement where it looks like there should be a Next. Could just be a typo.

Finally, this board converts 8) to a smiley face. You may want to check the disable smiles on posts with code in them...

K
This message was edited by kkknie on 2002-06-21 10:55
 
Upvote 0
Thanks for the help. I was missing the EndIf at the end. But, hey, at least I only wasted a few hours trying to figure that one out. :-) Seems to work now!

Thanks, guys,

Oaktree
 
Upvote 0

Forum statistics

Threads
1,222,682
Messages
6,167,620
Members
452,124
Latest member
lozdemr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top