Link to home
Create AccountLog in
Avatar of upobDaPlaya
upobDaPlaya

asked on

Find the last row in Excel that has data - What are the Range paramaters

I want to find the last row that has data in it when reviewing Column A.  Note I always know that there will be values in Col A..

When I use the below code..I initially used LstRow = WS.Range("A:A").End(xlUp).Row, but the it kept coming back that row 1 was the last row that had data even though it should be row 5.  I changed the line of code to LstRow = WS.Range("A100000").End(xlUp).Row and now it works, but I want to search the entire column not the first 100,000 celss in Col A.

Why won't LstRow = WS.Range("A:A").End(xlUp).Row work ?

Sub ImportNewSSN()
Dim ThisWS As Worksheet
Dim WS As Worksheet
Dim WB As Workbook
Dim WBPath As String, WBFile As String
Dim ThisMaxRow As Long, MaxRow As Long, I As Long
Dim SSNCount As Long
Dim cCell As Range
Dim X
Dim LstRow As Long

Application.EnableEvents = False
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Set ThisWS = ActiveSheet
ThisMaxRow = ThisWS.Range("A:A").End(xlDown).Row + 1
WBPath = ActiveWorkbook.Path

Do
    WBFile = GFileName(WBPath, "Please choose SSN File to Import: ")
    If WBFile = "" Then
        If MsgBox("No file has been selected" & Chr(10) _
            & "[OK]     to continue and select a file." & Chr(10) _
            & "[Cancel] to Exit." & Chr(10) & Chr(10) _
            & "Please make a selection.", vbInformation + vbOKCancel, "Import SSN File") = vbCancel Then
            Application.EnableEvents = True
            Application.DisplayAlerts = True
            Application.ScreenUpdating = True
            Exit Sub
        End If
    End If
Loop Until WBFile <> ""
    
Set WB = Workbooks.Open(WBFile)
Set WS = ActiveSheet
LstRow = WS.Range("A100000").End(xlUp).Row

Open in new window

SOLUTION
Avatar of Juan Ocasio
Juan Ocasio
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
The statement you initially tried isn't working because the End property applies to the top left cell in the range. This makes it equivalent to:
LstRow = WS.Range("A1").End(xlUp).Row

An alternative way of doing it is:
With WS
     LstRow = .Cells(.Rows.Count, 1).End(xlUp).Row
End With

I use .Rows.Count instead of a fixed number so the code works both on a .xls workbook (65536 rows) and on a .xlsm workbook (1048576 rows)

It is worth noting that ActiveSheet.UsedRange.Rows.Count will return the number of rows used--but not necessarily the number of the last row used. That's because the first row used may not be row 1. To overcome this problem, you could use:
LstRow = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1
Did any of the above help you?
Avatar of upobDaPlaya
upobDaPlaya

ASKER

Hi Martin...First of all thanks as this did help.  Final question though from a excel language perspective is .Cells(.Rows.Count, 1) a Range property of Cells ?
.Rows.Count          'Returns the number of rows in the worksheet in the With block
That would be 65,536 rows for .xls workbook, or 1,048,576 for .xlsx

.Cells(.Rows.Count, 1)           'Returns a reference to either A65536 or A1048576 for worksheet in With block. It's a range object.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
thanks brad (did you do a video session once for ee ? if so I attended !)..I appreciate you taking the time to explain your position.  One of the things I love about EE is I definitely learn and for sure get a ton of value.  However, this would not be possible without people such as yourself and Martin who provide answers, but more importantly explain the mechanics of your solution.  It then puts me in a great position to assess the pros and cons of the solution for my situation and determine the best course of action.
Were you going to assign points to one or more of us?
Thnka again for the help and being patient with me..I reviewed again today and thoroughly understand...
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013