Solved

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

Posted on 2014-10-27
3
188 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 250 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 250 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

756 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