VBA Save Copy as csv complete with name and datestamp, and some additional format

Flitbee

New Member
Joined
May 28, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi
i am new to VBA, and would like some help getting my table to save as a CSV, but with some additional columns added.

This table is a report that is generated out of 8 different sales forecasts:
1685322771850.png


the final CSV has to be in this specific format for importing:

1685322820931.png



the CSV needs to save to a specific location, with name and date stamp.

I would like to be able to refresh the table and run the Macro to save the report automatically in the correct format.

hopefully this is do-able?

thank you
Flitbee
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
The basic code can be generated with the macro recorder (select and copy the whole table, paste into a new workbook, insert the extra columns and headers and save as CSV) and then edited to merge the Select and Selection statements, determine the last row in the table (needed to fill column A with 'Stock item') and specify the file name and date stamp.

You'll need to change the SaveAs folder path and file name in this code.
VBA Code:
Public Sub Save_Table_As_CSV()
    
    Dim n As Long
    
    n = ActiveSheet.ListObjects(1).Range.Rows.Count
    ActiveSheet.ListObjects(1).Range.Copy
    Workbooks.Add
    ActiveSheet.Paste
    
    Columns("B:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B1:C1").Value = Array("KEY 2", "KEY 3")
    Columns("A:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1:C1").Value = Array("SEQUENCE 1", "SEQUENCE 2", "SEQUENCE 3")
    Range("A2:A" & n).Value = "Stock item"
    
    Application.DisplayAlerts = False 'suppress warning if csv file already exists
    ActiveWorkbook.SaveAs Filename:="C:\path\to\folder\File name " & Format("YYYYMMDD") & ".csv", FileFormat:=xlCSV, CreateBackup:=False
    Application.DisplayAlerts = True
    ActiveWorkbook.Close SaveChanges:=False
    
End Sub
 
Upvote 1
Solution
Thank you John_W.

This is great. The macro doesn't give the opportunity to add the date. is there a specific code for this?
 
Upvote 0
Sorry, the SaveAs line, to use the current date should be:
VBA Code:
    ActiveWorkbook.SaveAs Filename:="C:\path\to\folder\File name " & Format(Date, "YYYYMMDD") & ".csv", FileFormat:=xlCSV, CreateBackup:=False
 
Upvote 1
Thank you John_w.
truely appreciated.
one last quetsion.
is it possible to include a time stamp also?
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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