Save as CSV with semicolon separator

后端 未结 6 1230
庸人自扰
庸人自扰 2021-01-17 17:06

I\'m currently use this function for saving, but I have a problem with it:

Private Sub spara()
    ActiveWorkbook.SaveAs Filename:=\"T:\\filepath+ ActiveWork         


        
相关标签:
6条回答
  • 2021-01-17 17:19

    I had been looking this up to help resolve a similar issue I was having, I had an excel sheet that I export to a csv file, this is then uloaded elsewhere but requires the use of semicolons rather than commas for the character seperation, this worked fine when i was manually exporting the file as i had already changed the following Control Panel >> Region and Language >> additonal settings >> List separator from comma to semicolon. But when i tried to automate via VBA it defaulted back to comma, to fix I added the local paprameter as suggested by Christian Sauer above which then picks up the fact that i have changed my regional settings.

    ActiveWorkbook.SaveAs Filename:="Filename.txt", FileFormat:=xlCSV, CreateBackup:=False, Local:=True
    

    Thanks to Christian for the pointer.

    0 讨论(0)
  • 2021-01-17 17:21

    It's quite possible this problem is not solvable using Excel VBA only. The problem is, while Excel Save As... uses machine locale define list separator value, Excel VBA always uses en-US locale, thus, it always uses , as a list separator.

    I would recommend saving a CSV and then use custom console app/script for postprocessing. There is plenty of CSV parsers available which can read a ,-csv and then save it as ;-csv.

    0 讨论(0)
  • 2021-01-17 17:31

    I solved it adding "SaveChanges:=False" when closing workbook

    With ActiveWorkbook
         .SaveAs Filename:="T:\filepath+ ActiveWorkbook.Name", FileFormat:=xlCSV, Local:=True
         .Close SaveChanges:=False
    End With
    
    0 讨论(0)
  • 2021-01-17 17:37

    Change Excel Options (Advanced, Editing options) to set Decimal separator to , (obviously (!))

    0 讨论(0)
  • 2021-01-17 17:39

    Which language does your Excel use? If your native language uses ";" as default, you can pass the parameter "local:=True"

    ActiveWorkbook.SaveAs Filename:="C:\Temp\Fredi.csv", FileFormat:=xlCSV, CreateBackup:=False, local:=True
    

    If not, your only choice is searching and replacing afterwards...

    0 讨论(0)
  • 2021-01-17 17:43

    For people having issues with this code

    dim wa as Workbook
    
    Workbooks.OpenText FileName:=path2file, _
    DataType:=xlDelimited, Semicolon:=True, Local:=True
    set wa = ActiveWorkbook
    
        wa.SaveAs FileName:=path2file, FileFormat:=xlCSV, _ ConflictResolution:=xlLocalSessionChanges, Local:=True
    
        wa.Close False
    

    The second line is really important, if wa.Close False is not there it will ask for approval to save, or if wa.Close True it will replace the ";" for ",".

    After going into local settings and making ";" the list delimiter, VBA was still separating with a ",". Modified the code to the above and it was done.

    Hope this throw some light for some

    0 讨论(0)
提交回复
热议问题