复制行中的范围,其中要复制的 x 的 # 在单元格中

Copying a range in a row where the # of x to be copied is in cell

我需要将 69 行中的每一行复制 n 次。应复制的行中每个范围的 n 次也在该行中。我附上了一个屏幕截图,这样你就可以看到数据。我在这里尝试了其他答案之一,但它对我不起作用。

因此,在上面的屏幕截图中,我希望将 B2,D2:G2 复制 284 次。在我输入此内容时,我发现如果我切换 C 列和 B 列,效果会更好。

无论如何 - 我看到了一些 VBA 的例子。我不熟悉它,但我对一般的编码并不陌生。所以,如果解决方案是 VBA,那么我愿意,我只需要虚拟级别的说明 ;)

在Excel中,您可以复制一些1行多列的数据(例如B5:Z5),然后将该数据粘贴到1列多行宽的范围内(例如 D10:D50) 并且数据将在每一行中重复。这就是下面的代码正在做的事情:

Sub MultiCopy()
    Dim sourceRange As Range
    Dim targetBook As Workbook
    Dim targetRange As Range
    Dim cell As Range
    Dim count As Integer
    Dim copyRange As Range

    'Adjust this range to reflect the list containing the numbers
    Set sourceRange = ActiveSheet.Range("B2:B6")

    'We'll copy the data to a new workbook
    Set targetBook = Workbooks.Add
    Set targetRange = targetBook.Worksheets(1).Range("A1")

    'Loop through the cells which contain the number of times to repeat the row
    For Each cell In sourceRange
        'Get the number of times that the current row will be repeated
        count = cell.Value
        'The range being copied starts in the cell to the right of the value, and is 5 cells wide
        Set copyRange = cell.Offset(0, 1).Resize(1, 5)
        'The data will be pasted in to a vertical block of cells
        'The copied data gets repeated in each row
        Set targetRange = targetRange.Resize(count, 1)
        copyRange.Copy targetRange
        'Reset the targetrange ready for the next row
        Set targetRange = targetRange.Offset(count, 0)
    Next
End Sub

我没有 Mac,但下面的代码在 Excel 2013 上运行良好,而且我不知道有什么原因它不能在 Mac。