ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   using macro to save worksheet with unique file name (https://www.excelbanter.com/excel-programming/279688-using-macro-save-worksheet-unique-file-name.html)

Noell

using macro to save worksheet with unique file name
 
I am trying to write a macro to save a worksheet with a
unique file name, preferably using a cell value, every
time the macro is used. Can anyone help me?

Harald Staff

using macro to save worksheet with unique file name
 
Hi

Why would a cell value be unique whenever you run the code ? Anyway, here's one that uses
a cell and also date/time. Unique unless you run it twice in the same second:

Function UniqName() As String
UniqName = Sheets(2).Range("B3").Value & _
Format(Now, "yymmddhhmmss") & ".xls"
End Function

Sub test()
MsgBox "Save me as " & UniqName
End Sub


--
HTH. Best wishes Harald
Followup to newsgroup only please.

"Noell" wrote in message
...
I am trying to write a macro to save a worksheet with a
unique file name, preferably using a cell value, every
time the macro is used. Can anyone help me?




Cliff Myers

using macro to save worksheet with unique file name
 
Activeworkbook.saveas cells(1, 1)
whatever is cell A1 will now be the filename. You can enter that into the
workbook_beforeclose event and that way before the workbook is closed it
will save it as the new name.

"Noell" wrote in message
...
I am trying to write a macro to save a worksheet with a
unique file name, preferably using a cell value, every
time the macro is used. Can anyone help me?




Ron de Bruin

using macro to save worksheet with unique file name
 
Hi Noell

You can use the date and time as a part of the filename

Sub Test()
Dim wb As Workbook
Dim strdate As String
strdate = Format(Now, "dd-mm-yy h-mm-ss")
Set wb = ActiveWorkbook
With wb
.SaveAs Sheets("Sheet1").Range("A1").Value _
& " " & strdate & ".xls"
'wb.Close False
End With
End Sub


--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl



"Noell" wrote in message ...
I am trying to write a macro to save a worksheet with a
unique file name, preferably using a cell value, every
time the macro is used. Can anyone help me?





All times are GMT +1. The time now is 02:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com