2017-05-04 119 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' –