复制要复制的x的#在一行中的范围

时间:2015-07-29 17:08:14

标签: excel-vba excel-2011 vba excel

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

所以,在上面的屏幕截图中,我想要将B2,D2:G2复制284次。当我输入这个时,我可以看到,如果我切换C列和B列,这将更好用。

无论如何 - 我见过VBA的一些例子。我对它不熟悉,但我并不熟悉编码。所以,如果解决方案是VBA,那么我就是为了它,我只需要一个虚拟级别的指令;)

1 个答案:

答案 0 :(得分:1)

在Excel中,您可以复制一行多列的数据(例如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上不起作用的任何原因。