Solved

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

Posted on 2014-10-27
3
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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!
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

738 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