我最终将以下内容拼接在一起来解决我的问题,这完全感谢@David 和@Exactabox。它效率极低并且有冗余位,但它运行(非常。缓慢)。如果有人知道如何清理它,请随意,否则它可以完成工作。
[编辑] 不幸的是,我现在意识到虽然这个宏将每一行导出为一个适当命名的新 .txt 文件,但每个文本文件的内容是文档的最后一行。因此,即使它将所有 20 行导出为 20 个具有适当文件名和正确格式的 .txt 文件,这 20 个文件中每个文件的实际内容都是相同的。我不确定如何纠正这个问题。
Sub SaveRowsAsTXT()
Dim wb As Excel.Workbook, wbNew As Excel.Workbook
Dim wsSource As Excel.Worksheet, wsTemp As Excel.Worksheet
Dim r As Long, c As Long
Dim filePath As String
Dim fileName As String
Dim rowRange As Range
Dim cell As Range
filePath = "C:\filepath\"
For Each cell In Range("B1", Range("B1048576").End(xlUp))
Set rowRange = Range(cell.Address, Range(cell.Address).End(xlToRight))
fileName = filePath & cell.Offset(0, -1).Value
Set wsSource = ThisWorkbook.Worksheets("Sheet1")
Application.DisplayAlerts = False 'will overwrite existing files without asking
r = 1
Do Until Len(Trim(wsSource.Cells(r, 1).Value)) = 0
ThisWorkbook.Worksheets.Add ThisWorkbook.Worksheets(1)
Set wsTemp = ThisWorkbook.Worksheets(1)
For c = 2 To 16
wsTemp.Cells((c - 1) * 2 - 1, 1).Value = wsSource.Cells(r, c).Value
Next c
wsTemp.Move
Set wbNew = ActiveWorkbook
Set wsTemp = wbNew.Worksheets(1)
wbNew.SaveAs fileName & ".txt", xlTextWindows 'save as .txt
wbNew.Close
ThisWorkbook.Activate
r = r + 1
Loop
Application.DisplayAlerts = True
Next
End Sub