How to Loop through code

Eurekaonide

Active Member
Joined
Feb 1, 2010
Messages
433
Hi Please can you help me expand this code range and loop?

I have 2 worksheets one is called Compare and one is called Directorate Resources_2
On the Compare I have a range of cells/columns/rows that are returning either 0 or 1
If it sees a 1 I want the cell in Directorate Resources_2 to be coloured Purple and Yellow text. (I have tried this through conditional formatting but because there are so many different things happening between the sheets before this step the conditional formatting keeps changing its rows etc even with $ in front)
So I feel best if I hard code it in.

I have the below which works fine but I need it to go down the rows which I dont know how long the data set will be and also cover the following columns:
Compare Sheet Columns are AP2 to BA
Directorate Resources_2 sheet Columns Q4 to AB

can you advise on the best way to loop through this formula/conditional formatting please?

Sub Test2()
Sheets("compare").Select

Range("AP2").Select

If Value = 1 Then
Sheets("Directorate Resources_2").Activate
Range("Q4").Select

With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 10498160
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.Color = -16711681
.TintAndShade = 0
End With
Selection.Font.Bold = True
End If
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Give this a try:
VBA Code:
Sub Test2()
Dim lRow As Long
Dim ws1 As Worksheet, ws2 As Worksheet
Dim c As Range

'Set worksheet variables
Set ws1 = Sheets("Compare")
Set ws2 = Sheets("Directorate Resources_2")

'Identify last used row in range AP:BA on sheet Compare
lRow = ws1.Range("AP" & Rows.Count).End(xlUp).Row

'Loop through each cell in the used range on sheet Compare and check values
'then format corresponding cells on sheet Directorate Resources_2 as necessary
For Each c In ws1.Range("AP2:BA" & lRow)
    If c.Value = 1 Then
        With ws2.Cells(c.Row + 2, c.Column - 25)
            .Interior.Pattern = xlSolid
            .Interior.PatternColorIndex = xlAutomatic
            .Interior.Color = 10498160
            .Interior.TintAndShade = 0
            .Interior.PatternTintAndShade = 0
            .Font.Color = -16711681
            .Font.TintAndShade = 0
            .Font.Bold = True
        End With
    End If
Next c
End Sub
 
Upvote 0
Solution
Give this a try:
VBA Code:
Sub Test2()
Dim lRow As Long
Dim ws1 As Worksheet, ws2 As Worksheet
Dim c As Range

'Set worksheet variables
Set ws1 = Sheets("Compare")
Set ws2 = Sheets("Directorate Resources_2")

'Identify last used row in range AP:BA on sheet Compare
lRow = ws1.Range("AP" & Rows.Count).End(xlUp).Row

'Loop through each cell in the used range on sheet Compare and check values
'then format corresponding cells on sheet Directorate Resources_2 as necessary
For Each c In ws1.Range("AP2:BA" & lRow)
    If c.Value = 1 Then
        With ws2.Cells(c.Row + 2, c.Column - 25)
            .Interior.Pattern = xlSolid
            .Interior.PatternColorIndex = xlAutomatic
            .Interior.Color = 10498160
            .Interior.TintAndShade = 0
            .Interior.PatternTintAndShade = 0
            .Font.Color = -16711681
            .Font.TintAndShade = 0
            .Font.Bold = True
        End With
    End If
Next c
End Sub
Thank you so so much this worked great - Much appreciated
 
Upvote 0

Forum statistics

Threads
1,223,914
Messages
6,175,353
Members
452,638
Latest member
Oluwabukunmi

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