Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Do Until Loop Example

Status
Not open for further replies.

DSerr77

Technical User
Jul 21, 2004
42
US
I am trying to write a simple macro that looks through 20 values and colors the cells until it reaches the value that I have specified. I believe I am having a problem with the "with" section of this code. Any help is appreciated.

Sub ColorUntil()
Dim A As Range
Dim B As Integer
A = "A4"
B = A
Range("C1:C20").Select
Do
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Loop Until ActiveCell.Value = B
End Sub
 
What about this ?
Sub ColorUntil()
Dim C As Range
For Each C In Range("C1:C20")
With C.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
If C.Value = Range("A4").Value Then ExitFor
Next
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I get an Error "Sub or Function Not Defined" when it reaches ExitFor
 
Thanks PH That works great, however, I am really trying to understand how do until loops work. any suggested reading. I thought a simple piece of code would help me understand.
 
OOps, sorry for the typo :~/
As for a loop:
Sub ColorUntil()
Dim r As Long
r = 0
Do
r = r + 1
With Range("C" & r).Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Loop Until Range("C" & r).Value = Range("A4").Value Or r >= 20
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top