Last row used in a closed workbook sheet

peterv6

Board Regular
Joined
May 16, 2005
Messages
129
Office Version
  1. 365
I know there's a way to find the last row used in a range in a sheet, but I need to know if there is a way (using vba) to determine what the last row used in a range is in a sheet in a closed workbook.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Is the data in the closed workbook contiguous? Is so it will be possible to use the COUNTA worksheet function to determine the last row.
 
Upvote 0
Try something like this:

Code:
Sub Test()
'   file name, sheet name and column number - change to suit
    Const FName As String = "P:\TEMP\MrExcel\FileLinks\[MyFile.xls]"
    Const ShName As String = "Sheet1"
    Const ColNo As Integer = 1
    Dim ShNew As Worksheet
    Dim LastRow As Long
    Application.DisplayAlerts = False
    Set ShNew = Worksheets.Add
    With ShNew.Range("A1")
        .FormulaR1C1 = "=COUNTA('" & FName & ShName & "'!C" & ColNo & ")"
        LastRow = .Value
    End With
    ShNew.Delete
    Application.DisplayAlerts = True
    MsgBox LastRow
End Sub
 
Upvote 0
Andrew, Outstanding! Thank you very much. This does just what I need. I do have one question, however. When I run this subroutine, I get a pop-up box titled "Select Sheet", and I have to manually select the sheet I want to get the information from. Can you tell me what I might be doing incorrectly, and what might fix this?

Sub Test()
' file name, sheet name and column number - change to suit
Const FName As String = "C:\Assignments\Metrics Assignments\Test Output\August\[Opened in August.xls]Subtotals"
Const ShName As String = "Subtotals"
Const ColNo As Integer = 1
Dim ShNew As Worksheet
Dim LastRow As Long
Application.DisplayAlerts = False
Set ShNew = Worksheets.Add
With ShNew.Range("A1")
.FormulaR1C1 = "=COUNTA('" & FName & ShName & "'!C" & ColNo & ")"
LastRow = .Value
End With
ShNew.Delete
Application.DisplayAlerts = True
MsgBox LastRow
End Sub

Thanks again!!!!
 
Upvote 0
Andrew, my apologies! After posting, and seeing my subroutine below yours, I saw the problem. I added the sheet name to the end of the file. When I took the filename off, running it didn't produce the select sheets pop-up box, but it returned a value of 1, which was incorrect. (It should have been 9). What I did to fix it, was add the sheet name to the filename, and remove the sheet name from the formula line. (see below).
Thanks again, as I would never have figured this out without your help!!!!

Sub Test()
' file name, sheet name and column number - change to suit
Const FName As String = "C:\Assignments\Metrics Assignments\Test Output\August\[Opened in August.xls]Subtotals"
' Const FName As String = "C:\Assignments\Metrics Assignments\Test Output\August\[Opened in August.xls]"
Const ShName As String = "Subtotals"
Const ColNo As Integer = 1
Dim ShNew As Worksheet
Dim LastRow As Long
Application.DisplayAlerts = False
Set ShNew = Worksheets.Add
With ShNew.Range("A1")
.FormulaR1C1 = "=COUNTA('" & FName & "'!C" & ColNo & ")"
' .FormulaR1C1 = "=COUNTA('" & FName & ShName & "'!C" & ColNo & ")" <-- Returned 1 (incorrect)
LastRow = .Value
End With
ShNew.Delete
Application.DisplayAlerts = True
MsgBox LastRow
End Sub
 
Upvote 0
Thank you for this thread, which has helped with a particular issue today - I've updated the code as below

Code:
Private Function LastRowFromClosedFile(InFolder As String, InFile As String, OnSheet As String, Optional FromCell As String = "A1", Optional InColumn As Long = 1) As Long
'
' LastRowFromClosedFile Function
'
' Based on a macro created 2005-08-08 by Andrew Poulsom (https://www.mrexcel.com/forum/members/andrew-poulsom.html) shared on https://www.mrexcel.com/forum/excel-questions/155993-last-row-used-closed-workbook-sheet.html
' Created 2018-11-22 by Timothy Daniel Cox
' last edited 2018-11-22 by Timothy Daniel Cox
'
' Returns the LastRow on a given sheet from a closed file: returns "-1" if there was an error
'
' Uses: N/A
' References: N/A
'
On Error GoTo ErrorHandler
    Dim ShNew As Worksheet
    Application.DisplayAlerts = False
    Set ShNew = Worksheets.Add
    With ShNew.Range("A1")
        .FormulaR1C1 = "=COUNTA('" & InFolder & "[" & InFile & "]" & ECMSDataSheetName & "'!C" & InColumn & ")"
        LastRowFromClosedFile = .Value
    End With
    ShNew.Delete
    Application.DisplayAlerts = True
Exit Function
ErrorHandler:
    Err.Clear
    LastRowFromClosedFile = -1
End Function

Try something like this:

Code:
Sub Test()
'   file name, sheet name and column number - change to suit
    Const FName As String = "P:\TEMP\MrExcel\FileLinks\[MyFile.xls]"
    Const ShName As String = "Sheet1"
    Const ColNo As Integer = 1
    Dim ShNew As Worksheet
    Dim LastRow As Long
    Application.DisplayAlerts = False
    Set ShNew = Worksheets.Add
    With ShNew.Range("A1")
        .FormulaR1C1 = "=COUNTA('" & FName & ShName & "'!C" & ColNo & ")"
        LastRow = .Value
    End With
    ShNew.Delete
    Application.DisplayAlerts = True
    MsgBox LastRow
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top