VBA - Getting type mismatch when trying to assign a cell value to a variant variable.

Entrerri99

New Member
Joined
Mar 9, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Sorry, this one is driving me crazy. I have the macro below that I'm trying to use to change the color of cells A:M based on the value in column C, however I'm getting a mismatch on this line:
Set CurrentStatus = ActiveSheet.Cells(Rng.Row, StatusColumn).Value
I've done a message box on the ActiveSheet.Cells(Rng.Row, StatusColumn).Value to make sure I was getting the right value and it shows the "Urgent" that is in the cell. Any idea what's up?


Private Sub Worksheet_Change(ByVal Target As Range)
'When the status in Column C is changed, this will make sure the color changes

Dim workRng As Range
Dim Rng As Range
Dim StatusColumn As Integer
Dim CurrentStatus As Variant

StatusColumn = 3
Set workRng = Intersect(Application.ActiveSheet.Range("A:M"), Target)


If Not workRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In workRng
Set CurrentStatus = ActiveSheet.Cells(Rng.Row, StatusColumn).Value
Select Case CurrentStatus
Case "Urgent"
Cells(Rng.Row, workRng).Interior.Color = 8420607
Case "Important"
Cells(Rng.Row, workRng).Interior.Color = 65535
End Select
Next
Application.EnableEvents = True
End If

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Remove the word Set. You only Set objects.
 
Upvote 0
Solution
I'm not sure if I understood correctly, but try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim workRng As Range, Rng As Range, StatusColumn As Integer, CurrentStatus As Variant
    StatusColumn = 3
    If Intersect(Target, Range("A:M")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    Select Case Range("C" & Target.Row).Value
        Case "Urgent"
            Target.Interior.Color = 8420607
        Case "Important"
            Target.Interior.Color = 65535
    End Select
    Application.EnableEvents = True
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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