fork download
  1. Option Explicit
  2.  
  3. Const TextFolder As String = "D:\goo\data5" 'テキスト格納フォルダ
  4. Const BookFolder As String = "D:\goo\data7" 'excel Book格納フォルダ
  5. Dim array_file() As String 'テキストファイル名の配列
  6. Dim owb As Workbook '出力ブック
  7. Public Sub 全テキスト読込()
  8. Dim FSO As Object 'FileSystemObject
  9. Dim srcfolder As Object 'テキストファイルフォルダ
  10. Dim wfiles As Object 'テキストファイル一覧
  11. Dim wfile As Object 'テキストファイル
  12. Dim file_count As Long 'テキストファイルの件数
  13. Dim book_count As Long 'Bookの件数
  14. Dim book_no As Long 'ブック番号
  15. Dim sheet_no As Long 'シート番号
  16. Dim save_count As Variant '退避領域
  17. Dim book_name As String '出力ブック名
  18. Dim ret As Boolean '戻り値
  19. Set FSO = CreateObject("Scripting.FileSystemObject")
  20. Set srcfolder = FSO.GetFolder(TextFolder) 'テキストファイル格納フォルダ情報取得
  21. Set wfiles = srcfolder.Files 'ファイル一覧取得
  22. file_count = 0
  23. '拡張子が.txtのファイルのみ取得し、array_fileに格納する
  24. For Each wfile In wfiles
  25. If LCase(Right(wfile.name, 4)) = ".txt" Then
  26. ReDim Preserve array_file(file_count)
  27. array_file(file_count) = wfile.name
  28. file_count = file_count + 1
  29. End If
  30. Next
  31. '出力するBookの件数を求める
  32. book_count = file_count \ 100
  33. 'テキストファイル件数が100で割り切れないならBook件数に1加算
  34. If file_count Mod 100 > 0 Then
  35. book_count = book_count + 1
  36. End If
  37. 'Book作成時のワークシートの数を退避
  38. save_count = Application.SheetsInNewWorkbook
  39. 'Book作成時のワークシートの数を100に設定
  40. Application.SheetsInNewWorkbook = 100
  41. '1~Book件数まで繰り返す
  42. For book_no = 1 To book_count
  43. Set owb = Workbooks.Add
  44. 'シート番号を1~100迄繰り返す
  45. For sheet_no = 1 To 100
  46. '1シート分を作成する
  47. ret = set_sheet(book_no, sheet_no)
  48. 'テキストファイル数の上限を超えているなら打ち切る
  49. If ret = False Then Exit For
  50. Next
  51. 'ブック名をBook+連番で出力する
  52. book_name = BookFolder & "\Book" & book_no & ".xlsx"
  53. owb.SaveAs Filename:=book_name
  54. owb.Close
  55. Next
  56. '退避したBook作成時のワークシートの数を戻す
  57. Application.SheetsInNewWorkbook = save_count
  58. MsgBox ("完了")
  59. End Sub
  60.  
  61. 'ブック番号(1~N)とシート番号(1~100)を元に、該当シートへテキストファイルを読み込む
  62. Private Function set_sheet(ByVal book_no As Long, ByVal sheet_no As Long) As Boolean
  63. Dim i As Long
  64. Dim fname As String 'テキストファイル名(フルパス)
  65. Dim fileNo As Long 'ファイル番号
  66. Dim lno As Long '行番号
  67. Dim text As String '読み込んだテキスト
  68. Dim RE As Object '正規表現オブジェクト
  69. Dim sname As String 'シート名
  70. Set RE = CreateObject("VBScript.RegExp")
  71. '1行が空白行か否かの判定用
  72. RE.Pattern = "^\s*$"
  73. RE.Global = True
  74. set_sheet = False
  75. 'array_fileの何番目かを求める
  76. i = (book_no - 1) * 100 + sheet_no - 1
  77. '上限を超えているならFlaseで終了
  78. If i > UBound(array_file) Then Exit Function
  79. 'テキストファイル名及びその拡張子を除いたものを取得
  80. fname = TextFolder & "\" & array_file(i)
  81. sname = array_file(i)
  82. sname = left(sname, Len(sname) - 4)
  83. fileNo = FreeFile
  84. Open fname For Input As #fileNo
  85. 'ファイル終端まで読み込む
  86. lno = 0
  87. Do Until EOF(fileNo)
  88. '1行読み込み
  89. Line Input #fileNo, text
  90. lno = lno + 1
  91. '空白行なら終了
  92. If RE.test(text) = True Then Exit Do
  93. 'A列の該当行へ設定
  94. owb.Worksheets(sheet_no).Cells(lno, "A").Value = text
  95. Loop
  96. Close #fileNo
  97. 'シート名を設定
  98. owb.Worksheets(sheet_no).name = sname
  99. '正常終了
  100. set_sheet = True
  101. End Function
  102.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty