Replace parts of filename (timestamp) - excel

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.

Related

Excel VBA - Open all .csv files and save as .xlsx using cell value

I would like to:
open all .csv files in the same folder.
save a copy each in .xlsx format.
with the .xlsx format file name as the Range(“B1”).Value from the corresponding .csv file.
Here is my code so far (not working):
Dim MyFolderPath As String
MyFolderPath = Application.DefaultFilePath
myFolderPath = Application.DefaultFilePath
MyFileName = Dir(myFolderPath & "\" & "*.csv")
While MyFileName <> ""
Set CSVWorkbook = Workbooks.Open(myFolderPath & "\" & MyFileName)
CSVWorkbook.SaveCopyAs Filename:=myFolderPath & "\" & CSVWorkbook.Sheets(1).Range("B1").Value & ".xlsx"
CSVWorkbook.Close SaveChanges:=False
Wend
Could anyone help me to identify the issue?
Many thanks!
Hello TropicalMagic...
try this
`Sub test()
Dim MyFolderPath As String
Dim CSVworkbook As Workbook
Dim MyfileName As Variant
Dim NewFilename As String
MyFolderPath = Application.DefaultFilePath
MyfileName = Dir(MyFolderPath & "\" & "*.csv")
While MyfileName <> ""
Set CSVworkbook = Workbooks.Open(MyFolderPath & "\" & MyfileName)
NewFilename = CSVworkbook.Sheets(1).Range("B1").Text
CSVworkbook.SaveCopyAs fileName:=MyFolderPath & "\" & NewFilename & ".xlsx"
CSVworkbook.Close SaveChanges:=False
MyfileName = Dir
Wend
End Sub`
Another point to mention. Ensure you have data in the Sheets(1) "b1" range.
If you open an empty CSV in excel and put something in B1, Save it and reopen and you will see the text in B1 will have moved to A1 due to the way excel interprets CSV's and there being no control or delimiters in an empty CSV Spread sheet. To get around this put something in A1.
Thanks for responding! I have found a solution as well:
Dim MyFolderPath As String
Dim CSVworkbook As Workbook
Dim MyFileName As Variant
Dim NewFileName As String
MyFolderPath = Application.DefaultFilePath
MyFileName = Dir(MyFolderPath & "\" & "*.csv")
While MyFileName <> ""
Set CSVworkbook = Workbooks.Open(MyFolderPath & "\" & MyFileName)
NewFileName = CSVworkbook.Sheets(1).Range("B1").Text
CSVworkbook.SaveAs MyFolderPath & "\" & NewFileName & ".xlsx", xlOpenXMLWorkbook
CSVworkbook.Close SaveChanges:=False
MyFileName = Dir
Wend
Cheers

How to close a workbook when i already set the filename and path in a variable

I already set the path and filename in a variable.
How to close the workbook using the filename that i already set?
This is the code:
Sub CLOSE_WORKBOOK_RAW()
Dim x As String
x = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))
Dim Filepath As String
Filepath = ThisWorkbook.Path
Dim Filename As String
Filename = Filepath & "\" & x & ".xlsx"
Dim Filename2 As String
Filename2 = Filepath & "\" & x & ".xlsm"
Workbooks(Filename).Close Savechanges:=True
End Sub
The short of your code appears to be embraced in
ThisWorkbook.Close Savechanges:=True
All the rest of it just confuses the issue. However, here is the syntax for constructing a file name if, for example, you want to save the active workbook, usually while it is other than ThisWorkbook.
Sub Close_Workbook_Raw()
Dim Filepath As String ' the path to save at
Dim Filename As String ' the file's name
Dim Ext As String ' the file name extension
Filepath = ThisWorkbook.Path ' or any other valid path
Filename = Format(Date, "yymmdd ") & "MyFile's name"
Ext = "xlsm"
With ActiveWorkbook
.SaveAs Filepath & "\" & Filename & "." & Ext
.Close
End With
End Sub
How to close the workbook using the filename that I already set?
It depends
Is the workbook already saved once?
Is it a newly created workbook?
Also what is the format (xlsx/xlsm) you are going to use.
See this example for saving as Xlsx
Dim x As String
x = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))
Dim Filepath As String
Filepath = ThisWorkbook.Path
Dim sFilename As String
sFilename = Filepath & "\" & x & ".xlsx"
Dim wb As Workbook
'~~> If already saved once
Set wb = Workbooks(x & ".xlsx")
'~~> If newly created and has the name `x`
Set wb = Workbooks(x)
wb.SaveAs Filename:=sFilename, FileFormat:=xlOpenXMLWorkbook
wb.Close (False)
Note:
Filename2 = Filepath & "" & x & ".xlsm"
When using saving it as .xlsm you have to be very careful as you are going to use the original file name; basically trying to overwrite the same file.

Excel VBA Saving with Variable Name

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

My code shows error msg while saving a file with date and time in the file name

i am using the code below to save a file with date and time in the file name. Unfortunately, it shows error msg. Is there a way to avoid this error?
dim a as variant
dim fname as string
dim newWB as workbooks
a = Now()
fname = CStr(a)
Set newWB = Workbooks.Add
newWB.SaveAs Filename:="C:\Users\0003079\Downloads\QiraTickets" & "(" & fname & ")", FileFormat:=56
Problems:
Use of : in filename isn't allowed
Declaration as Workbooks, should be workbook
Try:
Dim a As Variant
Dim fname As String
Dim newWB As Workbook
a = Now()
fname = Replace(CStr(a), ":", "'")
Set newWB = Workbooks.Add
newWB.SaveAs Filename:="C:\Users\0003079\Downloads\QiraTickets" & "(" & fname & ")", FileFormat:=56
Yellow highlighted are not allowed
From Mikku and McAlex: Slash ('/') and colon (':') are not allowed in
windows file names.
However, you can format your date output to something useful before you assign it to the filename
dim a as Date ' <-- always strongly type when you can
dim fname as string
dim newWB as workbook ' Workbook, not Workbooks
a = Now() ' Assuming that you want to use a later
fname = Format(a, "yymmdd hhnnss") ' <-- Format will output a string
Set newWB = Workbooks.Add
newWB.SaveAs Filename:="C:\Users\0003079\Downloads\QiraTickets" & "(" & fname & ")", FileFormat:=56
Of course, if a is only used to form the file name, we can tidy this up.
Dim fname As String
Dim newWB As Workbook
fname = "C:\Users\0003079\Downloads\QiraTickets(" & Format(Now(), "yymmdd hhnnss") & ")"
Set newWB = Workbooks.Add
newWB.SaveAs Filename:= fname, FileFormat:=56

CSV file saved as another CSV file changing format of date from dd/mm to mm/dd

Using VBA - am saving a csv file as another csv file. Somehow it's changing the date format from dd/mm to mm/dd
So date which was 2/8 (2nd Aug) is changed to 8/2 (8 feb). Any suggestions?
Code am using:
Application.DisplayAlerts = False
Dim wkbSource As Workbook
Dim wkbDest As Workbook
Dim shttocopy As Worksheet
Dim wbname As String
Dim getdt As String
wbname = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2)
wbname = "OOS_" & wbname & ".csv"
Set wkbSource = Workbooks.Open("..\" & wbname)
wkbSource.SaveAs Filename:="..\OOS_today.csv", FileFormat:=xlCSV
wkbSource.Close SaveChanges:=False
Help please!

Resources