2017-05-04 115 views
-1

我目前正试图通过查找找到我需要的数据,因为我不知道它位于何处,然后将数据向下复制到另一个表单上。我需要多次这样做。选择复制和粘贴错误

不过,我不断收到对象变量或带块变量未设置我的第二个发现错误,它的上线:

.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 

回答

0

数关于您的意见代码:

1.Dim ran, r As range表示ranVariant,只有rRange

2.没有必要有:

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

只使用完全组队参加Range没有任何Select ING:

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

确保您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 
+0

哦,我还以为然和R将是相同的!不确定如何使用范围对象,所以我绝对从网上摘取代码!谢谢你的帮助 ! –

+0

是的,它工作!我可以问,如何去使用查找不使用完全匹配 –

+0

@RachelChia将'LookAt:= xlWhole'改为'LookAt:= xlPart' –