VBA InStr

gripper

Board Regular
Joined
Oct 29, 2002
Messages
176
Hi

I am trying to create a VBA sub to highlight an entire row based on a partial string value in column "AK"
My error occurs at code line " If InStr(Cells(i, "AK").Value, "L-") Then "

I have a sheet with about 2000 rows of data and I run this reports and probably in 2 dozen of these rows there will be an entry in column "AK" that starts with "L-" and then a string of different numbers. I need to loop down the column and identify these records and highlight the row.

I searched the web before posting and it seems like my code is OK but I am obviously off with my syntax or approach all together.

Thank you for the help.


VBA Code:
sub highlightRow()

Dim lrow, i As Long 
lrow = Cells(Rows.Count, "B").End(xlUp).Row
i = 2

Columns("AK:AK").Select
Do While i <= lrow
  
  If InStr(Cells(i, "AK").Value, "L-") Then
    Range(Cells(i, 1), Cells(i, 45)).Interior.Color = RGB(167, 220, 233)
 
  End If

  i = i + 1
 
  Loop

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi Gripper,

Can you please share the sheet or data.

Thanks,
Saurabh
 
Upvote 0
What is the error number and error description that displays when the error occurs?
 
Upvote 0
I figured it out. I forgot to set "i = 2" in my code block. This is part of a larger block and I was debugging and did not have the code capturing my initial "i" value before debugging that small section of code.

Amateur move on my part :(
 
Upvote 0
Okay, I'm glad you figured it out. I was about to write back to you "Hmm, that is not an error I would expect with the line of code you say it is occurring on".
 
Upvote 0

Forum statistics

Threads
1,215,049
Messages
6,122,864
Members
449,097
Latest member
dbomb1414

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