0

I have an excel file which has a macro which saves the file as a .CSV file. I have a program that calls on the CSV file but the program only recognizes the .CSV file if I open it manually and save it. Then after doing so, the program recognizes the file and data.

I've used Google Sheets for years and just download to CSV and this has worked. I wanted to automate this process using Excel but seem to have found some weird issue.

The macro runs and saves the Excel Sheet to the CSV file and when opening it everything in the file is correct. Again the program calling the file will only recognize it if I manually open it and physically save it.

I can't figure out the issue. Does any have any input it would be greatly appreciated. Below is the macro I'm using.

Public Sub ExportWorksheetAndSaveAsCSV()

    Dim wbkExport As Workbook
    Dim shtToExport As Worksheet

    Set shtToExport = ThisWorkbook.Worksheets("nqdev")     'Sheet to export as CSV
    Set wbkExport = Application.Workbooks.Add
    shtToExport.Copy Before:=wbkExport.Worksheets(wbkExport.Worksheets.Count)
    Application.DisplayAlerts = False                       'Possibly overwrite without asking
    wbkExport.SaveAs FileName:="H:\My Drive\KML\TOTEliteMembers\NQDev.csv", FileFormat:=xlCSV
    Application.DisplayAlerts = True
    wbkExport.Close SaveChanges:=True

End Sub
4
  • Have you looked at the CSV file in a text editor before and after you "open and save it" like you mention you have to do before it works to see what might have changed? This will go a long way towards allowing you to see what you need to do in order to solve this problem. Commented Mar 28, 2022 at 1:53
  • Can you elaborate on what you mean "I have a program that calls on the CSV file but the program only recognizes the .CSV file if I open it manually and save it. Then after doing so, the program recognizes the file and data." Perhaps elaborate step-by-step the process when you do it manually vs. when its automatic, noting where the error occurs and what exactly the error is. Commented Mar 28, 2022 at 9:07
  • Your step by step description should answer: how does the other program "call" on the file? Is it a script continuously scanning for new csv files? Does it call the file from the command line e.g. "program.exe test.csv"? When manually opening, do you mean double-click the csv in Explorer or "File > Open" from within your program? Have you checked the contents of the CSV in notepad. Is it actually a CSV text file or just an excel file with a renamed extension. Do you manually open the macro-made csv file in Excel or in your other program? What is the Windows default program for CSV files? Commented Mar 28, 2022 at 9:19
  • Thanks for the replies I’ve checked the file and it is correct before and after. The program that calls on the csv file is the NinjaTrader Trading platform. I’ve been using Google Sheets and manually downloading the file to a csv file for the trading platform to call on. I used the excel macro to try and automate the process. I will do a few checks and see if I can narrow it down any more. Thanks again for all the replies. James Commented Mar 29, 2022 at 3:21

1 Answer 1

0

I always use this code when saving a file, works wonders and you can change the delimiter to whatever you desire to use.

Importante note: you need to change the Range to the one you're using, then change the "delimiter = |" just use ; for csv, and the Path in the Open.. line

Dim r As Range: Set r = Range("A1:P15") 
Dim buffer As String, delimiter As String, c As Range
Dim i As Long

For Each c In r
    If (i Mod r.Columns.Count) = 0 Then
        If (Len(buffer)) Then delimiter = vbCrLf
    Else
        delimiter = |
    End If
    buffer = buffer & delimiter & """" & CStr(c.Text) & """" '// c.value or c.text to preserve formatting
    i = (i + 1)
Next

Open "D:\YourFolder\Yourname.csv" For Output As #1
Print #1, buffer
Close #1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .