How to loop through and select a row using VBA and variables

philwojo

Well-known Member
Joined
May 10, 2013
Messages
533
OK, I know this is super easy but I am struggling with it.

I want to do a test to check a cell for a specific value, in this case 3000, I know how to do that and have that part working.

Now once I find that cell with the 3000 value I want to highlight that row from column-B through Column-BF.

This is the part I am struggling with what VBA code do I use to do this, here is what I have now, but it isn't working:

Code:
Range(Cells(rng, lastrow)).Interior.TintAndShade = -0.249977111117893

I know that isn't right, I am just not sure how to change it. "rng" is set to .Range("B7:B6000"), I also have this:
Dim textlen As Integer, rng As Range, Cell As Range, RecordLen As Integer

Thanks for any feedback, I want to learn how to do this but I am just stuck, maybe because it is Friday.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I want to do a test to check a cell for a specific value, in this case 3000, I know how to do that and have that part working.
Show us the code you have so that we know how to tell you to modify it for what you want to so with it.
 
Upvote 0
Code:
Private Sub Highlight3k()

Dim textlen As Integer, rng As Range, Cell As Range, RecordLen As Integer


With ThisWorkbook.Worksheets("PIF Checker Output - Horz")


    Set rng = .Range("B7:B6000")
    RecordLen = 3000
    
    For Each Cell In rng
      
    If Cell.Value = RecordLen Then
        Range(Cells(rng, lastrow)).Interior.TintAndShade = -0.249977111117893


    End If
    Next Cell
    
End With




End Sub
 
Upvote 0
Try
Code:
    If Cell.Value = RecordLen Then
        Cell.Resize(, 57).Interior.TintAndShade = -0.249977111117893
    End If
 
Upvote 0
Thanks Fluff, that worked.

Is there a way to make my code more efficient so it doesn't loop all the way to B6000 if there is no data?
 
Upvote 0
Code:
Private Sub Highlight3k()

Dim textlen As Integer, rng As Range, Cell As Range, RecordLen As Integer

With ThisWorkbook.Worksheets("PIF Checker Output - Horz")

    Set rng = .Range("B7:B6000")
    RecordLen = 3000
    
    For Each Cell In rng
      
    If Cell.Value = RecordLen Then
        Range(Cells(rng, lastrow)).Interior.TintAndShade = -0.249977111117893

    End If
    Next Cell
    
End With

End Sub
First off, before you can TintAndShade a cell, you need to give it a color which I don't see you doing in the code above. Did you omit doing this or, if you already have something colored, what is it... the cell in Column B or all the cells in Columns B:BF?
 
Upvote 0
Like
Code:
Private Sub Highlight3k()

Dim Cell As Range, RecordLen As Integer


With ThisWorkbook.Worksheets("PIF Checker Output - Horz")

    RecordLen = 3000
    
    For Each Cell In .Range("B7", .Range("B" & Rows.Count).End(xlUp))
      If Cell.Value = RecordLen Then
          Cell.Resize(, 57).Interior.TintAndShade = -0.249977111117893
      End If
    Next Cell
End With

End Sub
 
Upvote 0
First off, before you can TintAndShade a cell, you need to give it a color which I don't see you doing in the code above. Did you omit doing this or, if you already have something colored, what is it... the cell in Column B or all the cells in Columns B:BF?


I am doing this through another bit of code, but thanks for that tip, I was not aware of that.

Fluff, thanks for the new code it is working great. Now I have to look it over so I can educate myself and learn from this, I'm getting better, but still not where I want to be with it.
 
Upvote 0
Fluff, thanks for the new code it is working great. Now I have to look it over so I can educate myself and learn from this, I'm getting better, but still not where I want to be with it.
Here is Fluff's code line...

Cell.Resize(, 57).Interior.TintAndShade = -0.249977111117893

What I highlighted in red can be replaced with this....

Intersect(Cell.EntireRow, Columns("B:BF"))

Not sure if that helps you see what is going on or not, but I thought I would post it just in case.
 
Upvote 0
Thanks Rick, I will read up on the differences between the two so I can better understand. I know that Column-BF is the same as 57 in Fluff's code though.
 
Upvote 0

Forum statistics

Threads
1,215,474
Messages
6,125,024
Members
449,204
Latest member
LKN2GO

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