後這裏是一個快速和骯髒的方式做到這一點。這可能需要幾分鐘時間才能運行,但這是處理多行時需要的。
50,000x7 = 350,000行,因此如果您有任何最新版本的Excel,則可以將輸出放在另一個工作表上。我在2010年,行限額爲1,048,576。
這裏假設數據在Sheet1上,我們將它寫出到Sheet2。
在你的VBA IDE中,進入工具菜單並選擇參考。選擇「Microstoft的ActiveX數據對象2.8庫
Private Sub CommandButton1_Click()
Dim ws As Excel.Worksheet
Dim rs As New ADODB.Recordset
Dim lRow As Long
'Add fields to your recordset for storing data. This is how we will store the original data so we can process it after we read it.
With rs
.Fields.Append "Order", adInteger
.Fields.Append "Line", adInteger
.Fields.Append "Item", adChar, 25
.Fields.Append "Day", adChar, 10
.Fields.Append "Day2", adChar, 10
.Fields.Append "Day3", adChar, 10
.Fields.Append "Day4", adChar, 10
.Fields.Append "Day5", adChar, 10
.Fields.Append "Day6", adChar, 10
.Fields.Append "Day7", adChar, 10
.Open
End With
lRow = 2 'Start at two if there is a header row...
Set ws = ActiveWorkbook.Sheets("Sheet1")
ws.Activate
'Loop through the rows and record the data
Do While lRow <= ws.UsedRange.Rows.count
If ws.Range("A" & lRow).Value <> "" Then
rs.AddNew
rs.Fields("Order").Value = ws.Range("A" & lRow).Value
rs.Fields("Line").Value = ws.Range("B" & lRow).Value
rs.Fields("Item").Value = ws.Range("C" & lRow).Value
rs.Fields("Day").Value = ws.Range("D" & lRow).Value
rs.Fields("Day2").Value = ws.Range("E" & lRow).Value
rs.Fields("Day3").Value = ws.Range("F" & lRow).Value
rs.Fields("Day4").Value = ws.Range("G" & lRow).Value
rs.Fields("Day5").Value = ws.Range("H" & lRow).Value
rs.Fields("Day6").Value = ws.Range("I" & lRow).Value
rs.Fields("Day7").Value = ws.Range("J" & lRow).Value
rs.Update
End If
lRow = lRow + 1
ws.Range("A" & lRow).Activate
Loop
'Switch to the second worksheet
Set ws = Nothing
Set ws = ActiveWorkbook.Sheets("Sheet2")
ws.Activate
lRow = 1
If rs.RecordCount > 0 Then
rs.MoveFirst
End If
Do While rs.EOF = False
If Trim(rs.Fields("Day").Value) <> "" Then
ws.Range("A" & lRow).Value = rs.Fields("Order").Value
ws.Range("B" & lRow).Value = rs.Fields("Line").Value
ws.Range("C" & lRow).Value = rs.Fields("Item").Value
ws.Range("D" & lRow).Value = rs.Fields("Day").Value
lRow = lRow + 1
End If
If Trim(rs.Fields("Day2").Value) <> "" Then
ws.Range("A" & lRow).Value = rs.Fields("Order").Value
ws.Range("B" & lRow).Value = rs.Fields("Line").Value
ws.Range("C" & lRow).Value = rs.Fields("Item").Value
ws.Range("D" & lRow).Value = rs.Fields("Day2").Value
lRow = lRow + 1
End If
If Trim(rs.Fields("Day3").Value) <> "" Then
ws.Range("A" & lRow).Value = rs.Fields("Order").Value
ws.Range("B" & lRow).Value = rs.Fields("Line").Value
ws.Range("C" & lRow).Value = rs.Fields("Item").Value
ws.Range("D" & lRow).Value = rs.Fields("Day3").Value
lRow = lRow + 1
End If
If Trim(rs.Fields("Day4").Value) <> "" Then
ws.Range("A" & lRow).Value = rs.Fields("Order").Value
ws.Range("B" & lRow).Value = rs.Fields("Line").Value
ws.Range("C" & lRow).Value = rs.Fields("Item").Value
ws.Range("D" & lRow).Value = rs.Fields("Day4").Value
lRow = lRow + 1
End If
If Trim(rs.Fields("Day5").Value) <> "" Then
ws.Range("A" & lRow).Value = rs.Fields("Order").Value
ws.Range("B" & lRow).Value = rs.Fields("Line").Value
ws.Range("C" & lRow).Value = rs.Fields("Item").Value
ws.Range("D" & lRow).Value = rs.Fields("Day5").Value
lRow = lRow + 1
End If
If Trim(rs.Fields("Day6").Value) <> "" Then
ws.Range("A" & lRow).Value = rs.Fields("Order").Value
ws.Range("B" & lRow).Value = rs.Fields("Line").Value
ws.Range("C" & lRow).Value = rs.Fields("Item").Value
ws.Range("D" & lRow).Value = rs.Fields("Day6").Value
lRow = lRow + 1
End If
If Trim(rs.Fields("Day7").Value) <> "" Then
ws.Range("A" & lRow).Value = rs.Fields("Order").Value
ws.Range("B" & lRow).Value = rs.Fields("Line").Value
ws.Range("C" & lRow).Value = rs.Fields("Item").Value
ws.Range("D" & lRow).Value = rs.Fields("Day7").Value
lRow = lRow + 1
End If
ws.Range("A" & lRow).Activate
rs.MoveNext
Loop
End Sub
你可以用一個類來完成此創建一個用戶定義的對象,並添加日期作爲一個集合。如果列出超過七天,會發生什麼? –
嗨羅恩,謝謝你的答案,但我從來沒有使用過類,我只能寫簡單的查詢, m只是一個功能顧問:)只有7天,但數據存儲爲1-127,我必須將它轉換爲二進制,1是0000001,星期日是127,而127是每週的任何一天。他們是不同日子的組合。我正在將數據導入Netsuite,並且在當前的定製中,每天都必須成爲單獨的一行。感謝您的建議,順便說一句,我會諮詢谷歌,並試圖找出它:) – user3618691
建議你開始芯片皮爾遜的[課程簡介](http://www.cpearson.com/excel/Classes.aspx)。另外,根據您的具體要求,每張工作表可能只有10^6行以上的空間。 –