1

我在 Excel 中有一个包含 450 行左右的源电子表格。每行有 6 列数据,我需要从每一行创建一个单独的文件,文件名 = 列 A,内容 = 列 BG,它们之间有一个换行符。

例如,我正在尝试此操作,但收到错误“找不到文件”:

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。

4

2 回答 2

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 
于 2015-06-18T19:36:46.017 回答
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

至于每行之间的中断,不幸的是我没有足够的经验知道如何做到这一点。

于 2016-01-14T01:23:26.550 回答