VBA to create .csv

jas1980

New Member
Joined
Oct 14, 2016
Messages
39
Office Version
  1. 2019
Platform
  1. Windows
I have tried to use this VBA code:

Sub csvcreate()
Workbooks.Add.SaveAs fileName:=Range("P2"), FileFormat:=xlCSV, _
CreateBackup:=False
End Sub

to create a csv file, in P2 is this address:
C:\Users\jamie\AppData\Roaming\MetaQuotes\Terminal\06980EAAF7FCC460D7BD131105CC8618\MQL4\Files\NF\NF.csv
and is created by changeable values using an & formula, this gives me an error and after many different tries I cannot get to work. Could someone please show me what I need to add or change.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Why are you saving the file to such an obscure location ?

Here is a sample code from my toolbox :

VBA Code:
Option Explicit


Sub test()
Dim TempWB As Workbook
Dim fd As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
ActiveSheet.Copy
Set TempWB = ActiveWorkbook

fd = "C:\Users\My\Desktop"   'Edit path as required. Assumes the folder Test already exists on the desktop
'fd now holds the path to the folder
 
With TempWB
'Exchange Sheet is the name of the saved CSV file. Edit name as required.
    .SaveAs Filename:=fd & "\Exchange_Sheet", FileFormat:=xlCSVWindows, CreateBackup:=False
    .Close
End With

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Why are you saving the file to such an obscure location ?

Here is a sample code from my toolbox :

VBA Code:
Option Explicit


Sub test()
Dim TempWB As Workbook
Dim fd As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
ActiveSheet.Copy
Set TempWB = ActiveWorkbook

fd = "C:\Users\My\Desktop"   'Edit path as required. Assumes the folder Test already exists on the desktop
'fd now holds the path to the folder

With TempWB
'Exchange Sheet is the name of the saved CSV file. Edit name as required.
    .SaveAs Filename:=fd & "\Exchange_Sheet", FileFormat:=xlCSVWindows, CreateBackup:=False
    .Close
End With

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Thank you I will see if I can get this code to work for me. The location is created by an MT4 expert advisor but, sometimes I need to create to add data so the EA can still access, bit of a pain but if used on a different PC then the xl docs still need to access.
 
Upvote 0
Why are you saving the file to such an obscure location ?

Here is a sample code from my toolbox :

VBA Code:
Option Explicit


Sub test()
Dim TempWB As Workbook
Dim fd As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
ActiveSheet.Copy
Set TempWB = ActiveWorkbook

fd = "C:\Users\My\Desktop"   'Edit path as required. Assumes the folder Test already exists on the desktop
'fd now holds the path to the folder

With TempWB
'Exchange Sheet is the name of the saved CSV file. Edit name as required.
    .SaveAs Filename:=fd & "\Exchange_Sheet", FileFormat:=xlCSVWindows, CreateBackup:=False
    .Close
End With

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Thank you very much for this code it works perfectly
 
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,479
Members
452,516
Latest member
archcalx

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