I keep running into an error 9.
Dim SavePath As String
Dim SaveAs As String
Dim FileName As String
Dim sDate As String
'// Save it Path
SavePath = "P:\Price book\SQF Food Safety Management System\Records - Monitoring Forms\Production Records\"
'// File Name
FileName = " - Production Record"
'// Format the on "N2" to YYYY-MM-DD
sDate = Format(ThisWorkbook.Sheets("Sheet8").Range("N1"), YYY.MM.DD)
'// Save with File Name & Date & .pdf
SaveAs = sDate & FileName & ".pdf"
Application.DisplayAlerts = True
'// Export Active Sheet as pdf
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
SavePath & SaveAs
Application.DisplayAlerts = True
The sDate is out of range.
I want to export the worksheet as a PDF with the date which is in a cell N1 and ' - Production Record' at the end.
Please, try changing the sDate definition line in this way:
sDate = Format(ThisWorkbook.Sheets("Sheet8").Range("N1").value, "YYY.MM.DD")
Related
I am trying to copy value from one tab and paste to another excel file.
Source file was saved in the destination file saved in
"D:\Feb 2023\20230214\Daily Rec 20230214.xlsb"
and destination file saved in
"D:\Feb 2023\20230214\Daily Rec 20230214.xlsb"
I am wonder if I can input Month and date value in the destination file to make the macro smart enough to go for the newly saved files (normally saved by month and date)
For example, in the designation file Sheet "Rec", Cell"A1", input is "Feb 2023", and Cell"B1", input is "20230214". Can I make the filenames taken the value from these 2 cells?
Thank you.
Sub LoadJPMTrans()
Const csvFile = "D:\Feb 2023\20230214\JPM Statement_MTD_20230214.xls"
Dim ws As Worksheet, csv As Workbook, cCount As Long, cName As String
Set ws = ThisWorkbook.ActiveSheet
Workbooks.Open csvFile
Set csv = ActiveWorkbook
Filename = "D:\Feb 2023\20230214\Daily Rec 20230214.xlsb"
cName = csv.Name
ActiveWorkbook.Sheets("Details").Columns("A:AH").Copy
Workbooks.Open Filename
Set test = ActiveWorkbook.Sheets("JPM Statement")
test.Activate
test.Range("A:AH").PasteSpecial xlPasteValues
csv.Close
End Sub
This shows how to read a date from a cell, and use that date to create a file path:
Dim wb As Workbook, wsRec As Worksheet, dt As Date, ymd As String, my As String
Dim csvFile As String, fileName As String
Set wb = ThisWorkbook
Set wsRec = wb.Worksheets("Rec")
dt = wsRec.Range("A1").Value 'read the date (only need one cell)
my = Format(dt, "Mmm yyyy") 'format as (eg) "Feb 2023"
ymd = Format(dt, "yyyymmdd") 'format as (eg) "20230214"
csvFile = "D:\" & my & "\" & ymd & "\JPM Statement_MTD_" & ymd & ".xls"
Debug.Print csvFile
fileName = "D:\" & my & "\" & ymd & "\Daily Rec " & ymd & ".xlsb"
Debug.Print fileName
Format - https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/format-function-visual-basic-for-applications
Sub LoopThroughFolder()
Dim table As Range
Dim FSO
Dim month As String
Dim year As String
Dim FileName As String
Dim OldFileName As String
Dim MainPath As String
Dim ClientPath As String
Dim FullPath As String
Dim FileToOpen As Workbook
Dim Text As String
Application.ScreenUpdating = False
Set wb = ThisWorkbook
Set ws = wb.Worksheets("FileName")
month = ws.Range("E8")
year = ws.Range("F8")
OldFileName = ws.Range("R5")
MainPath = "C:\Document\documents\CPREIF_daily_test\"
ClientPath = MainPath & year & "\" & month & " - " & year & "\"
Set table = Range("B8", Range("B8").End(xlToRight).End(xlDown))
For Each Row In table.Rows
Text = Row.Cells(1, 1)
FileName = Row.Cells(1, 7)
Set FileToOpen = Workbooks.Open(ClientPath & OldFileName, UpdateLinks:=0)
Range("B4").ClearContents
Range("B4") = Text
Range("B4").NumberFormat = "dddd mmmm d" & ", " & "yyyy"
ActiveWorkbook.Close True, ClientPath & FileName
Next Row
MsgBox "Client Files Turned"
End Sub
Hey All. I wrote VBA to loop through each row of a table, renaming the workbook and changing the date within a cell, based off each row in a table. When I run the code within VBA editor, the code works. When I create a button and assign the macro to the button, I receive a runtime error. The code that breaks is:
ActiveWorkbook.Close True, ClientPath & FileName
Thanks!
I'm a beginner.
I want to save a copy of the active workbook with a different name to a specific folder.
The name would look like this Dailycheck Nov 11 2022.xlsm
Can someone please help me?
Thank you
Sub BackupWorkbook()
'Step 1: Create a Backup of a Workbook with Current Date in the Same folder
ThisWorkbook.SaveCopyAs _
FileName:=ThisWorkbook.path & "\" & _
Format(Date, "mm-dd-yy") & " " & _
ThisWorkbook.Name
End Sub
I copied the above code but I don't know how to modify it.
Save a Copy of ThisWorkbook
Workbook.SaveCopyAs (Microsoft)
The following saves a copy of the workbook containing this code (ThisWorkbook) to the same folder with a specific name also containing today's formatted date.
Sub SaveCopyOfMe()
' Set the workbook.
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Build the folder path.
Dim FolderPath As String: FolderPath = wb.Path & Application.PathSeparator
' Build the base name (no file extension).
Dim BaseName As String: BaseName = "DailyCheck " _
& StrConv(Format(Date, "mmm dd yyyy"), vbProperCase)
' If you want English, no matter your locale, instead use:
'Dim BaseName As String: BaseName = "DailyCheck " _
& StrConv(WorksheetFunction.Text(Date, "mmm dd yyyy"), vbProperCase)
' Note that VBA's 'Date' is the equivalent of Excel's 'TODAY()'.
' Build the file extension.
Dim wbName As String: wbName = wb.Name
Dim FileExtension As String
FileExtension = "." & Right(wbName, Len(wbName) - InStrRev(wbName, "."))
' Or just:
'FileExtension = Right(wbName, Len(wbName) - InStrRev(wbName, ".") + 1)
' Build the file path.
Dim FilePath As String: FilePath = FolderPath & BaseName & FileExtension
' Save a copy using 'SaveCopyAs':
' - saves in the same file format (i.e. needs the same file extension)
' - overwrites without confirmation
wb.SaveCopyAs Filename:=FilePath
End Sub
Soo.... having a problem saving the excel with as the name i want it to generate.. it keeps saving as "FALSE"... from what i can tell I have everything correct. Since the directory will be a variable I rather just have it save in the current folder.
Ultimately I want it as Week # m-d-yy Site.xlsm
e.i Week 36 9-5-20 41st HMU
Sub SaveWorkBook()
Dim wb As Workbook
Dim myFile As String
Dim dDate As Date
Dim sSite As String
dDate = Date 'Todays date
sSite = Range("Q10").Value 'Site Name
myFile = "Week " & WorksheetFunction.WeekNum(dDate, 2) & Format(dDate, "m-d-yy") & " " & sSite & ".xlsm"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName = myFile
End Sub
I'm confronted with the following issue:
I would like to save a Sheet to PDF using VBA. The file name should include the values of some Cells, including one cell that holds a date. This date has been changed to the format "dd-mm-yyyy" (by means of code or in the Number Format dropdown in the Home tab) since Filenames cannot hold "/" but when I look at the formula bar the date is still "dd/mm/yyyy".
When running the code the filename returns the date in that format...
How can I change the date format so that I can include it correctly in the filename?
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strDate As Range
Dim strName As Range
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveWorkbook.Sheets("Devis")
Set strName = ActiveWorkbook.Sheets("Calc").Range("Designation")
Set strDate = ActiveWorkbook.Sheets("Calc").Range("arrival")
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
strDate = Replace(strDate, "/", "-")
strFile = strName & "_" & strDate & ".pdf"
strPathFile = strPath & strFile
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
You will see I tried to replace the "/" with "-" in the code but this does not seem to help...
Looking forward to your suggestions!
When you use strDate = Replace(strDate, "/", "-") you are calling the value in cell. Even if you see a date on it, dates on Excel are, actually, numbers.
So Replace(strDate, "/", "-") does not work, because there is nothing to replace.
Using Format allows to change how a value is displayed, and you can save that new formated value into a string variable.
That explains why strFile = strName & "_" & Format(strDate,"dd-mm-yyyy") & ".pdf" works.