do loop

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Do loop

Do loop

resolvedResolved · Low Priority · Version 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:

Formula for last day of month

In some cases it is necessary to find the last day of a month for a given date. If you use the following formula, you can achieve this, ie; if you have a column of dates, use this formula to find the end of month for each day by using the fill handle. The formula is as follows, and assumes in this example that the first date in question is in cell C5, in any other cell type; =DATE(YEAR(C5),MONTH(C5)+1,1)-1

View all Excel hints and tips


Server loaded in 0.05 secs.