Advice for existing code edit please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,

This code works how it should with no problems.

Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim My_Range As Range
    
    Dim LastRow As Long
    
    Set My_Range = Worksheets("COLOR CELLS").Range("A1:Z50")
    
    For Each cell In My_Range
    
    If cell.Value Like "MONDAY" Then
        cell.Interior.ColorIndex = 3
        
    ElseIf cell.Value Like "TUESDAY" Then
        cell.Interior.ColorIndex = 4
        
    ElseIf cell.Value Like "WEDNESDAY" Then
        cell.Interior.ColorIndex = 22
        
    ElseIf cell.Value Like "THURSDAY" Then
        cell.Interior.ColorIndex = 6
        
    ElseIf cell.Value Like "FRIDAY" Then
        cell.Interior.ColorIndex = 7
        
    ElseIf cell.Value Like "SATURDAY" Then
        cell.Interior.ColorIndex = 8
        
    ElseIf cell.Value Like "SUNDAY" Then
        cell.Interior.ColorIndex = 46
        
    Else
        cell.Interior.ColorIndex = xlNone
        
    End If
    
    Next
    
End Sub


However i would like to know how to edit it to not restrict it to the current range shown of which is A1:Z50

I have done the below for it to hopefully work using the xlUp function but im getting deeper into it now not working but popping up error messages

Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim My_Range As Range
    
    Dim LastRow As Long
    
    With Sheets("COLOR CELLS")
    
    If cell.Value Like "MONDAY" Then
        cell.Interior.ColorIndex = 3
        
    ElseIf cell.Value Like "TUESDAY" Then
        cell.Interior.ColorIndex = 4
        
    ElseIf cell.Value Like "WEDNESDAY" Then
        cell.Interior.ColorIndex = 22
        
    ElseIf cell.Value Like "THURSDAY" Then
        cell.Interior.ColorIndex = 6
        
    ElseIf cell.Value Like "FRIDAY" Then
        cell.Interior.ColorIndex = 7
        
    ElseIf cell.Value Like "SATURDAY" Then
        cell.Interior.ColorIndex = 8
        
    ElseIf cell.Value Like "SUNDAY" Then
        cell.Interior.ColorIndex = 46
        
    Else
        cell.Interior.ColorIndex = xlNone
        
    End If
    
    LastRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
    
    
    End With
    
End Sub
 
What about
VBA Code:
 Set My_Range = Worksheets("COLOR CELLS").CurrentRegion
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Maybe
VBA Code:
 Set My_Range = Worksheets("COLOR CELLS").Cells()
 
Upvote 0
Why are you using this:
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1

this appears to mean look for last filled cell in column A

So your saying if any cell on the entire sheet has the value Monday entered you want cell.Interior.ColorIndex = 3
 
Upvote 0
I am not sure how to use it so i was trying to work it out,i stopped & put the whole code for you to see.

Yes anywhere on the sheet would change the color etc
 
Upvote 0
I am not sure how to use it so i was trying to work it out,i stopped & put the whole code for you to see.

Yes anywhere on the sheet would change the color etc
This may be why some codes provided are locking up.
Normally most code like this applies to certain columns or rows.
Having the script look at all 5 billions cells to see if there is a change to a cell where these Days are entered if very tasking.
 
Upvote 0
Try this:

This script only applies to column 1 to 10
Modify to your needs:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  3/16/2021  9:47:47 AM  EDT
If Target.Column > 0 < 11 Then

    Select Case True
        Case (InStr(Target.Value, "MONDAY") > 0): Target.Interior.ColorIndex = 3
        Case (InStr(Target.Value, "TUESDAY") > 0): Target.Interior.ColorIndex = 4
        Case (InStr(Target.Value, "WEDNESDAY") > 0): Target.Interior.ColorIndex = 22
        Case (InStr(Target.Value, "THURSDAY") > 0): Target.Interior.ColorIndex = 6
        Case (InStr(Target.Value, "FRIDAY") > 0): Target.Interior.ColorIndex = 7
        Case (InStr(Target.Value, "SATURDAY") > 0): Target.Interior.ColorIndex = 8
        Case (InStr(Target.Value, "SUNDAY") > 0): Target.Interior.ColorIndex = 46
    
            Case Else
            Target.Interior.Color = xlNone
    
    End Select
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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