简体   繁体   English

如何根据Excel VBA中另一列的数量在Excel中插入/复制

[英]How to insert/copy in Excel base on number of another column in Excel VBA or not

Basically, I would like a simple way to the following example scenario : Number of times of A,B and C copied based on lines of numbers.基本上,我想要以下示例场景的简单方法:根据数字行复制 A、B 和 C 的次数。 Number of lines are much more than below example in real cases of course.当然,在实际情况下,行数比下面的例子多得多。

From

A  1
B  2
C  3
   6
   7

to

A  1
A  2
A  3
A  6
A  7
B  1
B  2
B  3
B  6
B  7
C  1
C  2
C  3
C  6
C  7

Any quick way?有什么快捷的方法吗?

To follow up,去跟随,

Below function and VBA solved my question above.下面的函数和 VBA 解决了我上面的问题。

Used below function to get the first column使用下面的函数来获取第一列

=INDEX(Sheet1!A:A,ROUNDUP(ROW()/5,0))

Anyone know a corresponding VBA code for this part?任何人都知道这部分的相应 VBA 代码? The function works, however, it is not very convenient when there are too many lines.该功能有效,但是当线路太多时不是很方便。 It will be great to set range in VBA.在 VBA 中设置范围会很棒。

The second column used VBA code as below (Tried multiple columns so be VBA is slightly different with the question) :第二列使用 VBA 代码如下(尝试了多个列,因此 VBA 与问题略有不同):

Sub Macro1()
    '
    ' Macro1 Macro
    '
    Dim lastRow As Long

    ' Subdivision total row number
    a = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    ' Porject total row number
    b = Worksheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row
    Sheets("Sheet1").Select
    ' Project total cell range
    Range("B1:C5").Copy
    Range("F1").Select
    Sheets("Sheet1").Paste
    ' Row number need to -1 as the first copy is done above
    For i = 1 To A - 1
        lastRow = Sheets("Sheet1").Cells(Rows.Count, "F").End(xlUp).Row + 1
        Range("F" & lastRow).Select
        ActiveSheet.Paste
    Next
End Sub

Below function and VBA solved my question above.下面的函数和 VBA 解决了我上面的问题。

Used below function to get the first column使用下面的函数来获取第一列

=INDEX(Sheet1!A:A,ROUNDUP(ROW()/5,0)) =INDEX(Sheet1!A:A,ROUNDUP(ROW()/5,0))

Anyone know a corresponding VBA code for this part?任何人都知道这部分的相应 VBA 代码? The function works, however, it is not very convenient when there are too many lines.该功能有效,但是当线路太多时不是很方便。 IT will be great to set range in VBA.在 VBA 中设置范围会很棒。

The second column used VBA code as below (Tried multiple columns so be VBA is slightly different with the question) :第二列使用 VBA 代码如下(尝试了多个列,因此 VBA 与问题略有不同):

Sub Macro1()
'
' Macro1 Macro
'
 Dim lastRow As Long

 ' Subdivision total row number
    a = Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
  ' Porject total row number
    b = Worksheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row
        Sheets("Sheet1").Select
    ' Project total cell range
    Range("B1:C5").Copy
    Range("F1").Select
    Sheets("Sheet1").Paste
    ' Row number need to -1 as the first copy is done above
  For i = 1 To A - 1
    lastRow = Sheets("Sheet1").Cells(Rows.Count, "F").End(xlUp).Row + 1
    Range("F" & lastRow).Select
    ActiveSheet.Paste
  Next
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM