Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Selecting from Row 1 to the End of Cell Addresses (improve code)

Posted on 2014-10-27
3
Medium Priority
?
198 Views
Last Modified: 2014-10-27
I'm using the below code to find values in cells and then delete the range in-between the found range. My question is does it need to be improved? Specifically, where I set nRow from 1 to 1048576 (the last row address in excel 2007). I don't know this for sure, but it would seem to me that setting 1048576 is a bad practice and there should be a way to set it from 1 to the last address, Thoughts?

Public Sub DeleteAllAppended()
If MsgBox("Are you sure? This will delete every report you have added.", vbOKCancel) = vbCancel Then
    Exit Sub
End If
Dim nRow As Long
Dim nStart As Long, nEnd As Long

 'Figure out where Appened Reports starts.
 For nRow = 1 To 1048576
 If Range("A" & nRow).Value = "Created by:" Then
 nStart = nRow
 Exit For
 End If
 Next nRow
 nStart = nStart + 2

 ' Figure out where Appened Reports end.
 For nRow = nStart To 1048576
 If Range("A" & nRow).Value = "Totals" Then
 nEnd = nRow
 Exit For
 End If
 Next nRow
 nEnd = nEnd - 1

 Range("A" & nStart & ":I" & nEnd).Delete
 End Sub

Open in new window

0
Comment
Question by:-Polak
3 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 1000 total points
ID: 40406881
For nRow = 1 To ActiveSheet.UsedRange.Rows.Count

Open in new window

0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 1000 total points
ID: 40406905
Alternatively:
For nRow = 1 to Cells.SpecialCells(xlLastCell).Row

Open in new window

0
 
LVL 1

Author Closing Comment

by:-Polak
ID: 40406938
Thanks guys, glad my suspicions were correct, I split points because Glenn's answer is helpful if I'm not activating the sheet beforehand.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question