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