Solved

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

Posted on 2014-10-27
3
174 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
Comment Utility
For nRow = 1 To ActiveSheet.UsedRange.Rows.Count

Open in new window

0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 250 total points
Comment Utility
Alternatively:
For nRow = 1 to Cells.SpecialCells(xlLastCell).Row

Open in new window

0
 
LVL 1

Author Closing Comment

by:-Polak
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now