Open last added file containing a name via vba - vba

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

Related

Looping through files and protect worksheet by opening and saving one at a time

I am working on more than 200 excel files and I need to protect my excel worksheets before using it to send to other companies. The code below managed to work but the problem is it keeps opening all the excel files in the folder until it ran out of memory to process and I also have to manually save all the files that are opened
Is there a way where the code can loop the excel files in the folder one at a time and save it before proceeding with the next excel file ?
Sub LoopThroughFiles()
FolderName = "C:\Users\Desktop\PROJECTS\PROJECT FILE\A"
If Right(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator
Fname = Dir(FolderName & "*.xlsx")
'loop through the files
Do While Len(Fname)
With Workbooks.Open(FolderName & Fname)
' here comes the code for the operations on every file the code finds
ActiveSheet.Protect "password", True, True
End With
' go to the next file in the folder
Fname = Dir
Loop
End Sub
Sub LoopThroughFiles()
FolderName = "C:\Users\Desktop\PROJECTS\PROJECT FILE\A"
If Right(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator
Fname = Dir(FolderName & "*.xlsx")
'loop through the files
Do While Len(Fname)
With Workbooks.Open(FolderName & Fname)
' here comes the code for the operations on every file the code finds
ActiveSheet.Protect "password", True, True
.save
.close
End With
' go to the next file in the folder
Fname = Dir
Loop
End Sub

VBA Open File From This Workbook's Folder Knowing Part of The Name

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

Create new folder if path doesn't exist (else paste in existing folder)

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

Open Multiple Files from Variable location using VBA

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

Open .csv file with Excel VBA

I'm having a problem with this piece of my code in VBA and I can't figure out why it isn't working! I've tried another code to see if it was the loop that has the issue but the issue is specifically opening the files and not iterating through them.
Sub fileloop(Path)
Dim strFile As String, strPath As String
Dim MyBook As Workbook
strPath = Path '& "\*.csv"
strFile = Dir(strPath & "\*.csv")
MsgBox (strFile)
While strFile <> ""
'placed another messagebox here to see if the strFile was the same inside the loop.
MsgBox (strFile)
'this line has the error.
Workbooks.OpenText FileName:=strFile, _
DataType:=xlDelimited, Comma:=True, Local:=True
set MyBook = ActiveWorkbook
Call SortColumnB(MyBook)
strFile = Dir
Wend
End Sub
The error message I get goes something like this:
'AC000W0009.csv' could not be found. Check the spelling of the file name, and verify that the file location is correct.
If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.
I've tried so many variations apart from the one listed above and I can't understand why VBA won't recognize that the file exists.
EDIT
Going off of what Mike said about opening a file with the complete path the changes I made to the code to let it open .csv files:
strPath = Path & "\"
strFile = Dir(strPath & "*.csv")
While strFile <> ""
MsgBox (strFile) 'added path to file name.
Workbooks.OpenText FileName:=strPath & strFile, _
DataType:=xlDelimited, Comma:=True, Local:=True
I beleve the Dir only returns only the filename.
If you want to open it, you need to add the path to the file name returned by Dir.
There's some good examples here