Excel macro to open file with partial filename - vba

I have tried looking on various forums and cannot seem to find a solution that fits my needs.
I have a file "BABERs FORMULAS - 24 Jan 2017 - Rev 079 11-27.xlsm". I use a macro to save the changes to the file which adds the date, revision number and timestamp to the filename. The file is located at the following path "D:\FORMULAS".
The macro I have is as below but this then gives me a Run-time error '1004' and says to check that the spelling of the file name and verify the location is correct.
Dim fname as Variant
fname = Dir("D:\FORMULAS\BABERs FORMULAS*")
If fname <> "" then
Workbooks.open (fname)
End If
Any ideas where I could be going wrong? Any assistance would be appreciated.

Try something like the code below:
Dim fname As Variant
Dim myPath As String
myPath = "D:\FORMULAS\"
fname = Dir(myPath & "BABERs FORMULAS*")
If fname <> "" Then
Workbooks.Open (myPath & fname)
End If

Related

Open latest pdf file with vba

I have been looking for codes on the internet and writing some myself to open the latest pdf file in a sharepoint folder. The files that I am interested in the folder are all named as such "SD Progress_YYYYMMDD.pdf". So I tried having a for loop through all the files in this folder and comparing the YYYYMMDD in each file names and keeping the highest value (classic max value programming). Unfortunately I am quite new with vba and I believe that I have a mistake with string or array dimensions in my code below but I can't quite figure it out. The following error occurs at the first If statement:
Run-time error '13': Type mismatch
You guys are the experts so if you have any advices for my code below please I am very interested. Thank you
CODE BELOW HAS BEEN EDITED AND WORKS NOW. THANK YOU.
Sub Shop_Drawing_Status()
Dim MyPath As String
Dim LatestDate As Integer
Dim MyFile As String
MyPath = "C:\Users\Documents...etc\"
MyFile = Dir(MyPath & "*.pdf", vbNormal)
While Len(MyFile) > 0
If Right(MyFile, 3) = "pdf" Then
LatestFile = Split(MyFile, ".")
If Right(LatestFile(0), 4) > LatestDate Then
LatestDate = Right(LatestFile(0), 4)
End If
End If
MyFile = Dir()
Wend
ActiveWorkbook.FollowHyperlink (MyPath & "SD Progress_2020" & LatestDate & ".pdf")
On Error Resume Next
End Sub

Open the most recent file in a shared folder

I would like to open the latest file in a shared folder.
I have a code to check the files in a folder of my laptop, like "Downloads" but I have to open a folder in a shared drive and then copy the info of this workbook and paste in another file.
'Force the explicit declaration of variables
Option Explicit
Sub OpenLatestFile()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
'Specify the path to the folder
MyPath = "P:\GTS\zdss\"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.xls", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Excel file from the folder
MyFile = Dir
Loop
'Open the latest file
Workbooks.Open MyPath & LatestFile
End Sub
Here are 3 ideas. I am not sure whether one will solve your problem but maybe it could help you.
1) I have found this discussion on a forum (http://www.vbaexpress.com/forum/showthread.php?19669-Workbooks-Open-using-network-path). In my understanding, the problem seems close to your. Maybe try to use the few lines using to manage errors (begin by On Error Resume next).
2) I think you have already verified but the extension of the file is ".xls", not ".xlsx" ?
3) Doing operations on dates in VBA needs particular functions. Here, you are doing a comparison as if it is integers (LMD > LatestDate). Moreover, I am not sure that LatestDate will have a proper value, because you never define it in the beginning. I suggest to change the code in this way. First define LatestDate before the While statement with an arbitrary low value (so, you are sure that the variable has a value and the If LMD > LatestDate statement will work properly).
LatestDate = Format("01.01.1900", "dd.mm.yyyy")
Second, change the If LMD > LatestDate statement :
If DateDiff("d",LatestDate,LMD) > 0 Then
Of course, you need to change the parameter "d" (for day) in case you want a comparison in other unit.
Cheers.

