Runtime Error - Object doesn't support this method

fali34533

New Member
Joined
Apr 5, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to cycle through the rows of the worksheet and if the value in Column D is London I want that row to be coloured.

However, I keep getting a runtime error and can't work out why. This is the code below:

Sub ColorRows()
Dim lastRow As Long
Dim i As Long
lastRow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
For i = 1 To lastRow
If ActiveSheet.Cells(i, "D").Value = "London" Then
ActiveSheet.Range("A" & i & ":BJ" & i).Interior.Color = RGB(254, 241, 207)
End If
Next i
End Sub

Any help would be much appreciated.

Thank you
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You are not giveing us the full error message and you are not telling us what line is highlighted when you click on the Debug button.
Is your sheet protected by any chance ?
The code works fine for me.
 
Upvote 0
Is this correct ?
:BJ


Rich (BB code):
("A" & i & ":BJ" & i)
 
Upvote 0
You are not giveing us the full error message and you are not telling us what line is highlighted when you click on the Debug button.
Is your sheet protected by any chance ?
The code works fine for me.
When I go into debug this line is highlighted

ActiveSheet.Range("A" & i & ":BJ" & i).Interior.Color = RGB(254, 241, 207)
 
Upvote 0
Like i mentioned.
Why is symbol BEFORE the BJ there ?

Try this
Rich (BB code):
ActiveSheet.Range("A" & i & "BJ" & i).Interior.Color = RGB(254, 241, 207)
 

Attachments

  • EaseUS_2023_04_16_13_05_11.jpg
    EaseUS_2023_04_16_13_05_11.jpg
    21.4 KB · Views: 4
Upvote 0
Like i mentioned.
Why is symbol BEFORE the BJ there ?

Try this
Rich (BB code):
ActiveSheet.Range("A" & i & "BJ" & i).Interior.Color = RGB(254, 241, 207)
More importantly did you try what you are suggesting ? You will find it gives you a 1004 error object defined error. The original code is fine it is meant to produce A2:BJ2 (assuming i = 2) which is correct.

@fali34533
Did you copy in the code you have here or type it in ? Like I said before it works fine for me.
This is the same code you are using just using the VBA code button (which you need to be using too) and applying indents.

VBA Code:
Sub ColorRows()
    Dim lastRow As Long
    Dim i As Long
    lastRow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
    For i = 1 To lastRow
        If ActiveSheet.Cells(i, "D").Value = "London" Then
            ActiveSheet.Range("A" & i & ":BJ" & i).Interior.Color = RGB(254, 241, 207)
        End If
    Next i
End Sub

The output from the macro was:

20230416 VBA RunTime Error fali34533.xlsm
ABCDEFG
1Col DCol ECol F
2London
3Adelaide
4Perth
5London
6Sydney
7
Sheet1
 
Upvote 0
Also you didn't answer the question as to whether your sheet was protected ?
Another consideration is do you have any merged cells ?
 
Upvote 0

Forum statistics

Threads
1,215,125
Messages
6,123,193
Members
449,090
Latest member
bes000

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