此主題已得出結論:我是一個初學者,我可以工作 - 如果您需要調整簡單的東西,您可能想要閱讀所有這些在這裏說...將excel導出爲csv的工作VBA UTF8
的溶液在這篇文章的底部抄...
原始任務: 這是一個更好的擅長以CSV在UTF8解決方案,我能找到那裏。大多數人想要安裝插件或不必要地使該過程複雜化。其中有很多。
一個問題已經解決。 (如何導出使用的行而不是預定義的數字)
剩下的是調整一些東西。
Case Excel
A1=Cat, B1=Dog
A2=empty B2=Empty
A3=Mouse B3=Bird
當前腳本出口
貓,狗
老鼠,鳥
請告訴我需要的是
"Cat","Dog"
,
"Mouse","Bird"
代碼:
Public Sub WriteCSV()
Set wkb = ActiveSheet
Dim fileName As String
Dim MaxCols As Integer
fileName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
If fileName = "False" Then
End
End If
On Error GoTo eh
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Charset = "UTF-8"
BinaryStream.Type = adTypeText
BinaryStream.Open
For r = 1 To 2444
s = ""
C = 1
While Not IsEmpty(wkb.Cells(r, C).Value)
s = s & wkb.Cells(r, C).Value & ","
C = C + 1
Wend
If Len(s) > 0 Then
s = Left(s, Len(s) - 1)
End If
BinaryStream.WriteText s, 1
Next r
BinaryStream.SaveToFile fileName, adSaveCreateOverWrite
BinaryStream.Close
MsgBox "CSV generated successfully"
eh:
End Sub
SOLUTION: (請注意,您可以預先通過用數字替換wkb.UsedRange.Rows.Count定義的行數 - 同樣的柱子,和做其他小的調整應該需要) 如果你想一個預先定義的文件路徑放在空引號後的文件名= Application.GetSaveAsFilename( 「」
Public Sub WriteCSV()
Set wkb = ActiveSheet
Dim fileName As String
Dim MaxCols As Integer
fileName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
If fileName = "False" Then
End
End If
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Charset = "UTF-8"
BinaryStream.Type = adTypeText
BinaryStream.Open
For r = 1 To wkb.UsedRange.Rows.Count
S = ""
sep = ""
For c = 1 To wkb.UsedRange.Columns.Count
S = S + sep
sep = ","
If Not IsEmpty(wkb.Cells(r, c).Value) Then
S = S & """" & wkb.Cells(r, c).Value & """"
End If
Next
BinaryStream.WriteText S, 1
Next r
BinaryStream.SaveToFile fileName, adSaveCreateOverWrite
BinaryStream.Close
MsgBox "CSV generated successfully"
eh:
End Sub
用途:'當r = 1〜wkb.UsedRange.Rows' – Fratyx 2014-10-16 10:51:07
不起作用。你必須做「對於r = 1到wkb.UsedRange.Rows.Count」,任何方式你可以發佈它,所以我可以接受答案並解決線程。 – helena4 2014-10-16 13:34:13
你是對的。我沒有寫出想法,因爲我沒有Excel來測試它。但我會按照你的邀請:-) – Fratyx 2014-10-16 13:36:30