VBA loop to colour cells containing a number over a given value.

DoctorPG

New Member
Joined
Feb 18, 2019
Messages
2
Hello! Very new guy to VBA here. I have a pretty simple problem (i think) i try to solve with some VBA code (could use conditional formatting as well, but i want to use VBA) I Have one column with a lot of rows of nummeric data, around 5000. I want make a code so when the value in the cell is > 330, it colours the given cell red. I tried some with the for loop and if - else commands, but i just cant get the syntax right i think.

Any help is greatly appriciated!

Kind regards
PG
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi & welcome to MrExcel
One option is
Code:
Sub DoctorPG()
   Dim Cl As Range
   
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      If Cl.Value > 330 Then Cl.Interior.color = vbRed
   Next Cl
End Sub
But you could do this without a loop like
Code:
Sub DoctorPG()
   Range("A1").AutoFilter 1, ">330"
   Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlVisible).Interior.color = vbRed
   Range("A1").AutoFilter
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,217
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top