if function

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » IF Function

IF Function

resolvedResolved · 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 courses

 

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
What does 'Resolved' mean?

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 gridlines

Excel 2010 allows you to change the colour of grid lines instead of keeping them in boring black.

Select the File tab on the Ribbon, click Options, click Advanced, scroll down to ''Display options for this worksheet.'' Next to ''Gridline colour,'' choose your favourite colour, then once you've done this, click OK. Easy!

View all Excel hints and tips


Server loaded in 0.05 secs.