excel 如何将数据复制到数组中/对其进行操作/粘贴到新工作表

pdsfdshx  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(377)

设置

我有一个Excel文件,其中A到J列中包含源数据。
在K列中有一个“发送类型”值,它可以是“多个”或“单个”。
L列中有一个“发送计数”值,该值为数字(“N”)。

目标

1.复制源数据
1.插入N-1行
1.将该数据N-1次粘贴到这些行中。
我希望结果是粘贴N次的数据。

  • 如果N为1,则不需要复制/插入/粘贴
  • 如果N大于1,则结果应为显示N次的数据。

Excel工作表示例

当前VBA

Sub Copy_PROD_Paste_Send_Count()

    Dim Copy_Row        As Integer
    Dim Send_Count      As Variant
    Dim TargetMapCount  As Integer
    Dim ProgressCount   As Integer
    Dim Send_Type       As String
    Dim ProgressTarget  As Integer
   
    Copy_Row = 1
    TargetMapCount = Application.WorksheetFunction.SumIf(Range("K:K"), "Many", Range("L:L"))
    Send_Type = Cells(Copy_Row, "K")
    ProgressTarget = Application.WorksheetFunction.Count(Range("A:A")) + Application.WorksheetFunction.SumIf(Range("K:K"), "Many", Range("L:L")) - Application.WorksheetFunction.CountIf(Range("K:K"), "Many")
    
    Application.ScreenUpdating = False
        
    Do While (Cells(Copy_Row, "A") <> "")
        Send_Count = Cells(Copy_Row, "L")
        Send_Type = Cells(Copy_Row, "K")

        If (Send_Type = "Many" And (Send_Count > 1) And IsNumeric(Send_Count)) Then
            
            Range(Cells(Copy_Row, "A"), Cells(Copy_Row, "L")).Copy
            Range(Cells(Copy_Row + 1, "A"), Cells(Copy_Row + Send_Count - 1, "L")).Select
            Selection.Insert Shift:=xlDown
            Copy_Row = Copy_Row + Send_Count - 1
                    
            ProgressCount = Range("A" & Rows.Count).End(xlUp).Row
                         
            Application.StatusBar = "Updating :" & ProgressCount - 1 & " of " & ProgressTarget & ": " & Format((ProgressCount - 1) / ProgressTarget, "0%")
                
        End If
        Copy_Row = Copy_Row + 1
          
    Loop        

End Sub

问题说明

宏在崩溃前最多执行大约2- 3 K行。我需要运行最多15 K。
我知道我应该试着把数据复制到一个数组中,在数组中操作它,然后把结果粘贴回一个新的工作表中,但我不知道该怎么做。

2w2cym1i

2w2cym1i1#

下面是一个例程,它将输入表复制到一个数组,然后从输入构建一个输出数组(在内存中),然后将输出数组转储回工作表。
注:它忽略“单次/多次”列,而仅使用最后一列中的值来确定重复输出的次数。

Sub RepeatData()

    'Declarations
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim m As Long, n As Long, o As Long, r As Long, c As Long
    
    With ActiveSheet
    
        'Find Last Row of table
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

        'Find Last Column of table
        LastColumn = .Cells(2, .Columns.Count).End(xlToLeft).Column
        
        'Copy table to array
        Dim ArrInput
        ArrInput = ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(LastRow, LastColumn)).Value
        
        'sum up multiples (total of last column in table) so we know how big the output array needs to be
        OutputRows = 1 + Application.WorksheetFunction.Sum(Cells(2, LastColumn).Resize(UBound(ArrInput) - 1, 1))
        
        'Create output array for filling
        ReDim ArrOutput(1 To OutputRows, 1 To LastColumn)
        
        'Copy data across
        o = 1   ' start outputting to index 1 of ArrOutput
        
        For r = 1 To LastRow
            
            If r = 1 Then                   ' if on header row
                m = 1                       ' set repeat to once (set m to 1)
            Else                            ' if not on header row
                m = ArrInput(r, LastColumn) ' set repeat to value in last column
            End If
            
            For n = 1 To m                  ' loop to repeat
                For c = 1 To LastColumn     ' cycle across columns
                    ArrOutput(o, c) = ArrInput(r, c)    'copy value
                Next
                o = o + 1                   ' increment output index
            Next
        Next

    End With
    
    'Write output array to sheet, 5 rows below the end of the input table
    ActiveSheet.Cells(LastRow + 5, 1).Resize(UBound(ArrOutput), UBound(ArrOutput, 2)).Value = ArrOutput

End Sub

相关问题