I have no idea why macro highlighting wrong rows

anand3dinesh

Board Regular
Joined
Dec 19, 2019
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Hi Excel Experts,

i have this code to highlight holidays in my daily tracker. when i run this code it highlights wrong rows also wrong cells.
Please check my code below. also image attached to show what cells exactly code is highlighting when i run this.
Many Thanks.


Sub holidays()

Dim i As Long
Dim lstrow As Long

lstrow = Range("B100").End(xlUp).Row
For i = 1 To lstrow
On Error Resume Next
Cells(i, 3).Find("Saturday").Range("A" & i & ":" & "E" & i).Interior.Color = RGB(248, 203, 173)
Cells(i, 3).Find("Sunday").Range("A" & i & ":" & "E" & i).Interior.Color = RGB(248, 203, 173)

Next i

End Sub
 

Attachments

  • VBA Query.JPG
    VBA Query.JPG
    116.2 KB · Views: 7
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
If you meant holidays = Saturdays and Sundays, maybe try this:
VBA Code:
Sub holidays()

Dim i As Long
Dim lstrow As Long

lstrow = Range("B100").End(xlUp).Row
For i = 1 To lstrow
    If Cells(i, 3).Value = "Saturday" Or Cells(i, 3).Value = "Sunday" Then Range("A" & i & ":" & "E" & i).Interior.Color = RGB(248, 203, 173)
Next i
End Sub
 
Upvote 0
Solution
Because this : .Range("A" & i & ":" & "E" & i)

Is offsetting Cells(i, 3).Find("Saturday") by i rows.

(Are you sure the code highlights A:E and not C:G ?)

Try this :
VBA Code:
Cells(i, 3).Find("Saturday").Offset(0, -2).Resize(, 5).Interior.Color = RGB(248, 203, 173)

Your code is not very efficient.
Why not try conditional formatting instead.
 
Upvote 0
If you meant holidays = Saturdays and Sundays, maybe try this:
VBA Code:
Sub holidays()

Dim i As Long
Dim lstrow As Long

lstrow = Range("B100").End(xlUp).Row
For i = 1 To lstrow
    If Cells(i, 3).Value = "Saturday" Or Cells(i, 3).Value = "Sunday" Then Range("A" & i & ":" & "E" & i).Interior.Color = RGB(248, 203, 173)
Next i
End Sub
This works perfect thank you
 
Upvote 0
Because this : .Range("A" & i & ":" & "E" & i)

Is offsetting Cells(i, 3).Find("Saturday") by i rows.

(Are you sure the code highlights A:E and not C:G ?)

Try this :
VBA Code:
Cells(i, 3).Find("Saturday").Offset(0, -2).Resize(, 5).Interior.Color = RGB(248, 203, 173)

Your code is not very efficient.
Why not try conditional formatting instead.
i tried conditional formating but i can only able to highlight perticular cell, i want to hilight row from A:E
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

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