0% found this document useful (0 votes)
13 views

Do-loop

The document provides instructions for creating a macro in Excel that uses a Do-While loop to scan data in column C, starting from row 4. If the value in a cell is greater than 100%, the cell is highlighted in red. It includes example code and recommendations for improving the macro.

Uploaded by

ming lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Do-loop

The document provides instructions for creating a macro in Excel that uses a Do-While loop to scan data in column C, starting from row 4. If the value in a cell is greater than 100%, the cell is highlighted in red. It includes example code and recommendations for improving the macro.

Uploaded by

ming lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 Interior.Color
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 ThisWorkbook.Sheets
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).Interior.Color = 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 = ThisWorkbook.Sheets("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 ws.Cells(rowNum, "C").Value <> ""

' Retrieve the current cell value in column C


currentValue = ws.Cells(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
ws.Cells(rowNum, "C").Interior.Color = RGB(255, 0, 0)

End If

' Move to the next row


rowNum = rowNum + 1

Loop
End Sub

You might also like