1

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

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

下面是我的代码。G4它根据我的需要创建和填充数据,只是在复制到B4in时不会删除 in 的第一个字符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
4

1 回答 1

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
于 2013-07-26T01:56:37.957 回答