Have to reselect cell to get macro to finish tasks.

SS Rookie

New Member
Joined
Apr 14, 2023
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
When a cell with a drop down list changes values, my macro performs actions depending on the value the drop down cell is changed too. The macro works except to get it to finish or change the color of a cell you have to click off the drop down cell and then reselect it. Is there something I need to add to the code to get the color to change at the same time it is doing the strikethrough? Copy of the If statement below.

If rngCurrent.Value = rngValue.Value Then
Range("B" & rngCurrent.Row & ":Q" & rngCurrent.Row).Font.Strikethrough = True
Range("M" & rngCurrent.Row).Interior.Color = Range("L" & rngCurrent.Row).Interior.Color
Else
Range("B" & rngCurrent.Row & ":Q" & rngCurrent.Row).Font.Strikethrough = False
Range("M" & rngCurrent.Row).Interior.ColorIndex = 4
'Exit Sub
End If
 
Try this version (I think I get what you're looking for)

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("L:L"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
       
        Select Case Target.Value
            Case Is = Range("A16").Value
                Range("B" & Target.Row & ":Q" & Target.Row).Font.Strikethrough = True
                Range("M" & Target.Row).Interior.Color = Range("L" & Target.Row).Interior.Color
            Case Is = Range("A15").Value
                Range("B" & Target.Row & ":Q" & Target.Row).Font.Strikethrough = False
                Range("M" & Target.Row).Interior.ColorIndex = 4
            Case Else
                '???
        End Select
    End If
   
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
Now it's not changing the cell color at any point.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Now it's not changing the cell color at any point.
When I use the code, it works for me:
SS Rookie.xlsm
ABGHIJKLMNOPQ
1HDR
2TESTTESTTESTTESTTESTATESTTESTTESTTESTTEST
3TESTTESTTESTTESTTESTATESTTESTTESTTESTTEST
4TESTTESTTESTTESTTESTTESTTESTTESTTESTTEST
5TESTTESTTESTTESTTESTBTESTTESTTESTTESTTEST
6TESTTESTTESTTESTTESTCTESTTESTTESTTESTTEST
7TESTTESTTESTTESTTESTTESTTESTTESTTESTTEST
8TESTTESTTESTTESTTESTBTESTTESTTESTTESTTEST
9TESTTESTTESTTESTTESTTESTTESTTESTTESTTEST
10TESTTESTTESTTESTTESTTESTTESTTESTTESTTEST
11TESTTESTTESTTESTTESTBTESTTESTTESTTESTTEST
12TESTTESTTESTTESTTESTTESTTESTTESTTESTTEST
13TESTTESTTESTTESTTESTATESTTESTTESTTESTTEST
14TESTTESTTESTTESTTESTTESTTESTTESTTESTTEST
15ATESTTESTTESTTESTTESTBTESTTESTTESTTESTTEST
16BTESTTESTTESTTESTTESTATESTTESTTESTTESTTEST
17TESTTESTTESTTESTTESTTESTTESTTESTTESTTEST
18TESTTESTTESTTESTTESTBTESTTESTTESTTESTTEST
19TESTTESTTESTTESTTESTBTESTTESTTESTTESTTEST
Sheet1
Cells with Data Validation
CellAllowCriteria
L2:L19ListA,B,C


1681850507509.png



I can't do much more to get to the bottom of the problem unless I can get a copy of your actual sheet using the XL2BB - Excel Range to BBCode or better still, if you could share your file using Dropbox, Google Drive or similar file sharing site.
 
Upvote 0
I found the one issue since which was just from copy & paste. My SS reference cells are Ai5 and Ai6. It's still for some reason requiring me to reselect the drop-down cell to get the color to change and also to go away. Attached is a link to the actual sheet.


 
Last edited:
Upvote 0
Thank you for providing the file. I'm away from my usual laptop at the moment, but I will get to this in a few hours' time.
 
Upvote 0
Turned out easier than I thought to figure out. The code in post #10 was Worksheet_Change code. For some reason you converted it to Worksheet_SelectionChange code. You do realise they're not the same thing? Anyhow, I changed it back to Worksheet_Change and it works just fine. File link here: Test_M.xlsm

Full code below:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Not Intersect(Range("L:L"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        
        Select Case Target.Value
            Case Is = Range("AI6").Value
                Range("B" & Target.Row & ":Q" & Target.Row).Font.Strikethrough = True
                Range("M" & Target.Row).Interior.Color = Range("L" & Target.Row).Interior.Color
            Case Is = Range("AI5").Value
                Range("B" & Target.Row & ":Q" & Target.Row).Font.Strikethrough = False
                Range("M" & Target.Row).Interior.ColorIndex = 4
            Case Else
        End Select
    End If
    
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
    
End Sub
 
Upvote 0
In hindsight, would be better with the Else option:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Not Intersect(Range("L:L"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        
        Select Case Target.Value
            Case Is = Range("AI6").Value
                Range("B" & Target.Row & ":Q" & Target.Row).Font.Strikethrough = True
                Range("M" & Target.Row).Interior.Color = Range("L" & Target.Row).Interior.Color
            Case Is = Range("AI5").Value
                Range("B" & Target.Row & ":Q" & Target.Row).Font.Strikethrough = False
                Range("M" & Target.Row).Interior.ColorIndex = 4
            Case Else
                Range("B" & Target.Row & ":Q" & Target.Row).Font.Strikethrough = False
                Range("M" & Target.Row).Interior.Color = Range("L" & Target.Row).Interior.Color
        End Select
    End If
    
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
    
End Sub
 
Upvote 0
In hindsight, would be better with the Else option:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
    If Not Intersect(Range("L:L"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
       
        Select Case Target.Value
            Case Is = Range("AI6").Value
                Range("B" & Target.Row & ":Q" & Target.Row).Font.Strikethrough = True
                Range("M" & Target.Row).Interior.Color = Range("L" & Target.Row).Interior.Color
            Case Is = Range("AI5").Value
                Range("B" & Target.Row & ":Q" & Target.Row).Font.Strikethrough = False
                Range("M" & Target.Row).Interior.ColorIndex = 4
            Case Else
                Range("B" & Target.Row & ":Q" & Target.Row).Font.Strikethrough = False
                Range("M" & Target.Row).Interior.Color = Range("L" & Target.Row).Interior.Color
        End Select
    End If
   
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
   
End Sub
Thanks, it is doing what I was going for. No, I'm not too keen on the differences between the Worksheet Change and WorksheetSelection Change. I'm also curious the need for the If Not at teh beginning of the code.
 
Upvote 0
Thanks, it is doing what I was going for.
Glad to hear that we found a Solution to your problem. I knew we'd get there in the end ;)

Regarding Worksheet Change vs WorksheetSelection Change, the first will initiate code running if a change happens in the value of a cell (except if it happens as a result of a formula - but that's another story) whereas the second will initiate code running if a different cell is merely selected (whether a change in value occurred or not).

The If Not...blah..blah...Is Nothing, is another way of saying If...blah..blah..Is Something or in other words, only continue with the code if a certain condition is met/true (e.g. a cell changes, in your case in column L) otherwise go straight to the End If line on the code.
 
Upvote 0

Forum statistics

Threads
1,214,962
Messages
6,122,482
Members
449,088
Latest member
Melvetica

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