【问题标题】:Excel VBA automation copy entire row "X" times based on cell value and paste in separate sheetExcel VBA自动根据单元格值复制整行“X”次并粘贴到单独的工作表中
【发布时间】:2020-02-14 08:53:46
【问题描述】:

VBA 相对较新,我遇到的情况是我有一个 A 到 Y 列,我需要根据 O 列中的数值复制和粘贴 X 次。我使用了下面的代码只需复制到单独的工作表中即可。我现在遇到的问题是我已经改变了,所以 A 列中有公式,使它更有活力;但是,现在代码正在复制公式。

我对 pastespecial 进行了更多研究,但似乎无法让我的代码与下面的第一个代码相同,只是将公式的值粘贴到 A 列中。我并不局限于复制整行,但我确实需要 A-Y 列。非常感谢任何帮助!

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

另外 - 我已经在这个论坛上潜伏了一段时间,你们都对自己在这里所做的事情和很棒的资源感到惊讶!很高兴终于加入了。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    试试下面的重构代码,它会实现你的目标,而且很可能运行得更快。

    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
    

    【讨论】:

    • 下面的行给了我一个类型不匹配的错误: Sheets("HDHelp1").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(rngSinglecell .Value, 25).Value = _ Range(Range("A" & rngSinglecell.Row), Range("Y" And rngSinglecell.Row)).Value
    • @DChantal - 抱歉最后一个And 应该是&。我编辑了答案。
    • 这非常有效,谢谢!我似乎无法编辑“rngSingelCell”以修复其中的“Single”以供人们将来使用(说 6 个字符以下)。
    • @DChantal - 已修复 :)
    猜你喜欢
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多