andrewb90
Well-known Member
- Joined
- Dec 16, 2009
- Messages
- 1,077
Hello all,
I feel like there should be a way to successfully do this, but I can't quite wrap my head around doing it properly. (I know I can do a very very long version to achieve what I want, but I'm trying to get better at writing more efficient code.)
Basically when the current row (defined as R) does not have the value of "yes" then I want to go backwards to find the next row (indicated in red) that has the value. Then I would need to find the corresponding date which would be in column b of the row. I wouldn't need to go back more than 10 rows, as a yes would appear well before that. In the event that a yes is not found, we could have a phrase "not found"
Now, there is one other condition, that I'm sure I could create separately, but if there is an elegant way to incorporate this, that'd be great.
If that value is 7 or less, then in the event that yes can't be found (which will only go back as far as 7 rows), then an alternate Else phrase would be needed.
Hopefully that makes sense. My thoughts were to try to do a loop that would step backwards by line, but as mentioned, I couldn't quite figure out how to incorporate what I wanted.
I feel like there should be a way to successfully do this, but I can't quite wrap my head around doing it properly. (I know I can do a very very long version to achieve what I want, but I'm trying to get better at writing more efficient code.)
Code:
If Sheets("Period" & P).Range("R" & R [COLOR=#ff0000]- 1[/COLOR]).Value = "yes" Then
Sheets("PDFPage").Range("L23").Value = Sheets("Period" [B][COLOR=#000080]& P-?)[/COLOR][/B].Range("B" & R).Value
Basically when the current row (defined as R) does not have the value of "yes" then I want to go backwards to find the next row (indicated in red) that has the value. Then I would need to find the corresponding date which would be in column b of the row. I wouldn't need to go back more than 10 rows, as a yes would appear well before that. In the event that a yes is not found, we could have a phrase "not found"
Now, there is one other condition, that I'm sure I could create separately, but if there is an elegant way to incorporate this, that'd be great.
Code:
If Sheets("PDFPage").Range("M1").Value <= 7 Then
If that value is 7 or less, then in the event that yes can't be found (which will only go back as far as 7 rows), then an alternate Else phrase would be needed.
Hopefully that makes sense. My thoughts were to try to do a loop that would step backwards by line, but as mentioned, I couldn't quite figure out how to incorporate what I wanted.