2011-02-02 72 views
1

我有一個包含抽獎信息的Excel電子表格。將Excel電子表格中的行復制到新電子表格中L列中值的數量

每一行都有一個人購買門票的信息。

問題是我需要製作一張新的電子表格(最終,我需要將它合併到標籤中),每張票一行,但是如果一個人購買了2張票,他們的信息只在一行中原始電子表格,「L」列中的票數量。

所以我需要一個宏,它會查看他在L列中的值,並將該行復制到新的電子表格L次 - 如果他們購買了1張票據,並且L列中的值爲1,則它將複製它是1次,如果他們購買了3張票,並且L列中的值是3,則將其複製3次。

有人能告訴我怎麼去做這件事嗎?

如果在郵件合併期間有辦法做到這一點,應該可以工作2,我只是想先製作一個新的電子表格,然後再從新的工作表製作標籤。

謝謝!

回答

0

我最終在網站here上找到了一些代碼,並根據我的需要修改了它。 這就是我正在使用的:

Sub MakeTickets() 
Dim X As Long, Z As Long, Qty As Long, Rw As Long 
Dim StartRow As Long, LastRow As Long 
Dim Source As String, Destination As String 
'Define the variables below 
StartRow = 2 'the row to start from in the source sheet 
FirstDestination = 1 'the row to start from in the destination sheet 
FirstCell = "A" 'the first column in each row that you want to copy 
LastCell = "O" 'the last column in each row that you want to copy 
Source = "Sold" 'source sheet name 
Destination = "Tickets" ' destination sheet name 
QtyClmn = "L" 'column to get the quantity from 
'Until here 
Rw = FirstDestination 
With Worksheets(Source) 
LastRow = .Cells(.Rows.Count, FirstCell).End(xlUp).Row 
For X = StartRow To LastRow 
Qty = Cells(X, QtyClmn).Value 
For Z = 1 To Qty 
Rw = Rw + 1 
Worksheets(Destination).Range(FirstCell & Rw & ":" & LastCell & Rw).Value = .Range(FirstCell & X & ":" & LastCell & X).Value 
Next 
Next 
End With 
End Sub 
相關問題