2016-12-15 69 views
0

当我尝试运行此循环时,我在Cell.Value.Copy行中得到一个Error:Object Required。我需要做什么来解决这个错误?通过Loop-VBA复制单元格

Sub Findings() 
Application.ScreenUpdating = False 

Dim Cell As Object 
Dim Rng As Range 

Set Rng = Sheets("Sheet1").Range("C5:C74") 
For Each Cell In Rng 
    If (Cell.Value <> "") Then 
    Cell.Value.Copy 
    End If 
Next Cell 

If IsEmpty(Range("C85").Value) = True Then 
    Range("C85").PasteSpecial xlPasteValues 
ElseIf IsEmpty(Range("C86").Value) = True Then 
    Range("C86").PasteSpecial xlPasteValues 
ElseIf IsEmpty(Range("C87").Value) = True Then 
    Range("C87").PasteSpecial xlPasteValues 
End If 

Application.ScreenUpdating = True 
End Sub 
+1

只需使用'Cell.Copy'。 – Phylogenesis

+0

因为随后的循环通过循环覆盖剪贴板中的值,所以开始“For Each Cell In Rng”的循环有点没有意义。在VBA中,使用复制/粘贴来传输值几乎没有什么好的理由 - 直接分配给目标范围的'.Value'。 –

+0

@ gg315你是否在下面的答案中试过了代码? –

回答

0

如果你只想单元格复制其值(不包括将要返回空值公式),你可以使用SpecialCells(xlCellTypeConstants)设定的范围内。

见下面我的代码:

Sub Findings() 

Application.ScreenUpdating = False 

Dim Rng As Range 

With Sheets("Sheet1") 
    ' set "Rng" only to cells inside the range with values inside them 
    Set Rng = .Range("C5:C74").SpecialCells(xlCellTypeConstants) 
    Rng.Copy 

    If IsEmpty(.Range("C85").Value) Then 
     .Range("C85").PasteSpecial xlPasteValues 
    ElseIf IsEmpty(.Range("C86").Value) Then 
     .Range("C86").PasteSpecial xlPasteValues 
    ElseIf IsEmpty(.Range("C87").Value) Then 
     .Range("C87").PasteSpecial xlPasteValues 
    End If 

End With  
Application.ScreenUpdating = True 

End Sub 
相关问题