我有一些vba代码应执行以下操作:
这是我复制范围的代码:
Sub paste_multiple(ByVal copy As Range, ByVal times As Long)
Dim last_row As Long
For i = 1 To times
Debug.Print (copy.Address)
last_row = WorksheetFunction.CountA(ThisWorkbook.Sheets("scratchpad").Range("A:A"))
copy.copy (ThisWorkbook.Sheets("scratchpad").Range("A" & last_row + 1))
Next i
End Sub
这是调用此函数的代码
Sub generate_SKU_list()
ThisWorkbook.Sheets("Planning View").Range("A:A").copy (ThisWorkbook.Sheets("scratchpad").Range("A1"))
Call ThisWorkbook.Sheets("scratchpad").Range("A:A").RemoveDuplicates(Columns:=1, Header:=xlYes)
Dim lr As Long
lr = WorksheetFunction.CountA(ThisWorkbook.Sheets("Planning View").Range("A:A")) - 1
Call paste_multiple(ThisWorkbook.Sheets("scratchpad").Range("A2:A" & lr), 2)
Call ThisWorkbook.Sheets("scratchpad").Range("A:A").Sort(Key1:=ThisWorkbook.Sheets("scratchpad").Range("A:A"), _
Order1:=xlAscending, Header:=xlYes)
End Sub
输出为每个唯一值创建4个副本,我不知道为什么。
谢谢您的任何建议。
答案 0 :(得分:1)
您的paste_multiple子项仅执行所需的2个副本,但是第二个副本从第一个副本开始复制整个副本范围,从而产生4个副本。
这是替代方法
Sub paste_multiple(ByVal rCopy As Range, ByVal times As Long)
Dim Dat As Variant
Dim i As Long
Dat = rCopy.Value
For i = 1 To times
rCopy.Offset(UBound(Dat, 1) * i, 0).Value = Dat
Next
End Sub