参照設定の方法がわからない方はこちら
https://qiita.com/daichi05w/items/c2d827df55bb298e965a
'参照設定:Microsoft ActiveX Data Object x.x Library「x.x」には最も数字が大きいものを選択する
Public Const HOZON_PASS As String = "" 'ファイル名を含む絶対パスを指定する
Sub main()
Dim info_table(0 To 1, 0 To 2) As String
Dim csv_string As String
info_table(0, 0) = Cells(1, 1).Value
info_table(0, 1) = Cells(1, 2).Value
info_table(0, 2) = Cells(1, 3).Value
info_table(1, 0) = Cells(2, 1).Value
info_table(1, 1) = Cells(2, 2).Value
info_table(1, 2) = Cells(2, 3).Value
csv_string = array_to_csv(info_table)
Call write_UTF8(csv_string, HOZON_PASS)
End Sub
Function array_to_csv(ByRef info_table() As String) As String
Dim i As Long
Dim j As Long
Dim csv_string As String
csv_string = ""
For i = 0 To UBound(info_table, 1)
For j = 0 To UBound(info_table, 2)
csv_string = csv_string & """" & info_table(i, j) & """" & ","
Next
csv_string = Left(csv_string, Len(csv_string) - 1)
csv_string = csv_string & vbCrLf
Next
array_to_csv = csv_string
End Function
Sub write_SJIS(ByVal csv_string As String, ByVal pass As String)
Dim objStream As ADODB.Stream
Set objStream = New ADODB.Stream
With objStream
.Open
.Type = adTypeText
.Charset = "Shift_JIS"
.WriteText csv_string
.SaveToFile pass, adSaveCreateOverWrite
.Close
End With
Set objStream = Nothing
End Sub
Sub write_UTF8(ByVal csv_string As String, ByVal pass As String)
Dim byteData() As Byte
Dim objStream As ADODB.Stream
Set objStream = New ADODB.Stream
With objStream
.Charset = "UTF-8"
.LineSeparator = adLF
.Open
.WriteText csv_string
.Position = 0
.Type = adTypeBinary
.Position = 3
byteData = .Read
.Close
.Open
.Write byteData
.SaveToFile pass, adSaveCreateOverWrite
.Close
End With
End Sub