从 .csv 写入 txt 文件,但列之间的模式不同 space
Writing from .csv to txt file but with differing space patterns between the columns
我有一个包含 4 列的 .csv 文件。我想将我的 excel 数据放入 txt 文件中,但是,我希望在 txt 文件中的列之间有不同的间距选项。
示例 - 如果第 1 行有四列是[列 a = 2 列 b = 3,列 c = 4,列 d = 5],文本文件中的输出将是:
2 3 4 5
2和3之间有一个制表符,3和4之间有四个空格,4和5之间有14个空格。这很随机,但格式是由于以前创建的文件。
我根据教程编写了以下代码,但不确定如何操作它以获得每行的不同间距。
Sub excelToTxt()
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 = vbTab
FilePath = Application.DefaultFilePath & "\test.txt"
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 = Trim(ActiveCell(i, j).Value) + CellData
End If
Next j
Write #2, CellData
CellData = vbTab
Next i
Close #2
End
有人能帮忙解决这个问题吗?
您将必须修改写出值的部分。检查您正在写出的列并在列之间添加您需要的值。
像这样。
For j = 1 To LastCol
If j = LastCol Then
CellData = CellData + Trim(ActiveCell(i, j).Value)
Elseif j = 1 Then
CellData = Trim(ActiveCell(i, j).Value) + CellData
Elseif j = 2 Then
CellData = Trim(ActiveCell(i, j).Value) + vbTab
Elseif j = 3 Then
CellData = Trim(ActiveCell(i, j).Value) + " "
Elseif j = 4 Then
CellData = Trim(ActiveCell(i, j).Value) + " "
Elseif j = 5 Then
CellData = Trim(ActiveCell(i, j).Value) + " "
End If
Next j
Write #2, CellData
CellData = vbTab
您可以使用类似的东西:
Dim spacing As Variant
Select Case Cells(i, j).Column
Case 1: spacing = vbTab
Case 2: spacing = Space(4) - Len(Cells(i, j).Value)
Case 3: spacing = Space(14) - Len(Cells(i, j).Value)
End Select
Write #2, Cells(i, j).Value & spacing
我有一个包含 4 列的 .csv 文件。我想将我的 excel 数据放入 txt 文件中,但是,我希望在 txt 文件中的列之间有不同的间距选项。
示例 - 如果第 1 行有四列是[列 a = 2 列 b = 3,列 c = 4,列 d = 5],文本文件中的输出将是:
2 3 4 5
2和3之间有一个制表符,3和4之间有四个空格,4和5之间有14个空格。这很随机,但格式是由于以前创建的文件。
我根据教程编写了以下代码,但不确定如何操作它以获得每行的不同间距。
Sub excelToTxt()
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 = vbTab
FilePath = Application.DefaultFilePath & "\test.txt"
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 = Trim(ActiveCell(i, j).Value) + CellData
End If
Next j
Write #2, CellData
CellData = vbTab
Next i
Close #2
End
有人能帮忙解决这个问题吗?
您将必须修改写出值的部分。检查您正在写出的列并在列之间添加您需要的值。
像这样。
For j = 1 To LastCol
If j = LastCol Then
CellData = CellData + Trim(ActiveCell(i, j).Value)
Elseif j = 1 Then
CellData = Trim(ActiveCell(i, j).Value) + CellData
Elseif j = 2 Then
CellData = Trim(ActiveCell(i, j).Value) + vbTab
Elseif j = 3 Then
CellData = Trim(ActiveCell(i, j).Value) + " "
Elseif j = 4 Then
CellData = Trim(ActiveCell(i, j).Value) + " "
Elseif j = 5 Then
CellData = Trim(ActiveCell(i, j).Value) + " "
End If
Next j
Write #2, CellData
CellData = vbTab
您可以使用类似的东西:
Dim spacing As Variant
Select Case Cells(i, j).Column
Case 1: spacing = vbTab
Case 2: spacing = Space(4) - Len(Cells(i, j).Value)
Case 3: spacing = Space(14) - Len(Cells(i, j).Value)
End Select
Write #2, Cells(i, j).Value & spacing