Run-time error '1004' when highlighting rows with certain values

alyos

New Member
Joined
Jan 21, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've looked at previous posts but still couldn't resolve the issue with a simple code, and would appreciate any navigations and advice - I'm beginner at this.

I work on a spreadsheet with only one worksheet, trying to come up with a decent way to highlight a row in a table ("Table1") that contains certain value (Conditional formatting is not something I want to use in this case).


VBA Code:
Sub Highlight()

Dim count, i As Long

count = ActiveSheet.Cells(Rows.count, "A").End(xIUp).Row
i = 17
Do While i <= count
If Cells(i, 2).Value = "Transport" Then
Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(67, 195, 233)
End If
    i = i + 1
    Loop
    End Sub

When I debug, it highlights row 3 (count = ActiveSheet.Cells(Rows.count, "A").End(xIUp).Row)

Screenshots of a worksheet and code have been attached. There might be different ways of achieving this, anything would be of help!

Thank you for your time and help :)
 

Attachments

  • Excel.png
    Excel.png
    29.7 KB · Views: 9
  • Run-time error.png
    Run-time error.png
    76.2 KB · Views: 9

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Welcome to the MrExcel board!

A couple of other comments on your code
Dim count, i As Long
This only declares i as Long data type. count will a Variant data type.
To declare both as long you need
VBA Code:
Dim count as Long, i As Long

It is a very bad idea to use a variable name the same as another word that already has a special meaning in vba (eg count)
 
Upvote 0
Welcome to the MrExcel board!

A couple of other comments on your code

This only declares i as Long data type. count will a Variant data type.
To declare both as long you need
VBA Code:
Dim count as Long, i As Long

It is a very bad idea to use a variable name the same as another word that already has a special meaning in vba (eg count)

Many thanks Peter for your tip, will incorporate this as well!
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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