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
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 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")
I run a report on a daily basis called "Contract Values UK - dd-mm-yy"
where dd-mm-yy represents the day month and year the report was run.
I've tried the below code but this seems unable to locate the file.
Can someone help me adapt the below code - many thanks.
Sub OpenLatest()
a matching date
Dim dtTestDate As Date
Dim sStartWB As String
Const sPath As String = "C:\Users\Documents\Weekly Contract Values Analysis\"
Const dtEarliest = #1/1/2018#
dtTestDate = Date
sStartWB = ActiveWorkbook.Name
While ActiveWorkbook.Name = sStartWB And dtTestDate >= dtEarliest
On Error Resume Next
Workbooks.Open sPath & "Contract Values UK - " & Format(dtTestDate, "(DD-MM-YY)") & ".xlsm"
dtTestDate = dtTestDate - 1
On Error GoTo 0
Wend
If ActiveWorkbook.Name = sStartWB Then MsgBox "Earlier file not found."
End Sub
Is this what you are trying? (Untested)
I am assuming the file name is like Contract Values UK - dd-mm-yy.xlsm
Const sPath As String = "C:\Users\Documents\Weekly Contract Values Analysis\"
Const dtEarliest = #1/1/2018#
Sub Sample()
Dim i As Long
Dim dt As Date: dt = Date
Dim flName As String, dtPart As String
'~~> Loop through dates in reverse
For i = dt To dtEarliest Step -1
dtPart = Format(i, "dd-mm-yy")
'~~> Create your file name
flName = "Contract Values UK - " & dtPart & ".xlsm"
'~~> Check if exists
If Dir(sPath & flName) <> "" Then
MsgBox sPath & flName '<~~ You can now work with this file
Exit For
End If
Next i
End Sub
I have trouble when saving a new filename with timestamp
then it just puts in a new timestamp.
how can i replace the current timestamp in the name with the new one?
my filename goes from "28-11-2018 XXXXXXX" to "29-11-2018 28-11-2018 XXXXXX"
here is my code:
Sub workbook_save()
Dim thisWb As Workbook
Set thisWb = ActiveWorkbook
MyOldName = ActiveWorkbook.FullName
MyNewName = Format(Now, "dd-mm-yyyy") & " " & ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:=thisWb.Path & "\" & MyNewName
Kill MyOldName
End Sub
thanks in advance
Try this
In fixfilename put the "XXXXXX" part of your filename which I assume is a static value.
Sub workbook_save()
Dim thisWb As Workbook
Dim fixfilename As String
Dim newfilename As String
Set thisWb = ActiveWorkbook
fixfilename = "TESTFILE"
newfilename = Format(Now, "dd-mm-yyyy") & fixfilename
thisWb.SaveAs Filename:=thisWb.Path & "\" & newfilename
End Sub
Format(Now, "dd-mm-yyyy") you are saving your file simply as dd-mm-yyyy - there is no output of hours, minutes, seconds in your file name, change the date format to dd-mm-yyyy hhmmss000.