VBA-Export to CSV with File name and a referenced date

BrookssXX

New Member
Joined
Mar 24, 2022
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Hello,
I am trying to create VBA to export a loaded power query sheet to CSV. When I add the date section I receive an error message. The portion in red is throwing me off today. Suggestions please :)

Sub Export()

Dim MyFileName As String
Dim CurrentWB As Workbook, TempWB As Workbook

Set CurrentWB = ActiveWorkbook
ActiveWorkbook.ActiveSheet.UsedRange.Copy

Set TempWB = Application.Workbooks.Add(1)
With TempWB.Sheets(1).Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
End With

'Dim Change below to "- 4" to become compatible with .xls files

MyFileName = CurrentWB.Path & "\" & "CJ74+CJI5 Export_CHANGE DATE" & Format(Sheets("Path-Dates").Range("CuteOffDate").Value, "ddmmmyy") & ".csv"

Application.DisplayAlerts = False
TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSV, CreateBackup:=False, Local:=True
TempWB.Close SaveChanges:=False
Application.DisplayAlerts = True


End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
The reference to the workbook was missing:

Rich (BB code):
MyFileName = CurrentWB.Path & "\" & "CJ74+CJI5 Export_CHANGE DATE" & Format(CurrentWB.Sheets("Path-Dates").Range("CuteOffDate").Value, "ddmmmyy") & ".csv"
 
Upvote 0
Solution
The reference to the workbook was missing:

Rich (BB code):
MyFileName = CurrentWB.Path & "\" & "CJ74+CJI5 Export_CHANGE DATE" & Format(CurrentWB.Sheets("Path-Dates").Range("CuteOffDate").Value, "ddmmmyy") & ".csv"


Thank you very much!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

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