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
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