Find last row of a closed workbook

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This code

Rich (BB 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

was taken from here:


How can I change it so instead of displaying the result in a cell, I want to assign a variable to it, eg

Rich (BB code):
Dim a as Integer

a="=COUNTA('" & FName & ShName & "'!C" & ColNo & ")"

Thanks
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Thanks but is it possible to put it into a variable directly, WITHOUT first assigning to the cell A1 first?
 
Last edited:
Upvote 0
Hi ,

See if this works :
Code:
Sub Test()
'   file name, sheet name and column number - change to suit
    Const FName As String = "F:\Excel File Downloads\Abhijeet\[Sample File.xlsm]"
    Const ShName As String = "Sheet1"
    Const ColNo As Integer = 1
    Dim LastRow As Long
    LastRow = Application.Evaluate("=COUNTA('" & FName & ShName & "'!C" & ColNo & ")")
    MsgBox LastRow
End Sub
 
Upvote 0
Hi ,

See if this works :
Code:
Sub Test()
'   file name, sheet name and column number - change to suit
    Const FName As String = "F:\Excel File Downloads\Abhijeet\[Sample File.xlsm]"
    Const ShName As String = "Sheet1"
    Const ColNo As Integer = 1
    Dim LastRow As Long
    LastRow = Application.Evaluate("=COUNTA('" & FName & ShName & "'!C" & ColNo & ")")
    MsgBox LastRow
End Sub

Afraid not, it returns 1 when it should be a lot more.

EDIT SORTED:

Code:
 LastRow = Application.Evaluate("=COUNTA('" & FName & ShName & "'!$A:$A)")

Thanks
 
Last edited:
Upvote 0
You may have to use a temporary remote cell to get your result from the closed workbook


Code:
With ActiveSheet.Range("AZ65536")
     .FormulaR1C1 = "=COUNTA('" & FName & ShName & "'!C" & ColNo & ")"
     LastRow = .Value
     .Delete
End With
 
Last edited:
Upvote 0
Yes, I've tested it and LastRow changes depending whether the workbook is open or not.
 
Upvote 0
Like this:

Rich (BB 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

This code

Rich (BB 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

was taken from here:


How can I change it so instead of displaying the result in a cell, I want to assign a variable to it, eg

Rich (BB code):
Dim a as Integer

a="=COUNTA('" & FName & ShName & "'!C" & ColNo & ")"

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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