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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Last Row or UsedRange? 2

Status
Not open for further replies.

VictoriaLJones

Technical User
May 14, 2003
55
US
Hi,

I have writen some code in order to copy down the previous cells contents, from a selected row, however I need it to stop when it finds the last row. At the moment it just keeps going and going and...... etc!!!

I know there is a find last row and Used Range property, but I am unsure as to how to incorporate them into my code.

Any help gratefully received!! I have copied the code below.

Thanks
Victoria

Dim oCell as Range

For Each oCell In Selection.Cells
If IsEmpty(oCell) Then
oCell.Offset(-1, 0).Activate
ActiveCell.Copy
oCell.Offset(0, 0).PasteSpecial (xlPasteAll)
Else:
End If
Next
 
to get the rows:

r =thisworkbook.worksheets("Name of Sheet").usedrange.rows.count

to get columns:

c = thisworkbook.worksheets("Name of Sheet").usedrange.columns.count
 
Sorry - being a bit dim this morning.... how do I use this code? I want it to select only the cells in column A....
 
Hi Victoria,

This updated version of your code should do the trick.

Code:
Dim oCell as Range
Dim LastRow as Double
LastRow = Range("A65536").End(xlUp).Row
For Each oCell In Selection.Cells
If IsEmpty(oCell) And oCell.Row <= LastRow Then
    oCell.Offset(-1, 0).Activate
    ActiveCell.Copy
    oCell.Offset(0, 0).PasteSpecial (xlPasteAll)
End If
Next

I hope this helps.

Good Luck! :)



If you can't be &quot;The Best&quot;, be the best at what you can!!!

Never say Never!!!
Nothing is impossible!!!
 
have a look at the fAQs section - there are at least 2 that deal with different ways of getting the last used row / cell etc

Rgds, Geoff
Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?
Want the best answers to your questions ? faq222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top