2017-05-26 137 views
0

致任何人誰可以幫助...在此先感謝。Excel如何根據單元格值重複多個值X次

的項數,包中的每一行,並且尺寸是需要根據

請注意在列「標籤數」的數量被重複多次在一個單獨的片材:在數標籤的數量是僅用於測試目的,不需要遞增

表1將如下所示

Item # Pack Size Number of Labels 
12545 20  1.8oz 1 
56010 6  4PK  2 
70091 6  7oz  3 
61816 24  1.6oz 4 

我想表2輸出類似如下:

Item # Pack Size 
12545 20  1.8oz 
56010 6  4PK 
56010 6  4PK 
70091 6  7oz 
70091 6  7oz 
70091 6  7oz 
61816 24  1.6oz 
61816 24  1.6oz 
61816 24  1.6oz 
61816 24  1.6oz 

我發現下面的代碼,但我想要的細胞輸入範圍是固定的,並且不使用對話框boxesI需要幫助修改我的代碼發現爲了與我的問題一起工作。我需要下面的代碼來輸出多個colomns。 : (我來到這裏的代碼:https://www.extendoffice.com/documents/excel/1897-excel-repeat-cell-value-x-times.html#a2

Sub CopyData() 
'Update 20140724 
Dim Rng As Range 
Dim InputRng As Range, OutRng As Range 
xTitleId = "KutoolsforExcel" 
Set InputRng = Application.Selection 
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8) 
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8) 
Set OutRng = OutRng.Range("A1") 
For Each Rng In InputRng.Rows 
    xValue = Rng.Range("A1").Value 
    xNum = Rng.Range("B1").Value 
    OutRng.Resize(xNum, 1).Value = xValue 
    Set OutRng = OutRng.Offset(xNum, 0) 
Next 
End Sub 

的黑客我試圖做是行不通的任何幫助將是巨大的。

背景:我必須在我的新產品工作中創建許多標籤。我必須在Word中手動輸入每個標籤。我發現我可以使用Words Mail合併操作來導入Excel數據。我有這些部分工作,但現在我需要能夠得到每個項目所需的標籤的確切數量。

回答

0
Private Sub hereyago() 

    Dim arr As Variant 
    Dim wsO As Worksheet 
    Dim this As Integer 

    arr = ThisWorkbook.Sheets("Sheet1").UsedRange 
    Set wsO = ThisWorkbook.Sheets("Sheet2") 

    For i = LBound(arr, 1) To UBound(arr, 1) 
     If IsNumeric(arr(i, 4)) Then 
      this = arr(i, 4) 
      For h = 1 To this 
       wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(1, 0).Value = arr(i, 1) 
       wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(0, 1).Value = arr(i, 2) 
       wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(0, 2).Value = arr(i, 3) 
       wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(0, 3).Value = arr(i, 4) 
      Next h 
     End If 
    Next i 
End Sub 
相關問題