I want to open 4 different Excel files saved under same folder using VBA code, but the folder path is not fixed.
Let's say, I have 4 Excel files named A.xlsx, B.xlsx, C.xlsx & D.xlsx under folder named 22-Feb-15 (This folder name will change everyday, but the file names will remain same).
I want VBA code so that I can select the folder manually and once it is selected, all 4 files will open one by one (there are other files too, but I need to open only these 4 files).
Please see below:
Sub FolderSelect()
Dim intResult As Integer
Dim fldrPath As String
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
If intResult <> 0 Then
fldrPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
Workbooks.Open Filename:=fldrPath & "\" & "A.xlsx"
Workbooks.Open Filename:=fldrPath & "\" & "B.xlsx"
Workbooks.Open Filename:=fldrPath & "\" & "C.xlsx"
Workbooks.Open Filename:=fldrPath & "\" & "D.xlsx"
End If
End Sub
You can change file names and/or add new files by following the same structure
Related
I am trying to open file from the same folder as the main workbook. The problem is that the name is not permanent and just one word stays always inside the name - "NAME".
I want to use specific method with Thisworkbook.Path to open the xlsx file but it is not finding the workbook with the code.
that is the relavant part of code:
Sub RemoveDuplicats()
Dim Harel As Workbook
Dim SAP As Workbook
Dim Path As String
Dim Found As String
Path = ThisWorkbook.Path
Found = Dir(Path & "*NAME*.xlsx") 'open SAP report
If Found <> "" Then
Set SAP = Workbooks.Open(Path & Found)
End If
End Sub
ThisWorkbook.Path Returns the path without trailing backslash,
try
Found = Dir ( Path & "\" & "*NAME*.xlsx")
You would need to Loop though all Fiels in this Folder and compare the File Names like this:
Dim StrFile As String
StrFile = Dir(ThisWorkbook.Path & "\*" & ".xlsm")
Do While Len(StrFile) > 0
If StrFile Like "*Name*" Then
MsgBox StrFile 'This will be your File
End If
StrFile = Dir
Loop
I have this userform
It is fully fuctioning but I want to add a feature that is sending the file as .xlsx or .txt so I can remove the macro from the file.
I searched the internet for days and come up to a procedure that I need to make a 3 process to save it to another format. The process I come up is listed below:
1. .SaveCopyAs
Copy(Filename).(Same format)
.Open
Existing File
.SaveAs
(FileName).(Any format)
And delete the SaveCopyAs file to avoid redundancy Or catch the temporary file to SaveAs another file format? Every input should be save after clicking ok button and to overwrite the existing file.
Can someone tell me if I'm making the right approach to my problem? Thanks.
I have had a similar issue, my solution involved copying the Sheets into a new workbook then renaming them accordingly and saving that workbook with a specific name:
ans = MsgBox("Would you like to export the Report?", vbYesNo)
Select Case ans
Case vbYes
FPath = "C:\...\...\... Daily Report"
FName = "Report" & ".xlsx"
Set NewBook = Workbooks.Add
ThisWorkbook.Sheets("Sheet2").Copy Before:=NewBook.Sheets(1)
ThisWorkbook.Sheets("Sheet3").Copy Before:=NewBook.Sheets(1)
Application.DisplayAlerts = False
'NewBook.Sheets("Sheet1").Delete to delete the first empty sheet on the new workbook
If Dir(FPath & "\" & FName) <> "" Then
MsgBox "File " & FPath & "\" & FName & " already exists"
Else
NewBook.SaveAs Filename:=FPath & "\" & FName
End If
NewBook.Close False
CurrentReport = FPath & "\" & FName
Case vbNo
Exit Sub
End Select
Or to append the data to a text file, each time the user clicks "OK":
Sub VBA_to_append_existing_text_file()
Dim strFile_Path As String
strFile_Path = "C:\temp\test.txt" ‘Change as per your test folder and exiting file path to append it.
Open strFile_Path For Append As #1
Write #1, "This is my sample text" ' add entered data here
Close #1
End Sub
Files are added daily to a folder, they all contain a name that beggins with "mad" I need to open the last one in order to execute my macros. My code can open the file that it´s file name contains "mad", but it doesn´t open the newest one added to the folder:
sub openfilewithnamestring
sFound = Dir(ActiveWorkbook.Path & "\mad*.xlsb") 'should open the latest file added in the folder that constains "MAD" in the name
If sFound <> "" Then
Workbooks.Open Filename:=ActiveWorkbook.Path & "\" & sFound
end if
end sub
Example:
MAD0902 16.10.2017
MAD0902 17.10.2017
I have a project to create VBA to find and open all CSV files in a folder that is named the current month. I found some stuff online that seemed close but not quite. They will eventually be converted to XLSX files and parsed. That part I have. The macro that converts, parses and saves will be housed in a different file along the same path but not as "deep".
So on my Desktop is a folder name "CSV find test". Inside are two folders "Feburary" and "March". I need it to open all csv files in the most recent month. I have the rest of the syntax. . . . .
I wouldn't imagine that it would take a huge amount of syntax. Thanks for any direction.
Sub OpenFile()
FileMonth = Month(Date)
FileDate = Format(Date, "mmmm")
FilePath = "C:\Users\Me\Desktop\CSV find convert tests\" & FileMonth & "\" & FileDate & ".xls"
Workbooks.Open Filename:="FilePath" <- - - - error happens here.
End Sub
I don't think you really understand how a variable works as you keep putting it in a string. If you put something in double quotes it creates a string. Below is how you can add the month to the string via the variable.
Sub OpenCSVs()
Dim MyFiles As String, ThisMonth As String
Dim startPath As String
ThisMonth = Format(Date, "mmmm")
startPath = "C:\Users\ME\Desktop\CSV find convert tests\" & ThisMonth & "\"
MyFiles = Dir(startPath & "*.csv")
Do While MyFiles <> ""
Workbooks.Open startPath & MyFiles
'Do stuff to it will go here
'ActiveWorkbook.Close SaveChanges:=True (Deactivated for now)
MyFiles = Dir
Loop
End Sub
I've made an Excel sheet which processes data to a Sheet and Saves it as a new workbook in a certain Folder - Subfolder (named like the first part of the file names).
The code works fine but I'd like to make a new folder if the required path does not exists. Should definitely be possible to achieve with an 'If' function, but I don't know how to create new folders.
Note: skipped some part in the code below, to keep it short I only past the parts worth mentioning.
Sub SaveSheetAs()
Dim sMainFolder as String
Dim sFileName as string
Dim sSubFolder as string
sMainFolder = Z:\Parts Manufacturing\5. Kwaliteit\130 - in proces meten\EindProject\Bron '(Main folder, which isn't variable)
sFileName = 4022 646 68954#1234 '(Part name with Unique number)'variable number, in de real code this number is received by refering to a range("")
sSubFolder = 4022 646 68954 '(variable number, in de real code this number is received by refering to a range("")
ActiveWorkbook.SaveAs Filename:=sMainFolder & "\"& sSubFolder & "\" & sFileName & ".csv", FileFormat:=xlCSV, CreateBackup:=False, Local:=True
end sub
Here you go :
If Dir(sMainFolder & "\"& sSubFolder & "\", 16) <> vbNullString Then
Else
MkDir (sMainFolder & "\"& sSubFolder & "\")
End If