我借用了有关如何将每行电子表格导出到单独的.txt文件并将其调整为包含标题行的代码。如何在表单中添加所有86列。
Sub SaveWorksheet()
Dim MyWorkbook As Workbook
Dim MyDataWorksheet As Worksheet
Set MyWorkbook = Workbooks(ActiveWorkbook.Name)
Set MyDataWorksheet = MyWorkbook.Sheets("Data")
Dim OutputFile As String
Dim CellValue As String
Dim CurrentRow As Long
Dim CurrentCol As Long
Dim CurrentCharacter As Long
Dim LastRow As Long
Dim MyString As String
LastRow = MyDataWorksheet.Cells(Rows.Count, "a").End(xlUp).Row
For CurrentRow = 2 To LastRow
'C:\Regan\regan.xlsm
OutputFile = "C:\Regan\sample" & CurrentRow & ".txt"
Open OutputFile For Output As #1
CellValue = MyDataWorksheet.Cells(1, 1).Value & vbTab & vbCrLf & MyDataWorksheet.Cells(CurrentRow, 1).Value & vbTab
'Write #1, CellValue
Print #1, CellValue
Close #1
Next CurrentRow
MsgBox "Done"
End Sub
答案 0 :(得分:1)
以下代码遍历86列中的每一列:
Sub SaveWorksheet()
Dim MyWorkbook As Workbook
Dim MyDataWorksheet As Worksheet
Set MyWorkbook = Workbooks(ActiveWorkbook.Name)
Set MyDataWorksheet = MyWorkbook.Sheets("Data")
Dim OutputFile As String
Dim CellValue As String
Dim CurrentRow As Long
Dim CurrentCol As Long
Dim CurrentCharacter As Long
Dim LastRow As Long
Dim MyString As String
LastRow = MyDataWorksheet.Cells(Rows.Count, "a").End(xlUp).Row
For CurrentRow = 2 To LastRow
OutputFile = "C:\Regan\sample" & CurrentRow & ".txt"
Open OutputFile For Output As #1
'Write header record
For CurrentCol = 1 To 86
Print #1, MyDataWorksheet.Cells(1, CurrentCol).Value & vbTab;
Next
Print #1,""
'Write data record
For CurrentCol = 1 To 86
Print #1, MyDataWorksheet.Cells(CurrentRow, CurrentCol).Value & vbTab;
Next
Print #1,""
Close #1
Next CurrentRow
MsgBox "Done"
End Sub