1

我想将数据从 excel 复制到 csv 但似乎找不到正确的逻辑。以下是我想要复制数据的方式。例如,我想将 excel 的单元格 D4 中的数据复制到 csv 中的 A4 中。我想重复此操作,直到 D 列中的单元格为空。

"Excel ---> CSV
"D4-->A4
"F4&G4-->B4
"K4-->E4
"L4-->F4
"N4-->G4
"P4-->I4

很抱歉在我刚开始编写宏时问了这样一个基本问题。以下是我目前的代码。这会创建 csv 文件,但不会填充我需要的数据。

Sub csvfile()

Dim fs As Object, a As Object, i As Integer, s As String, t As String, l As String, mn As String
Set fs = CreateObject("Scripting.FileSystemObject")
sUser = Environ("username")
Set a = fs.CreateTextFile("S:\ics\jellybean\" & sUser & ".csv", True)

For r = 4 To Range("A65536").End(xlUp).Row
    s = ""
    c = 6
    While Not IsEmpty(Cells(r, c))
        s = s & Cells(r, c) & ","
        c = c + 1
    Wend
    a.writeline s 'write line
Next r

End Sub
4

1 回答 1

1

像这样的东西使用数组并且非常有效(它假设您的数据对于感兴趣的列具有从 D 到 P 的相同列长度)

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)
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-25T05:56:15.890 回答