我在Excel中有一个包含450行左右的源电子表格。每行有6列数据,我需要从每一行创建一个单独的文件,文件名= A列,内容= B-G列,它们之间有换行符。
例如,我正在尝试此操作,但收到错误“未找到文件”:
Sub DataDump()
Dim X
Dim lngRow As Long
Dim StrFolder As String
StrFolder = "/Users/danielfowler/Documents/_users_text_6.16"
X = Range([a1], Cells(Rows.Count, 2).End(xlUp))
For lngRow = 1 To UBound(X)
Open StrFolder & "\" & X(lngRow, 1) & ".txt" For Output As #1
Write #1, X(lngRow, 2)
Close #1
Next
End Sub
我在StackOverflow上已经看到了这样的六个问题......
但是这些解决方案中的每一个都会为我返回一个不同的错误。我正在使用Excel for Mac 2011,v14.4.2。
答案 0 :(得分:0)
Sub VBA_Print_to_a_text_file()
Dim strFile_Path As String
strFile_Path = "C:\temp\test.txt" ‘Change as per your test folder path
Open strFile_Path For Output As #1
Print #1, "This is my sample text"
Close #1
End Sub
答案 1 :(得分:0)
这将为每行输出一个文本文件,其中A列为标题,B列为最后一列,作为每个文件的内容。您可以将目录更改为您想要的目录,但目前它将文本文件保存到与Excel文件相同的目录中。您还可以将文件扩展名更改为您想要的任何内容。
Sub toFile()
Dim FilePath As String, CellData As String, LastCol As Long, LastRow As Long
Dim Filenum As Integer
LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
For i = 1 To LastRow
FilePath = Application.DefaultFilePath & "\" & Trim(ActiveSheet.Cells(i, 1).Value) & ".xpd"
Filenum = FreeFile
Open FilePath For Output As Filenum
CellData = ""
For j = 2 To LastCol
CellData = Trim(ActiveSheet.Cells(i, j).Value)
Write #Filenum, CellData
Next j
Close #Filenum
Next i
MsgBox ("Done")
End Sub
至于每一行之间的休息时间,遗憾的是我没有足够的经验知道如何做到这一点。