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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
4,150
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
 
Solution

jas1980

New Member
Joined
Oct 14, 2016
Messages
39
Office Version
  1. 2019
Platform
  1. Windows
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.
 

jas1980

New Member
Joined
Oct 14, 2016
Messages
39
Office Version
  1. 2019
Platform
  1. Windows
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
 

Forum statistics

Threads
1,141,402
Messages
5,706,232
Members
421,433
Latest member
yash0468

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
Top