1

我想复制一行特定单元格的值并将其粘贴到下一行的另一个单元格中。这是我到目前为止所拥有的。

for i= 2 to 26160
    If (Cells(i, 3) >= 99) Then

        Cells(i, 3).Select
        Selection.copy
        Cells(i, 4).Select 'error
        Selection.Paste    'error    
    end if    
next i

但是我的代码无法在第 4 行和第 5 行出现错误?

4

3 回答 3

1

您可以组合线条。试试下面的代码。避免在代码中使用 select。为什么?

   'at beginning of proc
With Excel.Application
    .ScreenUpdating = False
    .Calculation = Excel.xlCalculationManual
    .EnableEvents = False
End With

'''
'your code

' updated as per comment
j = 1
For i = 2 To 26160
    If (Sheets("sheet2").Cells(i, 3) >= 99) Then
        Sheets("sheet2").Cells(i, 3).Copy Sheets("sheet3").Cells(j, 4)
        j = j + 1
    End If
Next i

'at end of proc
With Excel.Application
    .ScreenUpdating = True
    .Calculation = Excel.xlAutomatic
    .EnableEvents = True
End With
于 2013-04-19T05:45:10.117 回答
0

为什么不使用直接复制并通过以下方法避免剪贴板?

set NewSheet = Sheets("Sheet1") 'New sheet name
j = 1 'start pasting in this row on new sheet
for i= 2 to 26160   
    If ActiveSheet.Cells(i, 3) >= 99 Then 
        NewSheet.Cells(j, 4) = ActiveSheet.Cells(i, 3)
        j = j + 1
    end if 
next i
于 2013-04-19T07:09:07.693 回答
0

这应该是简单而快速的:

Sub test()
    Dim c As Range
    Debug.Print Timer,
    For Each c In Range("C2:C26160")
        If c.Value >= 99 Then
            'copy to next cell
            c.Copy c.Offset(0, 1)
            'or copy to next col in other sheet
            c.Copy Sheet3.Range(c.Offset(0, 1).Address)
        End If
    Next c
    Debug.Print Timer

End Sub
于 2013-04-19T07:35:36.097 回答