我正在尝试将我的Excel数据导出到每行的单个文本文件中。我已经查看了各种SO问题,并且可以获得每行的工作结果,但无法从B列的所有列中获取内容并转发。这是我尝试过的。
这是来自VBA编程的tut。它输出我的所有列,但只输出一个文件:
Sub toXPD()
Dim FilePath As String
Dim CellData As String
Dim LastCol As Long
Dim LastRow As Long
LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
CellData = ""
FilePath = Application.DefaultFilePath & "\auth.csv"
Open FilePath For Output As #2
For i = 1 To LastRow
For j = 1 To LastCol
If j = LastCol Then
CellData = CellData + Trim(ActiveCell(i, j).Value)
Else
CellData = CellData + Trim(ActiveCell(i, j).Value) + ","
End If
Next j
Write #2, CellData
CellData = ""
Next i
Close #2
MsgBox ("Done")
End Sub
我也从这个问题Create text Files from every row in an Excel Spreadsheet尝试了这个问题,它可以用于为每一行创建单独的文件,但只填充B列中的内容而不是其他内容:
Sub savemyrowsastext()
Dim x
For Each cell In Sheet1.Range("A1:A" & Sheet1.UsedRange.Rows.Count)
' you can change the sheet1 to your own choice
saveText = cell.Text
Open "C:\wamp\www\GeoPC_NG\sogistate\igala_land\" & saveText & ".php" For Output As #1
Print #1, cell.Offset(0, 1).Text
Close #1
Beep ' Sound a tone.
Next x
Next cell
End Sub
我对VBA / Macro编码非常陌生,并认为这将是一项简单的任务。我试图将第二组代码中缺少的内容与第一组代码中的内容拼凑在一起但没有任何效果。如何获取第二组代码以包含所有具有值作为每个文本文件内容的列?
编辑:Per @Kumarapush追加答案 内容确实在单独的行上打印,但输出如此。
"columnB"
"columnBcolumnC"
"columnBcolumnCcolumnD"
编辑2:这是遇到此问题的其他任何人的完整工作代码。它将列B中的所有列作为txt文件的内容打印,列A作为每个文件的标题打印。谢谢@Kumarapush
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
答案 0 :(得分:2)
我修改了第一个示例代码中的几行。它只是错过了一些逻辑。它不是写入不同的文件,而是继续写入同一个文件。
尝试以下代码。我测试了它,它将每行的数据写入不同的CSV文件。
Sub toXPD()
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 & "\auth" & i & ".csv"
Filenum = FreeFile
Open FilePath For Output As Filenum
CellData = ""
For j = 1 To LastCol
If j = LastCol Then
CellData = CellData + Trim(ActiveSheet.Cells(i, j).Value)
Else
CellData = CellData + Trim(ActiveSheet.Cells(i, j).Value) + ","
End If
Next j
Write #Filenum, CellData
Close #Filenum
Next i
MsgBox ("Done")
End Sub
答案 1 :(得分:2)
根据您的评论回复所需的修改。即,要将每个列数据写入输出文件中的单独行: 史蒂夫不是问题。替换此代码
For j = 1 To LastCol
If j = LastCol Then
CellData = CellData + Trim(ActiveSheet.Cells(i, j).Value)
Else
CellData = CellData + Trim(ActiveSheet.Cells(i, j).Value) + ","
End If
Next j
Write #Filenum, CellData
有了这个
For j = 1 To LastCol
CellData = Trim(ActiveSheet.Cells(i, j).Value)
Write #Filenum, CellData
Next j