【发布时间】:2022-01-26 18:13:05
【问题描述】:
我使用此代码将我的列表保存为 CSV UTF-8 文件,但它保存为 CSV UTF-8 BOM 文件。
我需要它没有 BOM。我不太擅长编码,所以希望有人能帮我调整代码,所以它将我的列表保存为CSV UTF-8,而没有BOM。
这是我使用的代码:
Sub SaveList_To_CSV()
Dim MyPath As String, MyFileName As String, rng As Range
Dim C As Range
MyPath = Sheets("Dashboard").Range("B11").Value 'contain the save location for new file
MyFileName = Sheets("Dashboard").Range("B14").Value 'contain the name for new file
If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
ActiveWorkbook.Sheets("List_Sheet").Copy 'name of the sheet we want to save as CSV
Set rng = ActiveWorkbook.Sheets(1).UsedRange
rng.Offset(0, 1).Clear 'keep only the first column
For Each C In Selection
If Len(C) = 0 Then C.ClearContents
Next C
ActiveWorkbook.SaveAs FileName:=MyPath & MyFileName, FileFormat:=xlCSVUTF8, CreateBackup:=False
ActiveWorkbook.Close
Workbooks.Open FileName:=MyPath & MyFileName
ActiveWorkbook.Save
ActiveWorkbook.Close
MsgBox "List Export Successful!"
End Sub
【问题讨论】:
-
stackoverflow.com/questions/31435662/… 展示了一种从 UTF8 文件中删除 BOM 的方法或参见 stackoverflow.com/questions/4143524/…
标签: excel vba utf-8 export-to-csv