Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

if function

ResolvedVersion 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:

Display pictures on Chart Data Point

Replacing a single chart data point bar with a picture.
Step 1: Left click on a bar. Then, wait, and do a second single click on the bar. This will select just one data point.

Step 2: Right click on the bar and select Format Data Point.

Step 3: On the fill effects tab, choose a picture. Browse for a picture for that bar. Indicate if you want it to be stretched or stacked. Repeat for each bar.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.