简体   繁体   中英

Convert Excel data to CSV with UTF-8 using VBA

I have data on a worksheet - basically football tables.
I want to export the worksheet to a csv file with UTF8 encoding.
I have identical code on another sheet - and that does encode correctly to UTF8.
(I go to Notepad++ and check encoding - its always ANSI) The good one is UTF8.

Why does one macro work correctly, but another not?

My code on the sheet is:

Private Sub btHTMLUK_Click()
    Application.DisplayAlerts = False

    On Error Resume Next
    Kill "C:\news\work\eurofootballtables.csv"
    On Error GoTo 0

    Set NewBook = Workbooks.Add
    ThisWorkbook.Worksheets("eurofootballtables").Range("A1:I130").Copy
    NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
    NewBook.SaveAs Filename:="C:\News\work\eurofootballtables.csv",FileFormat _
      :=xlCSVMSDOS, CreateBackup:=False

    'even tried this:
    ActiveWorkbook.WebOptions.Encoding = msoEncodingUTF8

    Workbooks("eurofootballtables.csv").Close
End Sub

将文件格式更改为 xlCSVUTF8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM