导出excel到csv

时间:2013-07-26 01:52:25

标签: excel excel-vba csv vba

我想将数据从excel复制到。以下是我想要复制数据的方法。例如,我想将excel的单元格D4中的数据复制到中的A4

"Excel ---> CSV
"D4-->A4
"F4&all excluding the first character in G4-->B4
"K4-->E4
"L4-->F4
"N4-->G4
"P4-->I4

以下是我的代码。它会根据需要创建和填充数据,但在G4中复制到B4时,它不会删除csv中的第一个字符。有人可以告诉我如何更改我的代码来实现这一点。

Sub csvfile()

Dim fs As Object
Dim a As Object
Dim lngRow As Long
Dim X

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("C:\temp\" & Environ("username") & ".csv", True)
X = Range([d4], Cells(Rows.Count, "P").End(xlUp)).Value2

For lngRow = 1 To UBound(X) - 15
a.writeline X(lngRow, 1) & "," & X(lngRow, 3) & X(lngRow, 4) & ",,," & X(lngRow, 8) & "," & X(lngRow, 9) & "," & X(lngRow, 11) & ",," & X(lngRow, 13)
Next
a.Close

End Sub

1 个答案:

答案 0 :(得分:1)

您可以像这样操纵第四列(G):

完整代码

Sub csvfile()

Dim fs As Object
Dim a As Object
Dim lngRow As Long
Dim X

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("C:\temp\" & Environ("username") & ".csv", True)
X = Range([d4], Cells(Rows.Count, "P").End(xlUp)).Value2

For lngRow = 1 To UBound(X) - 15
If Len(X(lngRow, 4)) > 0 Then X(lngRow, 4) = Right$(X(lngRow, 4), Len(X(lngRow, 4)) - 1)
a.writeline X(lngRow, 1) & "," & X(lngRow, 3) & X(lngRow, 4) & ",,," & X(lngRow, 8) & "," & X(lngRow, 9) & "," & X(lngRow, 11) & ",," & X(lngRow, 13)
Next
a.Close

End Sub