VBA Copy&粘贴循环(生成字段编号)

时间:2017-08-05 09:42:57

标签: excel vba excel-vba

现在我正在努力根据excel中的数量生成标签。我设法得到它副本&根据单元格的值粘贴。但是,我不知道如何根据循环改变细胞。

以下是例子:

目前的结果:

| A                                 | B     | C                     | D     | E     |
|-------------------------------    |-----  |--------------------   |-----  |-----  |
| NMB IN DIA                        |       | MADE IN THAILAND      |       |       |
| INVOICE NO                        | :     | MM035639              |       |       |
| C/NO                              | :     | 1                     | /     | 2     |
| SHIP TO                           | :     | A                     |       |       |
| QTY                               | :     | 100                   |       |       |
| NMB PARTS NO                      | :     | SFASDF234             |       |       |
|                                   |       | *SFASDF234*           |       |       |
| CUST PARTS NO                     | :     | SFASDF234             |       |       |
| CUST ORDER NO                     | :     |                       |       |       |
| -----------------------------     | ---   | ------------------    | ---   | ---   |
| NMB IN DIA                        |       | MADE IN THAILAND      |       |       |
| INVOICE NO                        |  :    | MM035639              |       |       |
| C/NO                              |  :    | 1                     |  /    | 2     |
| SHIP TO                           |  :    | A                     |       |       |
| QTY                               |  :    | 100                   |       |       |
| NMB PARTS NO                      |  :    | SFASDF234             |       |       |
|                                   |       | *SFASDF234*           |       |       |
| CUST PARTS NO                     |  :    |                       |       |       |
| CUST ORDES NO                     |  :    |                       |       |       |

预期结果:

| A                                 | B     | C                     | D     | E     |
|-------------------------------    |-----  |--------------------   |-----  |-----  |
| NMB IN DIA                        |       | MADE IN THAILAND      |       |       |
| INVOICE NO                        | :     | MM035639              |       |       |
| C/NO                              | :     | 1                     | /     | 2     |
| SHIP TO                           | :     | A                     |       |       |
| QTY                               | :     | 100                   |       |       |
| NMB PARTS NO                      | :     | SFASDF234             |       |       |
|                                   |       | *SFASDF234*           |       |       |
| CUST PARTS NO                     | :     | SFASDF234             |       |       |
| CUST ORDER NO                     | :     |                       |       |       |
| -----------------------------     | ---   | ------------------    | ---   | ---   |
| NMB IN DIA                        |       | MADE IN THAILAND      |       |       |
| INVOICE NO                        |  :    | MM035639              |       |       |
| C/NO                              |  :    | 2                     |  /    | 2     |
| SHIP TO                           |  :    | A                     |       |       |
| QTY                               |  :    | 100                   |       |       |
| NMB PARTS NO                      |  :    | SFASDF234             |       |       |
|                                   |       | *SFASDF234*           |       |       |
| CUST PARTS NO                     |  :    |                       |       |       |
| CUST ORDES NO                     |  :    |                       |       |       |

正如您在预期结果中看到的那样,C / No是基于数量的循环。不只是复制粘贴。我有什么可以补充的吗?

以下是我目前的代码:

Private Sub CommandButton1_Click()

Dim i As Long

For i = 2 To Worksheets("Sheet3").Range("E3").Value

    Range("A1:A9", Range("E9")).Copy Sheet3.Range("A65536").End(xlUp)(2)

Next i

End Sub

1 个答案:

答案 0 :(得分:1)

只需将相关单元格的值设置为i

Private Sub CommandButton1_Click()
    Dim i As Long
    Dim NewLoc As Range

    For i = 2 To Worksheets("Sheet3").Range("E3").Value
        'Decide where to copy the output to
        Set NewLoc = Sheet3.Cells(Sheet3.Rows.Count, "A").End(xlUp).OffSet(1, 0)
        'Copy the range
        Range("A1:E9").Copy NewLoc
        'Change the value of the cell 2 rows down and 2 rows to the right
        NewLoc.Offset(2, 2).Value = i
    Next i

End Sub