Advice for existing code edit please

ipbr21054

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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,488
Office Version
  1. 365
Platform
  1. Windows
Why post the same problem in two different threads?
 

ipbr21054

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

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,488
Office Version
  1. 365
Platform
  1. Windows
They are both linked and can be covered in the one.
 

ipbr21054

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

ADVERTISEMENT

Ok then please advise and hightlite the added code to show which covers each of my questions.
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,091
Office Version
  1. 2013
Platform
  1. Windows
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
 

ipbr21054

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

ADVERTISEMENT

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
 

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 selection change and not change?
This means the code runs anytime you select a cell and not just make a change to the cell.
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,946
Office Version
  1. 2007
Platform
  1. Windows
An oversight & now changed thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,130,238
Messages
5,641,029
Members
417,189
Latest member
koelleyath

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