VBA复制和粘贴将某些值粘贴到不正确的行中,而某些则正确粘贴

时间:2020-05-20 15:58:12

标签: excel vba copy-paste

我在VBA中复制和粘贴值时遇到问题,其中大多数行值都粘贴在正确的行中。但是,在某一列之后,它将行值向下粘贴6行。似乎在循环中的某处添加了偏移量,但我无法确定位置。我尝试了PasteSpecial函数的不同变体,但结果相同。下面是复制和粘贴的代码:

s1.Activate
    'Assumes max 10 shipments per distributor
    With s1
        Totrows = .Cells(.Rows.Count, "A").End(xlUp).Row - 1
    End With
    'Range(Cells(3, 1), Cells(3, 1).End(xlDown).Address).Rows.Count 2
    'Loop through rows to see if we want them
     For i = 4 To Totrows Step 3
         If Cells(i, 1) = 1 Then
         Range("A"&CStr(i),"CW"&CStr(i)).Copy 'Destination:=s2.Rows(j)
         'activates All Geo Zones worksheet

         s2.Activate
         Rows(j).Select
         'Range("A"&j).End(xlUp).PasteSpecial Paste:=xlPasteValues
         'Selection.Insert Shift:=xlDown
         Range("A"&Cstr(j),"CW"&CStr(j)).PasteSpecial Paste:=xlPasteValues
         'Selection.PasteSpecial Paste:=xlPasteValues
            'Create unique id
            If Cells(j, 2) = Cells(j - 1, 2) And Cells(j, 3) = Cells(j - 1, 3) And Cells(j, 5) = Cells(j - 1, 5) Then
                Cells(j, 1) = Cells(j, 1) + 1
            End If


         s1.Activate
         'Copy row and paste it to the other worksheet
         j = j + 1
         End If

     Next i

您可以看到我正在从s1工作表的A列到CW列中选择一个范围,然后激活s2并将值粘贴到第j行中相同的范围内(从3开始)。下面的If语句检查是否为重复订单并更改ID。我认为这对粘贴没有任何影响。 我已经附上了结果的屏幕截图。您可以看到我在第一行中概述了应该在其中列出的值,但现在在第7行中了。我不明白为什么大多数值都被正确粘贴了,但是将它们向下粘贴了几行。注释中,此图像的最后一列是CW。 enter image description here

0 个答案:

没有答案
相关问题