Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Range End(xlDown) method not good when only one row

Status
Not open for further replies.

alwayslrN

IS-IT--Management
Jun 20, 2006
159
US
I have two workbooks which use the following code. The problem is it is possible at some point for one or both of the files to only have a header row and one row of data. In the case of the of the latter the End(xlDown) method will go throw every row in the spreadsheet. How can I avoid that, but still have the code work with there is more than one row after the header?

Code:
For Each CurtCellRng In Range(Range("K2"), Range("K2").End(xlDown))
       If CurtCellRng.Value = 0 Then
             CurtCellRng.Value = Cells(CurtCellRng.Row, "I").Value
             Cells(CurtCellRng.Row, "I").Value = 0
       Else
             CurtCellRng.Value = 0
       End If
    Next CurtCellRng
 



Hi,

I often use something like this...
Code:
    If Range("K2").CurrentRegion.Rows.Count > 1 Then
        For Each CurtCellRng In Range(Range("K2"), Range("K2").End(xlDown))
               If CurtCellRng.Value = 0 Then
                     CurtCellRng.Value = Cells(CurtCellRng.Row, "I").Value
                     Cells(CurtCellRng.Row, "I").Value = 0
               Else
                     CurtCellRng.Value = 0
               End If
        Next CurtCellRng
    End If


Skip,

[glasses] [red][/red]
[tongue]
 
alternatively:

For Each CurtCellRng In Range(Range("K2"), Range("K65536").End(xlUP))


Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top