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(xl Up).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 ?
When I use the below code..I initially used LstRow = WS.Range("A:A").End(xlUp).
Why won't LstRow = WS.Range("A:A").End(xlUp).
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
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Did any of the above help you?
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 ?
Take a look here
http://msdn.microsoft.com/en-us/library/office/ff196273.aspx
http://msdn.microsoft.com/en-us/library/office/ff196273.aspx
.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.
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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?
ASKER
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
Marty - MVP 2009 to 2013
LstRow = WS.Range("A1").End(xlUp).R
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
LstRow = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows