I am using a Macro to rollup a bunch of excel files in a folder, the path is set like this:
MyPath = "C:\Users\Tim\Desktop\XXX\Compiled"
I need to set a relative path as the folder XXX keeps changing names.
I have found that
Workbooks.Open Filename:=ThisWorkbook.Path & "\Compiled"
will set a relative path, but can not figure out how to apply it to my situation, I have tried
MyPath = Workbooks.Open Filename:=ThisWorkbook.Path & "\Compiled"
but does not work
Thanks
Edit
I figured it out
MyPath = ActiveWorkbook.Path & "\Compiled"
While you have resolved this in terms of the path of the host workbook, it may be useful to note that you can return a relative directory to the Desktop regardless of the OS by using SpecialFolders:
Dim wsShell As Object
Set wsShell = CreateObject("wscript.shell")
strDir = wsShell.SpecialFolders("Desktop") & "\XXX\Compiled"
Related
I am new with VBA and I am struggling when trying to run a simple command.
I want to open a workbook (name = test.xlsx) which is saved in a specific path (C:\Users\u16086\Desktop\Folder) in my PC with a VBA code.
I defined two names in my first excel that are two parameters that I can modify if the path or the name of the file is different:
path: C:\Users\u16086\Desktop\Folder
file_name: test.xlsx
The code I wrote is:
Option Explicit
Sub openworksheet()
Dim path As String
Dim file_name As String
Workbooks.Open Filename:=path & file_name
End Sub
I ran the code, but it says there’s an error run-time error1004. How do I solve this problem?
You have defined two variables. You must allocate values to them and then build the path as shown below:
Sub openworksheet()
Dim path As String
Dim file_name As String
path = "C:\Users\u16086\Desktop\Folder"
file_name = "test.xlsx"
Workbooks.Open fileName:=path & "\" & file_name
End Sub
The path C:\Users\u16086\Desktop\Folder\test.xlsx can also be written as
"C:\Users\u16086\Desktop\Folder" & "\" & "test.xlsx"
so try
Workbooks.Open Filename:=path & "\" & file_name
Alternatively, you can check if the folder path ends with "\" or not. For example
Option Explicit
Sub openworksheet()
Dim path As String
Dim file_name As String
'~~> Change Sheet name and cell address accordingly
path = Sheets("Sheet1").Range("A1").Value
file_name = Sheets("Sheet1").Range("A2").Value
If Right(path, 1) <> "\" Then path = path & "\"
Workbooks.Open Filename:=path & file_name
End Sub
The current code is:
Set Workbook = Workbooks.Open("Z:\test\bankinfo20180815.xls")
The file in the folder would change. For example: it was bankinfo20180814.xls yesterday, bankinfo20180815.xls today and bankinfo20180816.xls tomorrow. So I am wondering how I can specify in VBA code to point to the file that starts with "bankinfo"?
Try this:
MyFile = "Z:\test\bankinfo"
Set Workbook = Workbooks.Open(MyFile & "*.xls")
Hope this helps!
You can use a wildcard like *, but if there are multiple files, it can open a wrong file so a better method is to make sure you are opening the exact file.
Sub OpenMyWB()
sdir = "Z:\test\"
sFile = Dir(sdir & "bankinfo" & Format(Date, "yyyymmdd") & ".xls")
Set wb = Application.Workbooks.Open(sdir & sFile)
End Sub
I'm trying to use file dialogue to pick a file and save it to a folder but I can't figure out how to code a folder path and keep getting "Path not found error"
I'm not at all a coder, so please explain in plain English:)
Below is the piece of code I tried to use but didn't work.
ArchiveFolderPath = Environ("UserDomain") & "\" & Environ("username") & "\Desktop"
Full sub code from comment below:
Sub CreateCopyFile(FilePathToCopy As String)
Dim fso As Scripting.FileSystemObject
Dim FileToCopy As Scripting.File
Dim ArchiveFolderPath As String
'Create a new folder path
Set fso = New Scripting.FileSystemObject
ArchiveFolderPath = Environ("UserProfile") & "\Desktop\Archive"
'Create a new folder
If Not fso.FolderExists(ArchiveFolderPath) Then
fso.CreateFolder ArchiveFolderPath
End If
Set FileToCopy = fso.GetFile(FilePathToCopy)
FileToCopy.Copy ArchiveFolderPath & "\" & FileToCopy.name
Set fso = Nothing
End Sub
Try adding another backslash at the end of the word, "Desktop", and add two backslashes at the very beginning of the path. Like so:
ArchiveFolderPath = "\\" & Environ("UserDomain") & "\" & Environ("username") & "\Desktop\"
You need to put another \ at the end of Desktop and then make sure that you put the file name after it when you are saving.
So will look like ArchiveFolderPath = Environ("UserDomain") & "\" & Environ("username") & "\Desktop\"
Some of what you are looking for is going to depend on the operating system. In Windows 7, there is a Public Desktop that populates all user desktops and has fairly open file permissions. It is located at:
c:\Users\Public\Desktop
If that is where you want to put things then your code would be one of these,
ArchiveFolderPath = "c:\Users\Public\Desktop"
ArchiveFolderPath = Environ("PUBLIC") & "\Desktop"
If you wanted to point to the current users desktop then that would be one of these,
ArchiveFolderPath = "c:\Users\" & Environ("USERNAME") & "\Desktop"
ArchiveFolderPath = Environ("USERPROFILE") & "\Desktop"
The environment variable that you are using (`Environ("USERDOMAIN")) typically points to the computer name in a workgroup environment although it would be different in a structured Active Directory network.
Remember to use environment variables whenever possible to avoid variations in hard-coded paths between system operating versions. An XP machine's user profiles are not in the same place as a Windows 7 or Windows 8 machine.
A quick and easy way to get a list of the environment variables in the current session is to open a command prompt window and type SET then hit Enter.
good afternoon all,
i am using the following code on my spreadsheet to save the file in a specific folder with a specific format:
Const csPath As String = "C:\Stationery Orders\"
MyName = ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:=csPath & Sheets("Stationery").Cells(1, 1) & Format(CStr(Now), "ddmmyyyy_hhmm") & " " & MyName & ".xlsm", FileFormat:=52
my problem is that i can't find a way to create this folder C:\Stationery Orders\ if the folder doesn't exist and also paste a shortcut on the user's desktop. Is that even possible? any ideas?
kind regards
Put a check before doing SaveAs. Something like,
If Dir(csPath, vbDirectory) = "" Then MkDir csPath
Then do the SaveAs
Try this. It will check if folder exists and create it if it doesn't exist.
Sub MyCuteSub()
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists("C:\temp\temptemptemp") Then
FSO.CreateFolder ("C:\temp\temptemptemp")
End If
End Sub
I want to use a wildcard to open a workbook stored in the same folder as my macro workbook. In the folder is a file named 302113-401yr-r01.xlsm. Here is my code:
Workbooks.Open filename:=ActiveWorkbook.Path & "\302113*.xlsm"
However, it tells me that there is no such file. Any advice?
We cannot open a file using a wildcard - imagine the chaos if we could!
You'll need to use Dir(ActiveWorkbook.Path & "\302113*.xlsm") to loop through the files that this returns. If there will only be one then just use this function once:
Dim sFound As String
sFound = Dir(ActiveWorkbook.Path & "\302113*.xlsm") 'the first one found
If sFound <> "" Then
Workbooks.Open filename:= ActiveWorkbook.Path & "\" & sFound
End If
Dir Function :tech on the net
From my experience this works if you have the wildcard/asterix as the last symbol in the string and if there is only one file. Try doing:
Workbooks.Open filename:=ActiveWorkbook.Path & "\302113*"
For example I am using:
Workbooks.Open Filename:="X:\business\2014\Easy*"
and it works.
You can open files using the wildcard, but only with UNC paths for some reason.
For example :
Set xlFile = xlObj.WorkBooks.Open("\\yourServerHere\dataAutomation\*.xlsx")
I'm not that experienced yet with Excel but the following works well for me for using wildcards in filenames to open files. This example requires all files to be in the same directory/folder. Yes, it is pretty simplistic.
Sub using_wildcards_to_open_files_in_excel_vba()
Dim mypath As String
Dim sFilename As String
'Suppose you have three files in a folder
' Named blank.xlsx,, ex1_939_account.xlsx, and ex1_opt 5.xlsx
'Manually open the blank.xlsx file
'The following code lines will open the second two files before closing the previously opened file.
ActiveWorkbook.Activate
mypath = ActiveWorkbook.Path
'opening xlsx file with name containing "939" and closing current file
mypath = mypath & "\*939*.xlsx"
'MsgBox mypath 'Checking
sFilename = Dir(mypath)
'MsgBox sFilename 'Checking
ActiveWorkbook.Close savechanges:=False
Workbooks.Open Filename:=sFilename
ActiveWorkbook.Activate
mypath = ActiveWorkbook.Path
'opening xlsx file with name ending in "opt 5" and closing current file
mypath = mypath & "\*opt 5.xlsx"
'MsgBox mypath 'Checking
sFilename = Dir(mypath)
'MsgBox sFilename 'Checking
ActiveWorkbook.Close savechanges:=False
Workbooks.Open Filename:=sFilename
End Sub