Solved

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

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

630 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