简体   繁体   English

如何在Excel中的同一列中复制行N次?

[英]How to copy a row N number of times in the same column in Excel?

We are not able to create a formula which will copy 200 rows of a column in a same order and paste it multiple times in the same column and in the same order.我们无法创建一个公式,该公式将以相同的顺序复制一列的 200 行并以相同的顺序将其多次粘贴到同一列中。

Example: columns A1:A200 have names in a particular order and we want to repeat the same order in the same column for 3000 times.示例:列 A1:A200 具有特定顺序的名称,我们希望在同一列中重复相同的顺序 3000 次。

What is the way to do it without manual dragging?没有手动拖动的方法是什么?

Multi-Stack a Range Vertically垂直堆叠一个范围

Sub VMultiStackTEST()

    Const SourceRangeAddress As String = "A1:A200"
    Const DestinationFirstCellAddress As String = "A1"
    Const StackCount As Long = 3000

    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
    Dim srg As Range: Set srg = ws.Range(SourceRangeAddress)
    Dim dfCell As Range: Set dfCell = ws.Range(DestinationFirstCellAddress)
    
    VMultiStack srg, dfCell, StackCount
    
    ' or (instead) just e.g.:
    'VMultiStack Range("A1:A200"), Range("A1"), 3000

End Sub

Sub VMultiStack( _
        ByVal SourceRange As Range, _
        ByVal DestinationFirstCell As Range, _
        Optional ByVal StackCount As Long = 1)
    Const ProcName As String = "VMultiStack"
    On Error GoTo ClearError
 
    Dim IsSuccess As Boolean
 
    Dim sData As Variant
    Dim srCount As Long
    Dim cCount As Long
    Dim sAddress As String
    
    With SourceRange.Areas(1)
        sAddress = .Address(0, 0)
        srCount = .Rows.Count
        cCount = .Columns.Count
        If srCount + cCount = 2 Then
            ReDim sData(1 To 1, 1 To 1): sData(1, 1) = .Value
        Else
            sData = .Value
        End If
    End With
    
    Dim dData As Variant: ReDim dData(1 To srCount * StackCount, 1 To cCount)
    
    Dim n As Long
    Dim sr As Long
    Dim dr As Long
    Dim c As Long
    
    For n = 1 To StackCount
        For sr = 1 To srCount
            dr = dr + 1
            For c = 1 To cCount
                dData(dr, c) = sData(sr, c)
            Next c
        Next sr
    Next n
    
    Dim dAddress As String
    
    With DestinationFirstCell.Resize(, cCount)
        With .Resize(dr)
            .Value = dData
            dAddress = .Address(0, 0)
        End With
        .Resize(.Worksheet.Rows.Count - .Row - dr + 1).Offset(dr).Clear
    End With
    
    IsSuccess = True
    
ProcExit:
    If IsSuccess Then
        MsgBox "Stacked '" & sAddress & "' " & StackCount & " times to '" _
            & dAddress & "'.", _
            vbInformation, ProcName
    Else
        If Len(sAddress) > 0 Then
            MsgBox "Could not stack '" & sAddress & "' " & StackCount _
                & " times. No action taken.", _
                vbExclamation, ProcName
        Else
            MsgBox "The program failed.", vbCritical, ProcName
        End If
    End If

    Exit Sub
ClearError:
    Debug.Print "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume ProcExit
End Sub

With Office 365, you can put this into a LET as follows:使用 Office 365,您可以将其放入 LET,如下所示:

=LET( a, A1:A200, mBy, 3000,
       r, ROWS( a ),
       s, r * mBy,
       INDEX( a, MOD(SEQUENCE( s,,0 ),r) + 1 ) )

where a is the column of names and mBy is the multiple (3000).其中a是名称列, mBy是倍数 (3000)。

If you want to simplify it:如果你想简化它:

= INDEX( A1:A200, MOD(SEQUENCE( ROWS(A1:A200) * 3000,,0 ),ROWS(A1:A200)) + 1 )

暂无
暂无

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

相关问题 样本数组 n^2 次,没有相同的数字连续出现两次,并且不重复每 n - sample array n^2 times without the same number occurring twice in a row and not repeating every n 给定数字N,如何调用递归调用N次? - how to call a recursion call N number of times, given the number N? 获取加起来为数字“n”的数字列表(使用数字重复)——如何考虑多次使用相同数字的情况? - Get a list of numbers that adds up to a number "n" (using repetition of number) - how would one consider the case of using same number multiple times? 如何将数组的每个值复制一定次数(迭代行/列)? - How can I copy each value of an array for a certain number of times (Iteration over rows / column)? 如何将二维数组复制到第三维,N 次? - How to copy a 2D array into a 3rd dimension, N times? 如何在 O(n) 时间和 O(n) 空间中找到重复 (n/3) 次大小为 n 的数组的数字? - How to find a number that was repeated (n/3) times an array of size n, in O(n) time and O(n) space? 在数组中重复N次 - Repeat a Number N Times in an Array 在数组上过滤n次 - filter n number of times on an array 如何将相同的元素添加到javascript数组n次 - How to add same elements to javascript array n times VBA excel:如何在不选择的情况下将单元格中的数据作为数组获取到同一列中的一行? - VBA excel: How do I get data in cells as an array up one row in the same column without selecting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM