Q: how to use UsedRange correctly?

  • Thread starter Thread starter alex
  • Start date Start date
A

alex

Hi There,

I'm using UsedRange in a couple of places and hoping to get advice on
what I'm doing incorrectly. The data that's in the worksheet is from
andexternal data source so it changes.
What I'm seeing is that the number of columns that UsedRange see's is
correct but that the number of Rows is not. If it wasn't for the "If
somecount - 400 then Exit For" the macro would just run forever
(requiring me to KILL excel and loose any unsaved changes. D'oh)

Also, If I do cntrl-end, i end up at the botton right corner of
IV399...399 being where I for that stop.

Why aren't my results meeting my expectations?

Many Thanks,
Alex


UsedRange.Interior.ColorIndex = 2
and
For Each CurrCell In ActiveSheet.UsedRange.Range("AG:AG")
somecounter = somecounter + 1
If CurrCell >= 119 Then
'Range("A" & CurrCell.Row & ":D" &
CurrCell.Row).Interior.ColorIndex = 6
'Range("A" & CurrCell.Row & ":" & NumOfColumns & _
CurrCell.Row).Interior.ColorIndex = 6
CurrCell.EntireRow.Interior.ColorIndex = 6
End If
CurrCell.EntireRow.Font.ColorIndex = 1
If somecounter = 400 Then
Exit For
End If
Next
 
Option Explicit

Sub checker()
Dim ws As Worksheet
Dim CurrCell As Range
Dim lastrow As Long
Set ws = ActiveSheet

With ws.UsedRange
lastrow = .Row + .Rows.Count - 1
End With

ws.UsedRange.Interior.ColorIndex = 2

With ws
For Each CurrCell In .Range(.Range("AG1"), .Cells(lastrow, "AG"))
If CurrCell.Value >= 119 Then
CurrCell.EntireRow.Interior.ColorIndex = 6
End If
CurrCell.EntireRow.Font.ColorIndex = 1

Next
End With
End Sub
 
Thanks for the great replies. I've learned a lot from that example and
the FAQ (though I also have a bunch more questions too :)

Cheers!
Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top