Advice for existing code edit please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,942
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
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,091
Office Version
  1. 2013
Platform
  1. Windows
What about
VBA Code:
 Set My_Range = Worksheets("COLOR CELLS").CurrentRegion
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,091
Office Version
  1. 2013
Platform
  1. Windows
Maybe
VBA Code:
 Set My_Range = Worksheets("COLOR CELLS").Cells()
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,597
Office Version
  1. 2013
Platform
  1. Windows
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
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,942
Office Version
  1. 2007
Platform
  1. Windows

ADVERTISEMENT

Hi,
Post #11 shows not support.
Post #13 locks up the pc
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,942
Office Version
  1. 2007
Platform
  1. Windows
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,597
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

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.
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,597
Office Version
  1. 2013
Platform
  1. Windows
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
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,942
Office Version
  1. 2007
Platform
  1. Windows
Thanks will take a better look tomorrow
 

Watch MrExcel Video

Forum statistics

Threads
1,130,219
Messages
5,640,957
Members
417,183
Latest member
CuteLeo

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
Top