98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » IF Function
IF Function
Resolved · High Priority · Version 2010
Freddie has attended:
Excel VBA Introduction course
IF Function
Hi Guys
Are you able to help with the below?
The formula only works for one cell but i only want it to work for every cell that is populated in the column before the range - that is activecell.offset(0,-1). Where have i misplaced the ActiveCell.Offset(1, 0).Select?
Range("O2").Select
If ActiveCell.Offset(0, -1) <= 1 Then
ActiveCell.Value = "Overnight"
ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.Offset(0, -1) = ""
Loop
ElseIf ActiveCell.Offset(0, -1) <= 7 Then ActiveCell.Value = "1 week"
ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.Offset(0, -1) = ""
Loop
ElseIf ActiveCell.Offset(0, -1) <= 31 Then ActiveCell.Value = "1 month"
ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.Offset(0, -1) = ""
Loop
ElseIf ActiveCell.Offset(0, -1) >= 92 Then ActiveCell.Value = "3 months"
ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.Offset(0, -1) = ""
Loop
Else: ActiveCell.Value = "More than 6 months"
ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(1, 0).Select
Do Until ActiveCell.Offset(0, -1) = ""
Loop
End If
THanks
Freddie
RE: IF Function
Hi Freddie,
Thank you for your post. Please have a look at my code below which seems to work.
Range("o2").Select
Do Until ActiveCell.Offset(0, -1) = ""
If ActiveCell.Offset(0, -1) <= 1 Then
ActiveCell.Value = "Overnight"
ElseIf ActiveCell.Offset(0, -1) <= 7 Then
ActiveCell.Value = "1 week"
ElseIf ActiveCell.Offset(0, -1) <= 31 Then
ActiveCell.Value = "1 month"
ElseIf ActiveCell.Offset(0, -1) >= 92 Then
ActiveCell.Value = "3 months"
Else: ActiveCell.Value = "More than 6 months"
End If
ActiveCell.Offset(1, 0).Select
Loop
It's best to use just one loop when using an If Statement such as this. It lets VBA allocate the correct result from the choices before moving to the next record.
I hope this helps.
Kind regards
Marius Barnard
STL
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Brighten up your Excel 2010 Spreadsheet by changing the colours of the gridlinesExcel 2010 allows you to change the colour of grid lines instead of keeping them in boring black. |