Vba 要复制和粘贴的选择错误

Vba 要复制和粘贴的选择错误,vba,excel,Vba,Excel,我目前正试图通过find查找所需的数据,因为我不知道它位于何处,然后将数据沿列复制到另一张表上。我需要做很多次 但是,我在第二次查找时不断得到对象变量或块变量未设置的错误,它位于以下行: .range(Selection, Selection.End(xlDown)).Select 我的代码 Dim ran, r As range With Sheet2 Set ran = .Cells.find(What:="User Defined Label 4", LookIn:=xlValu

我目前正试图通过find查找所需的数据,因为我不知道它位于何处,然后将数据沿列复制到另一张表上。我需要做很多次

但是,我在第二次查找时不断得到对象变量或块变量未设置的错误,它位于以下行:

.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.
Dim ran,r As ran
表示
ran
变量
,只有
r
范围

2.无需:

ran.Select
.range(Selection, Selection.End(xlDown)).Select
Selection.Copy
只需使用完全限定的
范围
而不使用任何
选择
ing:

.Range(Ran, Ran.End(xlDown)).Copy
3.确保您的
Find
能够找到您要查找的字符串/值,添加
如果未运行即为空,则添加

请尝试以下代码:

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
选项显式
Sub-UseFind()
Dim Ran作为范围,r作为范围
附页2
Set Ran=.Cells.Find(内容:=“用户定义标签4”,LookIn:=xlValues,LookAt:=xlWhole)

如果不是Ran什么都不是,那么‘哦,我以为Ran和r是一样的!我不知道如何使用范围对象,所以我只能从网上提取代码!谢谢你的帮助!是的,它正在工作!请问如何使用find来避免使用exactmatch@RachelChia将
LookAt:=xlother
更改为
LookAt:=xlPart
谢谢您的帮助。你是最棒的哈哈