Activating a closed workbook with dates using partial string seach - excel

I have a file named "PLDriverSensitivityReportSIN__21112019_172032.xls"
"PLDriverSensitivityReportSIN__" is constant
"21112019" is the date that changes everyday
"172032" is random
I would like to have an Input box prompting " Key in today's date (ddmmyyyy)" and save this to a variable "Today"
Objective: The macro would prompt the user for today's date using the Input box and does a partial search in a specified directory for today's file. It should ignore the random element at end of the string.
The script should load the file based on the input "C:\Users\xxxx\Desktop\PLDriverSensitivityReportSIN__" & Today & "_*" & ".xls"
The current code I have is not dynamic - please see below
Set wkb2 = Workbooks.Open("C:\Users\xxxx\Desktop\PLDriverSensitivityReportSIN__21112019_172032.xls")

The following should help you achieve your desired result, it uses the Dir() function to find the Workbook using a wildcard then if the workbook is found, it will open it:
Sub OpenWildcard()
Dim sName As String
'Declare the variable for the workbook.
Today = InputBox("Enter Today's Date", "Enter Date")
'Prompt for date entry
'Today = Format(Date, "ddmmyyyy")
'Or use today's date without prompting the user for input
sName = Dir("C:\Users\xxxx\Desktop\PLDriverSensitivityReportSIN__" & Today & "_*" & ".xls")
'check if file is found
If sName <> "" Then
'if found then open
Workbooks.Open Filename:="C:\Users\xxxx\Desktop\" & sName
End If
End Sub

Related

Exporting excel file to pdf with the file name as combined information from a Cell and the date and a word

I am coding in VBA in excel. I am trying to export an excel file as a pdf. I want the name of the pdf file to be a combination of multiple things: Words (it's a name) from cell D3 & todays date & the word 'invoice'. Right now, the file exports, but it has the name 'combined' instead of showing Name09-12-202Invoice.
How do I get the 'combined' variable to reflect the Name & date & word invoice, and then get the pdf to have that name?
The code runs, just not as I am expecting. Since the exported pdf file has the name combined, instead of the information in the 'combined variable'.
I don't know if I need to have all of the dimensions for 'combined' to be a string. But if that's the case I don't know how to turn a date into a string dimension. Am I combining the clientname, client date, and client invoice variables together into one variable 'combined' properly? And primarily, why isn't the 'combined' at the end of saveLocation, not the information in the 'combined' variable?
Thank you to anyone who helps! I am new to VBA so I appreciate it!
Sub SaveActiveSheetsAsPDF()
'Create and assign variables
Dim saveLocation As String
Dim clientname As String
Dim clientdate As Date
Dim clientinvoice As String
Dim combined As String
clientname = Range("D3")
clientdate = Date
clientinvoice = "Invoice"
combined = clientname & clientdate & clientinvoice
saveLocation = "C:\Users\jessi\Documents\Info\combined.pdf"
'Save Active Sheet(s) as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:=saveLocation
End Sub
Like this:
clientname = Range("D3").Value
clientdate = Format(Date, "yyyy-mm-dd") 'Make sure you don't end up with (eg) mm/dd/yyyy format...
saveLocation = "C:\Users\jessi\Documents\Info\" & _
clientname & "_" & clientdate & "_Invoice.pdf"
'Save Active Sheet(s) as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:=saveLocation

Multiple variables from a single user input

I'm writing some VBA code that should go through all Excel files in a specific folder (folder names always formatted with Month Year, e.g. May 2020). In my code I also need to use the individual "Month" and "Year" strings e.g. "May" and "2020", and the date format mm/??/yy e.g. 5/??/20 (the day doesn't matter, so I just put ? as a placeholder) which are stored as variables.
So far, I am using Application.FileDialog(msoFileDialogFolderPicker) to let the user choose the folder, and I'm using InputBox("") three times to get the strings and date.
Is there a way to condense this so that the user only has to do one to two things, instead of four?
According to this answer combo box in a date format it seems like a combo box could work (maybe getting the month and year inputs as strings and getting the folder and date based on that?), but is there a better a way?
Any help would be appreciated!
This is the way it might work.
Ask the user for a date
From the date the macro creates the folder name
The path of the folder is stored in the macro
The code below implements and supports that work flow.
Sub GetFolderName()
Dim Inp As String
Dim FolderName As String
Dim FilePath As String
' make sure you ask for a date format that your computer can recognise
' it depends upon your Regional Settings (in Windows Control Panel)
Do
Inp = InputBox("Enter a date", "Date format dd/mm/yy", _
Format(Date, "dd/mm/yy"))
If Inp = "" Then Exit Sub ' blank or Cancel
If Not IsDate(Inp) Then
MsgBox "Sorry, I can't recognise your entry" & vbCr & _
"as a date. Please observe the date" & vbCr & _
"format requirement and try again."
End If
Loop While Not IsDate(Inp)
FilePath = Environ("userprofile") & "\Desktop\"
FolderName = Format(CDate(Inp), "mmmm yyyy")
MsgBox "Folder name is """ & FolderName & """" & vbCr & _
"File path = " & FilePath
' complete path is
Debug.Print FilePath & FolderName
End Sub
Note that the following options were not utilized but are still available.
Day(Cdate(Inp)) ' returns the day of the entered date
Month(Cdate(Inp)) ' returns the number of the month of the entered date
Year(Cdate(Inp)) ' returns the year of the entered date (45-digit)

Editing an automated naming macro in excel (based on PC name and date, with variable save path)

I'm currently trying to get an excel file to save into YYYYMMDD_fixed name piece_INITIALS OF LAST PERSON TO EDIT.
I'm using Environ function to call the User's and PC's name in a cell that i've found can be used to add to the name.
The issues i'm trying to fix are:
how can i define the save path to work on any PC regardless of user name, as current path has Users/my name/ , and up to 4 people with different PCs and names will edit this file. it should just save on Desktop on any of the 4 PCs
how can i modify the
strFile = "C:\Users\me\Desktop\" & Format(dtDate, "ddmmyyyy") & ".xlsx"
part so that it displays YYYYMMDD_name (i get this part ok) _ABC where ABC value is in cell A1 generated by the below attr function?
the function used is
Function attr(choice) As String
Select Case (choice)
Case "computer": attr = Environ("Computername")
Case "user": attr = Environ("UserName")
End Select
End Function
and the one i use to save (albeit a different format on a different file) is
Dim dtDate As Date
dtDate = Date
Dim strFile As String
strFile = "C:\Users\me\Desktop\" & Format(dtDate, "ddmmyyyy") & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strFile, FileFormat _
:=51, CreateBackup:=False
Any help would be greatly appreciated!
Programming is not my main job, just trying to automate bits where possible, so go easy on me please :)
Maybe something like that will help:
Dim strFile As String, strUserName As String
Dim dtDate As Date
dtDate = Now
strUserName = attr("user")
strFile = "C:\Users\" & strUserName & "\Desktop\" & Format(dtDate, "ddmmyyyy") & "_" & Sheets("Sheet1").Range("A1").Value & ".xlsx"
MsgBox strFile
Note that I assigned the value of an active username to strUserName and I'm using it with your strFile. I also added Sheets("Sheet1").Range("A1").Value to the code (change sheet name accordingly). The final result will look like that:
C:\Users\username\Desktop\12082019_username.xlsx

Add today's date to file name

I need to add today's date to a file name.
I have part of the code copied from another file, but it doesn't have that feature.
Where it says "/CARYYMMDD2428395101.BCA" is the place that I need to change to today's date.
Sub Export_Selection_As_Fixed_Length_File()
' Dimension all variables.
Dim DestinationFile, CellValue, Filler_Char_To_Replace_Blanks As String
Dim FileNum, ColumnCount, RowCount, FieldWidth As Integer
Dim sht As Worksheet
'Below are options incase you want to change the folder where VBA stores the .txt file
'We use ActiveWorkbook.Path in this example
'ActiveWorkbook.Path 'the activeworkbook
'ThisWorkbook.Path 'the workbook with the code
'CurDir 'the current directory (when you hit File|open)
'If a cell is blank, what character should be used instead
Filler_Char_To_Replace_Blanks = " "
'Check if the user has made any selection at all
If Selection.Cells.Count < 2 Then
MsgBox "No row has been selected"
Selection.Activate
End
End If
'This is the destination file name.
DestinationFile = ActiveWorkbook.Path & "/CARYYMMDD24284444101.BCA"
'Obtain next free file handle number.
FileNum = FreeFile()
I expect to get the name of the file as CAR19080824284444101.BCA
First I want to point out you qualified your variables incorrectly. The line Dim DestinationFile, CellValue, Filler_Char_To_Replace_Blanks As String only declares Filler_Char_To_Replace_Blanks as a String the rest are Variant types.
Format(Date, "yyyymmdd") is what you're looking for you can change the format however, I demonstrate below another way to name, but if you like what I here just modify it.
Sub Export_Selection_As_Fixed_Length_File()
' Dim all variables.
Dim DestinationFile As String, CellValue As String, Filler_Char_To_Replace_Blanks As String
Dim FileNum As Integer, ColumnCount As Integer, RowCountAs Integer, FieldWidth As Integer
Dim sht As Worksheet
'Below are options incase you want to change the folder where VBA stores the .txt file
'We use ActiveWorkbook.Path in this example
'ActiveWorkbook.Path 'the activeworkbook
'ThisWorkbook.Path 'the workbook with the code
'CurDir 'the current directory (when you hit File|open)
'If a cell is blank, what character should be used instead
Filler_Char_To_Replace_Blanks = " "
'Check if the user has made any selection at all
If Selection.Cells.Count < 2 Then
MsgBox "No row has been selected"
Selection.Activate
End
End If
'This is the destination file name. Unsure if you wanted a certain format, but this shows you how to add it.
DestinationFile = ActiveWorkbook.Path & "/CARYYMMDD24284444101.BCA" & Month(Date) &"."&Year(Date)
'Obtain next free file handle number.
FileNum = FreeFile()
This is some pseudo-code, which saves 'ThisWorkbook' into the specified path (directory eg. C:\Test) and adds the date to the end of the filename.
ThisWorkbook.SaveCopyAs <declare_path_variable> & **Format(Date, "dd-mm-yyyy")** & ThisWorkbook.Name
You can do it like this:
'Name of the Excel file with a date/time stamp
XLSFileName = DefPath & "MasterCSV " & _
Format(Now, "dd-mmm-yyyy h-mm-ss") & FileExtStr

Use Partial file name on import from SharePoint with Excel VBA

I'm trying to open, and import values to a workbook from another workbook. Both files are in SharePoint and in the same directory.
It works as long as the target file has a specific name. But the file will have a variable start and ending in the name. 5 varying numbers + name + varying date (72262-Faktureringsplan-2018-11-16).
I've tried using * but it seems it doesn't work with SharePoint. How can I use some kind of "wildcards" for accessing files names in SharePoint?
This is the part of the code that will load the file:
Sub faktureringsplan()
Dim FileName As String
FileName = ActiveWorkbook.Path & "\72262-Faktureringsplan-2018-11-16.xlsx"
Set wb = Workbooks.Open(FileName)
ThisWorkbook.Sheets(1).Range("B10:B31").Value = wb.Sheets(1).Range("B2:B23").Value
MsgBox ("Ferdig")
wb.Close
End Sub
This code is trying to look for very specific filename with '\72262-Faktureringsplan-2018-11-16.xlsx'
on line:
FileName = ActiveWorkbook.Path & "\72262-Faktureringsplan-2018-11-16.xlsx"
The way wildcard("*") works, is that it replaces the part you want to match with anything. So a simple change from the above to
"\72262-Faktureringsplan-*.xlsx"
would return filename that has any date starting with '72262-Faktureringsplan' in your case.
You could also make use of VBA's date function to find a matching filedate that has the same date as today's date.
Format(date, "YYYY-MM-DD")
would return today's date in the format "2018-11-16" (date of this reply).
Essentially, you can do something like this:
FileName = ActiveWorkbook.Path & "\*-Faktureringsplan-" & Format(date, "YYYY-MM-DD") & ".xlsx"
This would match the following: ANYTHING-Faktureringsplan-TODAY'S_DATE.xlsx

Resources