Selecting Data
Do-loop
1
REVISION
2
File:
Do-loop: Selecting Data DoLoop_example.xlsx
Expected to learn:
Do-while loop
• Description: If-then-else
• Create a macro that scans data [Link]
VbRed
in column C starting from row
number 4 using a Do-While Comments
loop. If the value in the cell is Indentation
greater than 100%, highlight [Link]
the cell in red. Set
RGB(255, 0, 0)
• Learn from the given program
(generated by ChatGPT) and Self-learning:
improve your code. Do Loop While
Do Loop Until
Do Until Loop
Cells() will return a
Range Object
Range Object
Sub test_DoWhileLoop()
Dim i As Long
i=4
Do While Cells(i, 1).Value <> ""
If Cells(i, 3).Value >= 1 Then
Cells(i, 3).[Link] = vbRed
End If
i=i+1
Loop
End Sub Color value
Remark: Background color
Cells( y , x )
Where x, and y are coordinates of a worksheet
Recommendation: Cells( y , col )
4
Sub HighlightValuesOver100Percent()
Dim ws As Worksheet
Dim rowNum As Long Row number:
Long
Code
Dim currentValue As Double
' Set the worksheet variable to the desired sheet
Set ws = [Link]("WorkingWorkSheet")
' Start scanning from row 4 in column C using a Do-While loop
rowNum = 4
Comment
Indentation ' Assuming Column C corresponds to the third column
Do While [Link](rowNum, "C").Value <> ""
' Retrieve the current cell value in column C
currentValue = [Link](rowNum, "C").Value
' Check if the value is greater than 100%
' Assuming 100% is represented as 1 in Excel Color:
If currentValue > 1 Then RGB(255, 0, 0)
' Highlight the cell in RED
[Link](rowNum, "C").[Link] = RGB(255, 0, 0)
End If
' Move to the next row
rowNum = rowNum + 1
Loop
End Sub