我想复制行的特定单元格的值并将其粘贴到下一行的另一个单元格。这是我到目前为止所拥有的。
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行没有出错?
答案 0 :(得分:1)
您可以组合这些线条。试试下面的代码。避免在代码中使用select。 Why?
'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
答案 1 :(得分: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
答案 2 :(得分: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