繁体   English   中英

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

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

基本上,我想要以下示例场景的简单方法:根据数字行复制 A、B 和 C 的次数。 当然,在实际情况下,行数比下面的例子多得多。

A  1
B  2
C  3
   6
   7

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

有什么快捷的方法吗?

去跟随,

下面的函数和 VBA 解决了我上面的问题。

使用下面的函数来获取第一列

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

任何人都知道这部分的相应 VBA 代码? 该功能有效,但是当线路太多时不是很方便。 在 VBA 中设置范围会很棒。

第二列使用 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

下面的函数和 VBA 解决了我上面的问题。

使用下面的函数来获取第一列

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

任何人都知道这部分的相应 VBA 代码? 该功能有效,但是当线路太多时不是很方便。 在 VBA 中设置范围会很棒。

第二列使用 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