我收到一個有n列數量的excel文件,我必須製作一個宏來只複製A到C列,而行數是未知的。此外,我必須將數據以連續形式導出到ascii文件,而不是表格。我發現下面的代碼在http://www.mcgimpsey.com/excel/textfiles.html:如何使用宏以非表格形式將excel複製到ascii文件?
Public Sub CharacterSV()
Const DELIMITER As String = "|"
Dim myRecord As Range
Dim myField As Range
Dim nFileNum As Long
Dim sOut As String
nFileNum = FreeFile
Open "Test.txt" For Output As #nFileNum
For Each myRecord In Range("A1:A" & _
Range("A" & Rows.Count).End(xlUp).Row)
With myRecord
For Each myField In Range(.Cells, _
Cells(.Row, Columns.Count).End(xlToLeft))
sOut = sOut & DELIMITER & myField.Text
Next myField
Print #nFileNum, Mid(sOut, 2)
sOut = Empty
End With
Next myRecord
Close #nFileNum
End Sub
然而,這是給我的輸出以表格形式。我的經驗是在C#中,我在excel中很少有經驗,在VBA和宏中是全新的,所以如果你能對此有所幫助,我將非常感激。
編輯:
輸入包括A列名B列姓氏和C列的ID號,看起來像這樣:
John Smith 12345656
James Doe 94848484
Jill Miles 78475899
Frank Young 48758599
輸出應該是這個樣子:
John Smith 12345656 James Doe 94848484 Jill Miles 78475899 Frank Young 48758599
你是什麼意思,「連續的形式,而不是表格」?請給我們一個例子。 –
向我們展示輸入的外觀以及相應輸出的外觀。 –
我編輯了包括輸入和輸出示例的問題。 –