I have a file saved in location A with path link C:\User\Testing\A\client_01.xlsm
But I need to open the file in location B eg. C:\User\Testing\B\client_01.xlsm where the location saved the related documents in there.
sometimes open in location C \ D etc.
So the path link save in macro needs to be variant as the location changes
Please advice as I'm beginner in VBA
Thanks
Sub Testing
Dim FilePath As String
Dim FileName As String
Dim wb As Workbook
Set wb = ThisWorkbook
FileName = wb
FilePath = "C:\User\Testing\",FileName,"\client_01".xlsm"
End Sub
FileName is a string so you'll have to use wb.Name
As for the FilePath, you can't concatenate like that, use & instead
FilePath = "C:\User\Testing\" & FileName & "\client_01.xlsm"
That is ofc if the workbook your code is in is called "A", "B", "C" or whatever you wanted.
If you want to use the path of the workbook that you're code is in: use something like:
Sub Testing
Dim FilePath As String
Dim FileName As String
Dim wb As Workbook
Set wb = ThisWorkbook
FileName = wb.Name
FilePath = ThisWorkbook.Path & "\" & FileName & "\client_01".xlsm"
End Sub
You can adjust as you see fit ofc
To return the current workbook's folder path as a string, use:
FilePath = wb.Path
Then if your code opens to related documents (lets say their names are saved as string variables relateddoc1 etc):
Workbooks.Open(FilePath & "\" & relateddoc1)
Related
I have a folder containing over 100 worksheets and named 1,2,3, etc but I can only open the workbooks by adding .xlsx to the number of the workbook in the combobox.
Is there a way to go around this?
Private Sub CommandButton1_Click()
Dim filename As String
Dim filepath As String
filename = ComboBox1.Value
filepath = "D:\e-library\MSC\DIS7000\data\"
Workbooks.Open (filepath & filename)
End Sub
Yes - you can get through that bu adding .xlsx to the filename programmatically instead of manually.
Try this:
Private Sub CommandButton1_Click()
Dim filename As String
Dim filepath As String
filename = ComboBox1.Value
filepath = "D:\e-library\MSC\DIS7000\data\"
Workbooks.Open (filepath & filename & ".xlsx")
End Sub
I would like to create a New workbook and name it as today's date and DE.
(Eg : 22.01.2018-DE)
Format(Date, "dd/mm/yyyy") & "-DE"
If the workbook is already existed or opened, then delete it or close it. Finally save the workbook. I used the code below but not working. Displaying object defined error. Help me
I need to rename Land-DE to 22.01.2018-DE.
Sub createlandDE()
Dim wb As Workbook
Set wb = Workbooks.add
ActiveWorkbook.Names.add Name:=Format(Date, "dd/mm/yyyy") & "-DE"
Dim path As String
Dim FSO As Object
path = "Q:\Job\Land-DE.xlsx" 'Need to rename the file here
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(path) Then
On Error Resume Next
Workbooks("Land-DE").Close False 'Workbook name must automatically come here
Kill path
wb.SaveAs path
Else
wb.SaveAs path
End If
How about this:
Sub createlandDE()
Dim wb As Workbook
Set wb = Workbooks.Add
NameValue = Format(Date, "dd-mm-yyyy") & "-DE"
Dim path As String
Dim FSO As Object
delpath = "Q:\Job\Land-DE.xlsx" 'Need to name the file to delete
path = "Q:\Job\" & NameValue & ".xlsx" 'Need to rename the file here
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(path) Then
On Error Resume Next
Workbooks(NameValue).Close False
Kill delpath
wb.SaveAs path
Else
wb.SaveAs path
End If
End Sub
Try with saving as:
Workbooks("Land-DE").SaveAs Filename:="Q:\Job\22.01.2018.xlsx"
Then delete the file with the old name.
To make it look better, consider saving "Q:\Job\22.01.2018.xlsx" as a String variable.
I believe you are editing the wrong property.
I would simply use ActiveWorkbook.Name = [your desired name]
I currently have a workbook for each person in my team where they have a worksheet named "Panel" that contains their initiatives and progress.
I want to develop a unified spreadsheet containing all their initiatives to have a view of the whole area.
In each "Panel" sheet, the "U5" cell contains the name of the owner. In my consolidated file, I want to put the name of the owner as the name of the corresponding sheet.
I made this macro to get, from a separate folder where they will all put their individual sheets, all the "Panel" sheets, put them in the main file and rename them to identify the owner.
Later on, I'll develop a database with the initiatives, identifying the start and end of the data fields to compile them in a single manner for a dashboard.
This is my code:
Sub GetSheets()
Path = "C:\Users\Admin\Desktop\PMO\Test consolidation\Independent files"
Filename = Dir(Path & "*.xlsm")
Dim wsname As String
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
Worksheets("Panel").Activate
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Worksheets("Panel").Select
wsname = Range("U5")
Worksheets("Panel").Name = wsname
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
Can you help to identify why this is not working?
Thanks!
Here is an example which checks whether path has \ present, whether sheets exists (code a la Rory) and also whether U5 is empty. Assumes, U5 in workbooks you are opening are being used for renaming.
Option Explicit
Sub GetSheets()
Dim path As String
Dim Filename As String
Dim wbMaster As Workbook
Dim wbActive As Workbook
Dim wsPanel As Worksheet
Set wbMaster = ThisWorkbook
path = "C:\Users\Admin\Desktop\PMO\Test consolidation\Independent files"
If Right$(path, 1) <> "\" Then path = path & "\"
Filename = Dir(path & "*.xlsm")
Dim wsname As String
Do While Filename <> ""
Set wbActive = Workbooks.Open(Filename:=path & Filename, ReadOnly:=True)
With wbActive
If Evaluate("ISREF('" & "Panel" & "'!A1)") Then 'Rory 'https://stackoverflow.com/questions/6688131/test-or-check-if-sheet-exists
Set wsPanel = wbActive.Worksheets("Panel")
wsPanel.Copy After:=wbMaster.Worksheets(1)
If Not IsEmpty(wsPanel.Range("U5")) Then
ActiveSheet.Name = wsPanel.Range("U5")
Else
MsgBox "Missing value to rename worksheet in " & Filename
End If
End If
End With
wbActive.Close
Filename = Dir()
Loop
End Sub
I am trying to get Excel to open any file in the a given folder
(ThisWorkbook.Path\Peach\Apple) that has .xlsm extension (there is always only 1 file). Is it possible to open it with wildcard character? I do not know the name of the file, just the extension.
If not, is there a way to do it?
Just ask the file system for the first matching file:
Dim path As String: path = ThisWorkbook.path & "\Peach\Apple\"
FindFirstFile = Dir$(path & "*.xlsm")
If (FindFirstFile <> "") Then
Workbooks.Open path & FindFirstFile
Else
'// not found
End If
(This will not search sub-directories)
You mentioned that it would be nice addition to open last modified file or file with shortest name, so let's start - there's a code example how you can grab all three files (first finded, last modified, with shortest name). You can modify this as you wish (add some parameters, add error handling, return only specified, etc).
Sub Test()
'declarations
Dim fso As Object
Dim folder As Object
Dim file As Object
Dim path As String
Dim first_finded As Object
Dim recently_modified As Object
Dim shortest_name As Object
Dim recently As Date
Dim shortest As Long
Dim firstFinded As Boolean
'setting default recently date(24 hours from now) and path
recently = DateAdd("h", -24, Now)
path = ThisWorkbook.path & "\Peach\Apple\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(path)
'iteration over folder
For Each file In folder.Files
If file.Name Like "*.xlsm" Then
'grab first finded .xlsm
If Not firstFinded Then
firstFinded = Not firstFinded
Set first_finded = file
End If
'grab lastmodified .xlsm
If file.DateLastModified > recently Then
recently = file.DateLastModified
Set recently_modified = file
End If
'grab short named .xlsm
If shortest = 0 Or shortest > Len(file.Name) Then
shortest = Len(file.Name)
Set shortest_name = file
End If
End If
Next
'debug-print names
Debug.Print first_finded.Name
Debug.Print recently_modified.Name
Debug.Print shortest_name.Name
'so now you can uncomment this and open what you want
'Call Workbooks.Open(path & recently_modified.Name)
End Sub
Try the code below, it will open your "*.xlsm" file, in the path you've requested.
Sub OpenXLSMWildcardfile()
Dim Path As String
Path = ThisWorkbook.Path & "\Peach\Apple\"
Workbooks.Open (Path & "*.xlsm")
End Sub
PFB for the code required for opening the macro file with extension(.xlsm).
Sub OpeningFile()
'Declaring variables
Dim FileName, FolderPath As String
'Initializing folder path
FolderPath = ThisWorkbook.Path & "\Peach\Apple\"
'Finding the file name using wildcard
FileName = Dir(FolderPath & "*.xlsm")
'Looping through the workbook which are saved as macro enabled workbooks
While FileName <> ""
Workbooks.Open FolderPath & FileName
FileName = Dir()
Wend
End Sub
I have 200 folders all with different names in a folder. Now, each folder with a different name has a macro excel file (.xlsm). I'm trying to edit all the files at once with a separate file. The code goes like this:
Sub Button1_Click()
Dim wb As Workbook
Dim ws As Excel.Worksheet
Dim strPath As String
Dim strFile As String
'Get the directories
strPath = "C:\Users\generaluser\Desktop\testing main folder\"
strFile = Dir(strPath)
'Loop through the dirs
Do While strFile <> ""
'Open the workbook.
strFileName = Dir(strPath & strFile & "*.xlsm")
'Open the workbook.
Set wb = Workbooks.Open(Filename:=strPath & strFile & "\" & strFileName , ReadOnly:=False)
'Loop through the sheets.
Set ws = Application.Worksheets(1)
'Do whatever
ws.Range("A1").Interior.ColorIndex = 0
'Close the workbook
wb.Close SaveChanges:=True
'Move to the next dir.
strFile = Dir
Loop
End Sub
But this doesn't work. I have tried tweaking it but whatever i do either does nothing or causes an error. Can someone please help me get this code to work.
(also: "testing main folder" is the folder on my desktop which holds the 200 other folders which hold the .xlsm files.)
Put Option Explicit at the top of the module. You'll get some compiler errors, one of them being that strFileName isn't declared. This would have been a great clue as to where to look, because the problem is that you're using two variable names that have roughly the same meaning when you read them, and they're getting mixed up.
After you're done fixing the variables, take a look at the documentation for the Dir function. The second issue is that you're also calling Dir multiple times in the loop, which means that you're skipping results.
It should look something more like this:
Dim wb As Workbook
Dim ws As Excel.Worksheet
Dim file As String
'path never changes, so make it a Const
Const path = "C:\Users\generaluser\Desktop\testing main folder\"
'This returns the first result.
file = Dir$(path & "*.xlsm")
Do While file <> vbNullString
Set wb = Workbooks.Open(Filename:=path & file, ReadOnly:=False)
Set ws = Application.Worksheets(1)
'Do whatever
wb.Close SaveChanges:=True
'This returns the next result, or vbNullString if none.
file = Dir$
Loop