0

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
Community
  • 1
  • 1
user2778021
  • 19
  • 1
  • 1
  • 2
  • 1
    Not sure if it's relevant but surely you'd want to change weboptions.encoding for NewBook not activeworkbook and you'd want to do it before the save not after. – Harassed Dad Nov 16 '17 at 15:13
  • related: https://stackoverflow.com/questions/48222148/convert-xlsx-to-csv-utf-8-format – matth May 25 '18 at 11:43
  • `I go to Notepad++ and check encoding` - UTF-8 is indistinguishable from ASCII for the first 128 code points. If your file only contains those, Notepad++ will have no way to know if it's ASCII or UTF-8. – GSerg Nov 22 '19 at 09:44

1 Answers1

0

Change Fileformat to xlCSVUTF8

Tarun Reddy
  • 1,315
  • 1
  • 8
  • 6