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
Related
I'm creating an excel file from nothing, adding content and saving it. I want to rename the excel file once I saved it, using VBA code. The file I want to rename isn't the same file in which I'm writing the code.
Currently I'm trying to do it this way (this is a snippet of my code, just to show how I'm saving the file):
Dim workbook1 As Workbook
Dim name As String, lastcell As String
Dim oldname As String, newname As String
Set workbook1 = Application.Workbooks.Add
name = "financial report - "
workbook1.SaveAs ThisWorkbook.Path & "\" & name & ".xlsx"
'lastcell has a date that I want in my new title
lastcell = Range("A1").End(xlDown).Text
oldname = ThisWorkbook.Path & "\" & name & ".xlsx"
newname = ThisWorkbook.Path & "\" & name & lastcell & ".xlsx"
Name oldname As newname
But when I run it I get this:
The value in my lastcell variable is supposed to be in a date format like this dd/mm/yyyy. The exact cell I'm trying to copy and use as part of the name of my new excel is 05/02/2021.
The value in name by the end of the sub should be financial report - 05/02/2021.
I'm gonna be surprised if this hasn't been asked before. Does anybody know what I'm doing wrong or have any recomendations for my code?
You need to provide a date value which can work as part of a filename:
lastcell = Format(Range("A1").End(xlDown),"yyyy-mm-dd")
Check out
https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.savecopyas
workbook1.SaveCopyAs newname
There you have your issue 05/02/2021 cannot be part of the file name as a slash / is not allowed in file names. Slash and backslash are considered to be path seperators.
Try the following: Make sure the variables are declared properly as below, to ensure the date is read as numerical date and not as some text that cannot be formatted.
Dim Name As String
Name = "financial report - "
Dim ReportDate As Date
ReportDate = Range("A1").End(xlDown).Value 'make sure you read the `.Value` not `.Text`
workbook1.SaveAs ThisWorkbook.Path & "\" & Name & ".xlsx"
workbook1.SaveCopyAs ThisWorkbook.Path & "\" & Name & Format$(ReportDate, "yyyy-mm-dd") ".xlsx"
Also use .SaveAs to save the original workbook and .SaveCopyAs to save the copy with the date attached.
I am importing daily data and manipulating it. I want to save the file as the date of the data which is picked up from A4.
The data initially comes in to me as a CSV file and the data is of the format 27/06/2020. I've reformatted the data during the manipulation, but regardless of this, when I come to save the file I get a
Runtime error 1004 Microsoft Excel cannot access the file
"C:\Users\steve\Desktop\27\06\D2D75130"
I'm unclear about whether or not (and how) to change this to text format so that it saves - but I like the idea of the date staying in date format to help with my data analysis. Or maybe I should add a text string in front of the date, i.e., "Sales(then the date)" - if that would work? Here is my existing code:
' This is to save the file on the desktop using the date (content of cell A4) as the filename
' It also saves the file with the name 'Latest Report' in the Reports Folder
Sub FileNameAsCellContent()
Dim FileName As String
Dim Path As String
Application.DisplayAlerts = False
Path = "C:\Users\steve\Desktop\" 'change this if you want to save it somewhere else
FileName = Sheets("Summary").range("A4").Value & ".xlsm"
ActiveWorkbook.SaveAs Path & FileName, xlOpenXMLWorkbookMacroEnabled
' Change the format here which matches with the extention above. Choose from the following link
' http://msdn.microsoft.com/en-us/library/office/ff198017.aspx
Application.DisplayAlerts = True
' This saves it in the reports folder as 'Latest Report'
ChDir "C:\Users\steve\Desktop\Reports"
ActiveWorkbook.SaveAs FileName:= _
"C:\Users\steve\Desktop\Reports\Latest Report.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
If Sheets("Summary").range("A4").Value has a / character in it, you need to replace or get rid of that first, because that character cannot be used in a filename. Use the following line instead to remove that character.
FileName = Replace(Sheets("Summary").range("A4").Value, "/", "") & ".xlsm"
You could also replace it with a new valid character:
FileName = Replace(Sheets("Summary").range("A4").Value, "/", "-") & ".xlsm"
Replace function VBA Documentation
Here's a list of all the illegal characters:
Remove illegal characters while saving workbook Excel VBA
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
I'm somewhat new to Excel VBA and am having trouble reading cell values and using them to find a file path and open the file. Here are some of the relevant facts (hopefully this is adequate specificity but please let me know if I am missing other relevant information).
I have the first name and last name of each person in adjoining cells, e.g. first name in F4 and last name in G4.
The files I am trying to open currently all start with "LastName, FirstName" but have different characters after them that don't follow a pattern. I'm trying to use a wildcard to make up for those unknown characters.
All the files are stored in "Z:\Documents\Warehouse Personnel Updates\" and they are all .xlsx files.
Eventually I'm going to create a loop to open multiple files for each person listed, but currently I am just focusing on getting one open.
Here is the code I currently have:
Sub findFile()
Dim Folder As String
Dim FileName As String
Dim FirstName As String
Dim LastName As String
FirstName = Range("F4").Value
LastName = Range("G4").Value
Folder = "Z:\Documents\Warehouse Personnel Updates\"
FileName = Dir(Folder & LastName & ", " & FirstName & "*.xlsx")
Workbooks.Open FileName
When I run this, I get "Run-time error '1004': Sorry, we couldn't find ." Just a space and then a period to end the sentence. It seems like it isn't reading the filename at all.
If anyone has insight to what I am missing, I would appreciate it! Thanks!
You can open a workbook using a wildcard in the Dir. This is just an example...
Dim sName As String
'Declare the variable for the workbook.
sName = Dir(ThisWorkbook.Path & "\*Smith_John*")
If sName <> "" Then
Workbooks.Open Filename:=ThisWorkbook.Path & "\" & sName
End If
I currently have an Access query that exports a .csv file to a directory with headers. I need to take the date column and take the MAX date and add it to an excel spreadsheet through the VBA code. I will have 10-20 access database that does this. Below is the export VBA code.
Option Compare Database
Option Explicit
Sub ExportQuery()
Dim PathName As String, FileName As String
PathName = Application.CurrentProject.Path & "\CsvFiles\"
FileName = Replace(Replace(CurrentProject.Name, "sharepoint_", ""), ".accdb", "")
DoCmd.TransferText acExportDelim, , "Qry_ExportToCsv", PathName & FileName & ".csv", HasFieldNames:=True
End Sub