Getting File Information about Query Source File

Clif Mac

New Member
Joined
Jun 4, 2019
Messages
9
Excel 2016 (Excel for Office 365) .. Windows desktop version
I'm trying to get to the file modified timestamp of the external (.csv) source data in my worksheet.
Until I started in on this project I was unaware of the terms Power Query and BI ... so I've had a bit of a crash course.
I've come up with a method that appears to work -
My code is triggered by a command button in the workbook, and assumes that there is only one linked table.
VBA Code:
Private Sub CommandButton1_Click()
   Dim sh As Worksheet
   Dim var

   Set sh = Worksheets("LinkedDataWorksheet") 'descriptive name for this post
   var = Split(ActiveWorkbook.Queries(sh.ListObjects(1).Name & "$").Formula, """", 3)
Debug.Print var(1) 'full path to the source file
Debug.Print FileDateTime(var(1)) 'source file date last modified

   'other code here

End Sub
My question is in two parts: Is my method sound? and Is there a better method?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
In case someone finds this information useful sometime:
After more trial and error I discovered that the Pivot Table points directly to the underlying query, so it was not necessary to work with the ListObjects at all. (That would have required more sanity checking than I was interested in developing.) This is the essential code that I ended up with:
VBA Code:
Private Sub CommandButton1_Click()
   Dim szFilePath As String
   Dim var, p As PivotTable
   Dim q As WorkbookQuery

'Get pivot table
    Set p = Me.PivotTables(1)
    
'Get Query from Pivot Table
    On Error Resume Next
    Set q = ActiveWorkbook.Queries(p.SourceData)
    On Error GoTo 0
    If q Is Nothing Then
        ' "Query not found?!" error processing
        Exit Sub
    End If

'Get Query Source Filename
'Example Query Code ...
'Print wb.Queries("WatcherData$").Formula
'let
'    Source = Csv.Document(File.Contents("S:\Watcher\filesystemchanges__20191114.txt"), _
        [Delimiter=",", Columns=5, Encoding=1200, QuoteStyle=QuoteStyle.None]),
'
'Splitting .Formula on " returns the file path as (1)

    var = Split(q.Formula, """", 3)
    szFilePath = var(1)

'Refresh Pivot, report information

    ActiveSheet.PivotTables(1).PivotCache.Refresh
    [I1].Value = FileDateTime(szFilePath)
    [i2].Value = szFilePath
    [i3].Value = q.Name
    [h4].Value = "Sheets:"
    [i4].Value = Sheets.Count
    [h5].Value = "Queries:"
    [i5].Value = ActiveWorkbook.Queries.Count
    [h6].Value = "Pivots:"
    [i6].Value = Me.PivotTables.Count
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,311
Members
449,080
Latest member
jmsotelo

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