Retrieving the last modified file with a partly variable name

We have a system that automatically downloads data and puts it in excel and other sheets. I am trying to write a macro for a master spreadsheet that retrieves the latest version of a certain file to edit, copy and paste into the master sheet.
I have trouble retrieving the file as the filenames include dates.
I am quite new to VBA and am still just throwing pieces of code together to get a working thing, but I cannot find exactly what I am looking for.
Filename is for example 'ML0003 - Daily Order Entry Information - 170927'
The last 6 figures represent the date and changes every time.
This is my code so far:
Dim dtTestDate As Date
Dim sStartWB As String
Const sPath As String = "D:\Berry\AddIn\Testing\"
Const dtEarliest = #1/1/2010#
dtTestDate = Date
sStartWB = ActiveWorkbook.Name
While ActiveWorkbook.Name = sStartWB And dtTestDate >= dtEarliest
On Error Resume Next
Workbooks.Open sPath & "ML0003 - Daily Order Entry Information - " & " ****** " & ".xls"
dtTestDate = dtTestDate - 1
On Error GoTo 0
Wend
If ActiveWorkbook.Name = sStartWB Then MsgBox "Earlier file not found."
I was under the assumtion that the asterix would allow any character there, but this does not seem to work. Any ideas?
You will want to use the Dir function to look for a file using the wildcard, like this:
Dim sFilename As String
While ActiveWorkbook.Name = sStartWB And dtTestDate >= dtEarliest
sFilename = Dir(sPath & "ML0003 - Daily Order Entry Information - *.xls*")
If sFilename <> "" Then Workbooks.Open sPath & sFilename
Wend

File Path Changed Dependent On Cell Value (VBA)

i think this will be really basic and i have tried to refer to multiple threads (How to use file path from a cell in VBA?). But cant seem to get any code working without any issues.
Basically what i want to do is open different XL files dependent on an input from a cell, all the files are in the same location (folder), but obviously the name will alter. So in cell C3 i write a number e.g. 12345 and i want it to look in a constant folder for the corresponding named XL document.
All files are saved in C:\ ***** \ *****\Documents\My Work, i want to open the folder C:\ ***** \ *****\Documents\My Work\12345.XLS which changes dependent on C3.
Here is the code i attempted to use:
Sub Macro()
Dim Filelocation As String
Dim File As String
Dim Filepathfull As String
Filelocation = "C:\ ***** \ *****\Documents\My Work"
File = "C3"
Filepathfull="Filelocation & "\" & "File" & ".xlsx"
Workbooks.Open(Filename:=Filepathfull)
End Sub
Ill keep looking in the meantime! thanks for your help in advance.
EDIT WITH ANSWER:
Sub Macro()
Dim Filelocation As String
Dim File As String
Dim Filepathfull As String
Filelocation = "C:\ ***** \ *****\Documents\My Work"
File = "C3"
Filepathfull = Filelocation & "\" & Range(File).Value & ".xlsx"
Workbooks.Open (Filepathfull)
End Sub
Try this (Untested)
Filepathfull= Filelocation & "\" & Sheets("Sheet1").Range(File).Value & ".xls"
Filelocation and File are variables. Treat them as such. Anything that you put in quotes will be considered as a string.
In the above code change Sheet1 to the relevant sheet. i.e the sheet where the file names are.

Using cell value as filename for workbook when saving

I have a macro and would like to save the workbook using the value in a cell as the name of the file: Here is what I have so far.
Dim FName As String
Dim FPath As String
Sheets("As Adjusted").Select
FPath = "N:\PricingAudit\FY15 Price Increase\ Phase 1 Built Tools"
FName = Sheets("As Adjusted").Range("C4").Text
ThisWorkbook.SaveAs Filename:=FPath & "\" & FName
I get a Save as method failed when run. Any advice would be great. The only thing I can think of would be that Column C is hidden.
Any help would be appreciated.
Thanks,
Your path is invalid. There is an extra space in front of \ Phase 1 Built Tools.