简体   繁体   English

Excel VBA 自动根据单元格值复制整行“X”次并粘贴到单独的工作表中

[英]Excel VBA automation copy entire row “X” times based on cell value and paste in separate sheet

Relatively new to VBA and the situation I've run into is I have a column A through Y that I need to copy and paste X number of times based on the numeric value in column O. I used the below code which worked fine for just copying into the separate sheet. VBA 相对较新,我遇到的情况是我有一个 A 列到 Y 列,我需要根据 O 列中的数值复制和粘贴 X 次。我使用了以下代码,该代码仅适用于复制到单独的工作表中。 The problem I'm running into now is I've changed so there is formulas in column A to make it a bit more dynamic;我现在遇到的问题是我已经改变了,所以 A 列中有公式,使其更具动态性; however, now the code is copying the formulas.但是,现在代码正在复制公式。

I did some more research on pastespecial but cannot seem to get my code to do the same as the first code below only pasting the value of the formula in column A instead.我对 pastespecial 进行了更多研究,但似乎无法让我的代码与下面的第一个代码一样,只是将公式的值粘贴到 A 列中。 I'm not tied to copying the entire row but I do need columns AY.我不依赖于复制整行,但我确实需要 AY 列。 Any assistance is much appreciated!非常感谢任何帮助!

Public Sub CopyData()
' This routing will copy rows based on the quantity to a new sheet.
Dim rngSinglecell As Range
Dim rngQuantityCells As Range
Dim intCount As Integer

' Set this for the range where the Quantity column exists. This works only if there are no empty cells
Set rngQuantityCells = Range("D1", Range("D1").End(xlDown))

For Each rngSinglecell In rngQuantityCells
    ' Check if this cell actually contains a number
    If IsNumeric(rngSinglecell.Value) Then
        ' Check if the number is greater than 0
        If rngSinglecell.Value > 0 Then
            ' Copy this row as many times as .value
            For intCount = 1 To rngSinglecell.Value
                ' Copy the row into the next emtpy row in sheet2
                Range(rngSinglecell.Address).EntireRow.Copy Destination:= Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)                                
                ' The above line finds the next empty row.

            Next
        End If
    End If
Next
End Sub

Also - I've lurked around this forum for some time and you all are amazing at what you do here and a great resource!另外 - 我已经潜伏在这个论坛一段时间了,你们都对自己在这里所做的事情和很棒的资源感到惊讶! Glad to have finally joined.很高兴终于加入。

Try the refactored code below, which will accomplish your goal and most likely run faster, too.试试下面的重构代码,这将实现你的目标,而且很可能运行得更快。

Public Sub CopyData()

' This routing will copy rows based on the quantity to a new sheet.
Dim rngSinglecell As Range
Dim rngQuantityCells As Range
Dim intCount As Integer

' Set this for the range where the Quantity column exists. This works only if there are no empty cells
Set rngQuantityCells = Range("D1", Range("D1").End(xlDown))

For Each rngSinglecell In rngQuantityCells

    ' Check if this cell actually contains a number and if the number is greater than 0
    If IsNumeric(rngSinglecell.Value) And rngSingleCell.Value > 0 Then

        ' Copy this row as many rows as .value and 25 columns (because A:Y is 25 columns)
        Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(rngSinglecell.Value, 25).Value = _
            Range(Range("A" & rngSinglecell.Row), Range("Y" & rngSinglecell.Row)).Value

    End If
Next

End Sub

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

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