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
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Why post the same problem in two different threads?
 
Upvote 0
It isnt the same problem.

One i would like to see how it can be wrote to cover part of the sheet without mentioning a range.

The other to allow upper / lower case

So two different questions.
 
Upvote 0
They are both linked and can be covered in the one.
 
Upvote 0
Ok then please advise and hightlite the added code to show which covers each of my questions.
 
Upvote 0
Like this?
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim My_Range As Range
    Dim LastRow As Long
    With ActiveSheet
    LastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
    End With
    
    Set My_Range = Worksheets("COLOR CELLS").Range("A1:Z" & LastRow)
    
    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
 
Upvote 0
Hi,
I dont need to mention a range.
I need it to just apply to any part of the sheet so not mentioning A1:Z etc
 
Upvote 0
Why are you using selection change and not change?
This means the code runs anytime you select a cell and not just make a change to the cell.
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,765
Members
449,049
Latest member
greyangel23

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