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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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,214,399
Messages
6,119,279
Members
448,884
Latest member
chuffman431a

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