Add date and cell value to export code

slayer1957

Board Regular
Joined
Jan 9, 2017
Messages
50
Hi all,
I need to just add todays date and cell value "S2" to my current code below. I have small section to add but cannot get it right and cell value still also needs to be added. Can someone please help.

Code:
_" & _Format(Date, "ddmmmyyyy") & Cell.value "S2" & ".xlsm"


VBA Code:
Sub EXPORT()


    Dim mySourceWB As Workbook
    Dim mySourceSheet As Worksheet
    Dim myDestWB As Workbook
    Dim myNewFileName As String
    
'   First capture current workbook and worksheet
    Set mySourceWB = ActiveWorkbook
    Set mySourceSheet = ActiveSheet


'   Build new file name based
    myNewFileName = mySourceWB.Path & "\" & mySourceSheet.Name & ".xlsx"


'   Add new workbook and save with name of sheet from other file
    Workbooks.Add
    ActiveWorkbook.SaveAs Filename:=myNewFileName
    Set myDestWB = ActiveWorkbook
    
'   Copy over sheet from previous file
    mySourceWB.Activate
    Cells.Copy
    myDestWB.Activate
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
'   Resave new workbook
    ActiveWorkbook.Save


End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try:
VBA Code:
 & Format(Date, "ddmmmyyyy") & Range("S2").Value & ".xlsm"
 
Upvote 0
Solution
It is giving runt time error, extension cannot be used for selected file type, change file extension int he file name text box or select a different file type by chnaging the save as type and flags line" ActiveWorkbook.SaveAs Filename:=myNewFileName"
 
Upvote 0
Note that is you have an "xlsx" file type being saved as an "xlsm" file type, or vice versa, then you are changing file types and need to include that file format argument in your SaveAs command.
If you turn on the Macro Recorder and record yourself manually doing a SaveAs with your desired file type, you will see what that line of code needs to look like, i.e.
Rich (BB code):
    ActiveWorkbook.SaveAs Filename:="file name here" _
        , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
 
Upvote 0
Got it, thanks for the help!

VBA Code:
myNewFileName = mySourceWB.Path & "\" & mySourceSheet.Name & "_" & Format(Date, "dd mmm yyyy") & "_" & Range("S2").Value & ".xlsx"
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top