Save excel workbook with certain tag

gazmoz17

Board Regular
Joined
Sep 18, 2020
Messages
158
Office Version
  1. 365
Platform
  1. Windows
Hi,

Not sure if this is VBA solutiion or maybe autohotkey...or both 🤦‍♂️🤣

No way of distinguishing in my file explorer what excel workbooks have connections/queries (Power Query) and which dont.

Therefore, want to start saving new workbooks containing a query with a "Q" tag. I can then filter on Tag "Q" in my file explorer.

Rather than manually adding the tag can F12 (save as) behave differently if a query/connection exists and automatically assign the "Q" tag? If no connection then just standard Save as.

Many Thanks
Gareth
 

Attachments

  • everything excel.PNG
    everything excel.PNG
    7.6 KB · Views: 7
  • Add tag.PNG
    Add tag.PNG
    3.7 KB · Views: 5

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
put the code into a module,
then add the SaveAsQfile() to the QuickAccess Toolbar for users to saveAs

Code:
Public Sub SaveAsQfile()
Dim vFile, vDir

vDir = getMyDocs()
vFile = vDir & ActiveWorkbook.Name

If HasConnection() Then
  vFile = vFile & "_Q.xlsx"
  ActiveWorkbook.SaveAs vFile
Else
  vFile = vFile & ".xlsx"
  ActiveWorkbook.SaveAs vFile
End If
MsgBox "Saved"
End Sub

private Function HasConnection() As Boolean
HasConnection = ActiveWorkbook.Connections.Count > 0
End Function

private Function getMyDocs()
On Error GoTo errDocs
getMyDocs = Environ$("USERPROFILE") & "\Documents\"
End Function
 
Upvote 0
Hi ranman,

Thanks for this much appreciated 👍

Im getting an error message per the attached image.

Do I need to change this line to my actual userprofile:

getMyDocs = Environ$("USERPROFILE") & "\Documents\"

Also will this work irrespective if I have Queries but not connections and vice versa. For either Query or Connection I wish to tag with "Q"

Many Thanks
 

Attachments

  • Error msg.PNG
    Error msg.PNG
    24 KB · Views: 4
  • Query v Connection.PNG
    Query v Connection.PNG
    5.2 KB · Views: 3
Upvote 0
swap out the MyDocs with:

Code:
Public Function getMyDocs()
Dim vDir, vUsr

On Error GoTo errDocs
vUsr = Environ("UserProfile")
vDir = vUsr & "\Documents\"
If Not DirExists(vDir) Then
    vDir = vUsr & "\My Documents\"
    If Not DirExists(vDir) Then
       vDir = "c:\temp"
       MakeDir vDir
    End If
End If
getMyDocs = vDir
Exit Function
errDocs:
MsgBox "Cannot find temp folder", vbInformation, "getMyDocs():" & Err
End Function

Public Function DirExists(ByVal pvDir) As Boolean
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
DirExists = fso.FolderExists(pvDir)
Set fso = Nothing
End Function

Public Sub MakeDir(ByVal pvDir)
Dim fso
On Error GoTo errMake
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(pvDir) Then fso.CreateFolder pvDir     'MkDir pvDir
Set fso = Nothing
Exit Sub
errMake:
'MsgBox Err.Description & vbCrLf & pvDir, , "MakeDir(): " & Err
Set fso = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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