vba run time error 424

kelvin_9

Active Member
Joined
Mar 6, 2015
Messages
482
Office Version
  1. 2019
Hi All,

i need a vba to work multiple request as below but i get an error 424 at "For Each rng In ranges"
how can i fix?

i have 5 columns in "source" and i need "week1" effect these changes based on "source"

last but not least, if i have another sheet named "week2", i need same effect, am i simply just add this?
Excel Formula:
    Set ws3 = ThisWorkbook.Sheets("week2")


thank you very much for your answering

here is my code

VBA Code:
Sub macro1()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim i As Long

    ' Set your worksheets
    Set ws1 = ThisWorkbook.Sheets("week1")
    Set ws2 = ThisWorkbook.Sheets("source")

    ' Define the ranges you want to format
    Dim ranges As Variant
    ranges = Array("D9:D200", "F9:F200", "H9:H200", "J9:J200", "L9:L200", "N9:N200", "P9:P200")

    ' Loop through each defined range
    For Each rng In ranges
        For Each cell In ws1.Range(rng)
            For i = 1 To 1000 ' Check against the first 1000 rows in Sheet2
                ' Check and apply formatting based on Sheet2
                If cell.Value = ws2.Cells(i, 1).Value Then
                    cell.Font.Bold = True ' Bold font size if condition met in Column A of Sheet2
                End If
                If cell.Value = ws2.Cells(i, 2).Value Then
                    cell.Font.Color = RGB(255, 0, 0) ' Red font color if condition met in Column B of Sheet2
                End If
                If cell.Value = ws2.Cells(i, 3).Value Then
                    cell.Interior.Color = RGB(0, 176, 240) ' Blue cell color if condition met in Column C of Sheet2
                End If
                If cell.Value = ws2.Cells(i, 4).Value Then
                    cell.Interior.Color = RGB(226, 239, 218) ' Green cell color if condition met in Column D of Sheet2
                End If
                If cell.Value = ws2.Cells(i, 5).Value Then
                    cell.Interior.Color = RGB(191, 191, 191) ' Grey cell color if condition met in Column E of Sheet2
                End If
            Next i
        Next cell
    Next rng
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I have not looked closely at all the code but that error should be fixed by changing ..

Rich (BB code):
Dim rng As Range
Dim rng As Variant
 
Upvote 0
Solution
I have not looked closely at all the code but that error should be fixed by changing ..

Rich (BB code):
Dim rng As Range
Dim rng As Variant
thank you very much for your answer, Peter_SSs

it works just prefect

thank you very much for your guidance
 
Upvote 0

Forum statistics

Threads
1,226,463
Messages
6,191,181
Members
453,645
Latest member
BOUCHOUATA

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