Count Cells in another File without opening the file

ManUBlueJay

Active Member
Joined
Aug 30, 2012
Messages
302
Office Version
  1. 2016
Platform
  1. Windows
I have a table (Table1) in one file called File1 that is linked to the data in another file we will call "File2. The Data is not in a table. It starts in A3, will always be 6 Cols wide, but will have different number of Rows
Is there a way to update the size of table1 in File1 without opening file 2.
This can either be using VBA or a function that counts the rows in File2.

The only way I can manage is opening the file which I would like to avoid.

Cheers in advance
 

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).
I used to have some code to read closed workbooks; can't seem to find it right now. If you open a file as read only, it's pretty fast and you can read the file even if somebody else has it open.

VBA Code:
Dim MELWB as Worksheet
Set MELWB = Workbooks.Open(FileName:=A, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
 
Upvote 0
That is an interesting take.
I Don't like to hard code VBA.
Can I name the path in excel Named Ranges and then call the name?
 
Upvote 0
So, given the code below, you could get a filename from a cell and see if it's open already. Remember, if you're opening a file as read only, it doesn't matter if somebody has it open except they may have made significant changes since opening it.

This tests if a WB is open by you
VBA Code:
Function IsWBOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
    On Error Resume Next
    IsWBOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function

This tests if a file is open by anybody and locked for read only
VBA Code:
'Checks to see if file is open by another user or by current user
Function IsFileOpen(Filename As String) As Integer
    Dim filenum As Integer, errnum As Integer

    On Error Resume Next    ' Turn error checking off.
    filenum = FreeFile()    ' Get a free file number.
    
                            ' Attempt to open the file and lock it.
    Open Filename For Input Lock Read As #filenum
    Close filenum          ' Close the file.
    errnum = Err           ' Save the error number that occurred.
    On Error GoTo 0        ' Turn error checking back on.

    ' Check to see which error occurred.
    Select Case errnum

        ' No error occurred.
        ' File is NOT already open by another user.
        Case 0
         IsFileOpen = 0

        ' Error number for "Permission Denied."
        ' File is already opened by another user.
        Case 70
            If IsWBOpen(GetFileName(Filename)) = True Then
              IsFileOpen = -1
            Else
              IsFileOpen = 1
            End If

        ' Another error occurred.
        Case Else
            Error errnum
    End Select

End Function

You can call that code something like this
VBA Code:
   Dim E as boolean
      E = IsFileOpen(FilePath)
      If E = False Then Exit Do

Another helpful SUB to return just the path of the string containing both the path and filename
VBA Code:
Function GetPath(PathFile As String) As String

  Dim X As Long
  Dim A As String
  Dim BS As Integer
  
  A = PathFile
  For X = 1 To Len(A)
    If Mid(A, X, 1) = "\" Then
      BS = X
    End If
  Next X
  If BS > 0 Then
    GetPath = Left(A, BS)
  Else
    GetPath = A & "\"
  End If
End Function
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,731
Members
449,093
Latest member
Mnur

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