选择复制和粘贴错误

时间:2017-05-04 06:42:08

标签: excel vba excel-vba

我目前正在尝试查找我需要的数据,因为我不知道它位于何处,然后将数据沿着列复制到另一张纸上。我需要多次这样做。

但是,我一直收到对象变量的错误或者在我的第二次查找时没有设置块变量,它就行了:

.range(Selection, Selection.End(xlDown)).Select

我的代码

Dim ran, r As range
With Sheet2
    Set ran = .Cells.find(What:="User Defined Label 4", LookIn:=xlValues, LookAt:=xlWhole)
    ran.Select
   .range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheet9.range("A1").PasteSpecial

    Set r = .Cells.find(What:="V Align", LookIn:=xlValues, LookAt:=xlWhole)
    r.Select
    .range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheet9.range("B1").PasteSpecial
End With

1 个答案:

答案 0 :(得分:0)

关于您的代码的一些评论:

1。 Dim ran, r As range表示ranVariantr只有Range

2. 无需:

ran.Select
.range(Selection, Selection.End(xlDown)).Select
Selection.Copy

只使用完全合格的Range而不使用任何Select

.Range(Ran, Ran.End(xlDown)).Copy

3。确保您的Find能够找到您要查找的字符串/值,添加If Not Ran Is Nothing Then

尝试以下代码:

Option Explicit

Sub UseFind()

Dim Ran As Range, r As Range

With Sheet2
    Set Ran = .Cells.Find(What:="User Defined Label 4", LookIn:=xlValues, LookAt:=xlWhole)
    If Not Ran Is Nothing Then ' <-- make sure Find was successful
        .Range(Ran, Ran.End(xlDown)).Copy
        Sheet9.Range("A1").PasteSpecial
    End If

    ' add your second "Find" here

End With    

End Sub