在excel中,如何将每行数据的输出为一个独立的文本文件txt

2024-10-30 13:32:52
有3个网友回答
网友(1):

在excel中,将每行数据的输出为一个独立的文本文件txt可用如下代码可以实现:

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False '关闭屏幕刷新
Dim nRow&, ncolumn& '定义区域变量
nRow = Cells(Rows.Count, 1).End(3).Row '确定最后行行号
For i = 1 To nRow '在行中进行循环
ncolumn = Cells(i, Columns.Count).End(1).Column '确定每行最后列列号
Open ThisWorkbook.Path & "\文件" & i & ".txt" For Output As #1 '创建文本进行输入内容
Print #1, Join(Application.Transpose(Application.Transpose(Range(Cells(i, 1), Cells(i, ncolumn)))), ",") '输入此行内容,以","分隔内容
Close #1 '关闭文本
Next '下次循环
Application.ScreenUpdating = True '开启屏幕刷新,与前对应
MsgBox "导出完成" '完成友好提示
End Sub

网友(2):

Sub 每一行保存为一个文本文件()
Dim Path As String
Dim yWb As Workbook
Dim xSh As Worksheet
Dim tRan As Range
Path = "E:\" '保存的路径
Set xSh = ActiveSheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each tRan In xSh.Range("A:A")
If tRan <> "" Then
Set yWb = Workbooks.Add
tRan.EntireRow.Copy yWb.Worksheets(1).Range("A1")
yWb.SaveAs Path & tRan.Row & ".txt", xlText
yWb.Saved = False
yWb.Close
Else
Exit For
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

网友(3):

在所打的内容前面加一个'