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

do loop

ResolvedVersion 2010

Do loop

I have written the following macro that highlights the active cell if it has the value "Wednesday" and if another cell (same row and three columns across) has a time value > 12pm.
Would you be able to advise why this does not work?

Sub WedsPM()
Range("I2").Select
Do Until ActiveCell = ""
If ActiveCell.Value = "Wednesday" And ActiveCell.Offset(0, 3).Value > Time(12, 0, 0) Then
ActiveCell.Interior.ColorIndex = 27
End If

ActiveCell.Offset(1, 0).Select
Loop

End Sub

RE: Do loop

Hi Natasha,

Thank you for your post. The code below worked for me. I changed the time reference to a value of 0.5, which means 12 PM (halfway through 1 day). Make sure your time cells are formatted as time.

Sub WedsPM()

Range("I2").Select
Do Until ActiveCell = ""
If ActiveCell.Value = "Wednesday" And ActiveCell.Offset(0, 3).Value > 0.5 Then
ActiveCell.Interior.ColorIndex = 27
End If

ActiveCell.Offset(1, 0).Select
Loop

End Sub

Kind regards
Marius Barnard
STL

RE: Do loop

Many thanks for your help Marius.
This now works for me :)

Kind regards
Natasha

 

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:

Naming and Using Constants

Constants make calculations easier so worksheets are more easily understood. Constant values also need to be given relevant and memorably names. It is also easier to change the value of a constant.

For example:
Instead of entering 17.5% in each cell when you generate a VAT amount you could name a Constant "VAT" and assigning a "0.175" value to it. To do this:

From the 'Insert' menu select 'Name', then select 'Define'.

Enter the constant

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.09 secs.