シートに書かれた内容のファイルを作成する。
下のソースではセル区切りのファイルを作成する。
ソース
Function ファイル作成(FilePath, FileName, SheetName, OutputFileName)
Dim fs
Dim Row
Dim Col
Dim EndRow
Dim EndCol
Dim Content
Row = 1
EndRow = Workbooks(FileName).Sheets(SheetName).Cells.SpecialCells(xlCellTypeLastCell).Row
EndCol = Workbooks(FileName).Sheets(SheetName).Cells.SpecialCells(xlCellTypeLastCell).Column
Dim OutputPath
OutputPath = FilePath
Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FolderExists(OutputPath) Then
'出力フォルダがない場合
MkDir (OutputPath)
End If
Open OutputPath & "\" & OutputFileName For Output As #1 'ファイル番号1で新規作成
'行ループ
Do While True
If EndRow + 1 = Row Then
Exit Do
End If
Col = 1
Dim Str
Str = ""
'列ループ
Do While True
If EndCol + 1 = Col Then
Exit Do
End If
Content = Workbooks(FileName).Sheets(SheetName).Cells(Row, Col).Value
'ファイル出力
If IsEmpty(Content) Then
Str = Str + vbTab
Else
Str = Str + Content
End If
If Not EndCol = Col Then
Str = Str + vbTab
End If
Col = Col + 1
Loop
'ファイル出力
Print #1, Str
Row = Row + 1
Loop
Close #1 'ファイルクローズ
ファイル作成 = True
End Function