I am getting a Run-time error '1004': Application-defined or object-defined error, on…
- User
I am getting a Run-time error '1004': Application-defined or object-defined error, on the line 'cell.Interior.ColorIndex = xlNone' when this VBA code runs. Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim rng As Range Dim cell As Range
' Define the range to check Set rng = Range("C2:X22") ' Check if any cell in the range has no value, remove the interior color If Not Intersect(Target, rng) Is Nothing Then Application.EnableEvents = False For Each cell In rng If cell.Value = "" Then cell.Interior.ColorIndex = xlNone End If Next cell End IfApplication.EnableEvents = True End Sub
- Assistant
The error occurs because you are trying to reset the color index of a cell to "xlNone" which is not a valid color index value. Instead, you need to use a valid color index number or a predefined color constant.
Replace the line "cell.Interior.ColorIndex = xlNone" with either of the following options:
-
To remove the interior color, you can use the RGB color code: cell.Interior.Color = RGB(255, 255, 255) ' Sets the color to white
-
To remove the interior color and revert to the default color, you can use the predefined color constant: cell.Interior.Color = xlNone ' Sets the color to the default
Choose the option that best fits your requirement and replace the line accordingly.
-