CSVなど表形式のデータをLDIFみたいな形式に変換するためのマクロ

要はCSVなんかの表を項目ごとに括って縦に並べるだけのマクロです。

たまに使おうと思ったとき1から作成するのが面倒なのでメモ。

Option Explicit
  Sub test()
  Dim varValueData() As Variant '//2次元配列データ
  Dim strRowData(1000) As String '//1行のデータを連結した1次元配列
  Dim strTemp As String
  Dim m As Long
  Dim n As Long
  Dim h As Long
  varValueData = ThisWorkbook.Worksheets("sheet1").Range("A1:E25").Value
  '取得できた全データ書き出しと連結格納
  'ReDim strRowData(LBound(varValueData, 1) To UBound(varValueData, 1))
  h = 1
  For m = LBound(varValueData, 1) To UBound(varValueData, 1)
  For n = LBound(varValueData, 2) To UBound(varValueData, 2)
  h = h + 1
  strTemp = ""
  Debug.Print "[" & CStr(m) & ":" & CStr(n) & "]" & CStr(varValueData(m, n))
  strTemp = CStr(varValueData(m, n))
  strRowData(h) = strTemp
  Debug.Print strTemp
  Call actPrint(strTemp)
  Next
  Next

 End Sub

 Function actPrint(strTemp As String)
  Dim intFF As Integer ' FreeFile値
  Const cnsFILENAME = "\SAMPLE.txt"
  ' FreeFile値の取得(以降この値で入出力する)
  intFF = FreeFile
  ' 指定ファイルをOPEN(出力モード)
  Open ThisWorkbook.Path & cnsFILENAME For Append As #intFF
  Print #intFF, strTemp
  Close #intFF
  Debug.Print strTemp

 End Function