Vba 对象变量或未设置变量

Vba 对象变量或未设置变量,vba,excel,Vba,Excel,学习一些VBA。到目前为止,我已经构建了这段代码,它应该允许我(尽管它还没有)执行以下操作: 获取“M”&i单元格中的数字(在第一次迭代中是M5) 在A列中查找该数字 找到后,将PutHereIfFound的值设置为与F6的值相同(因此为偏移量) 如果找到一个数字,则增加i,以便循环继续搜索M6、M7、。。。全部到M20单元 它返回一个运行时错误91,表示对象变量或未设置变量。调试时,它指向Set PuthereIfFound行 这个错误的原因是什么 Sub FindThis() Di

学习一些VBA。到目前为止,我已经构建了这段代码,它应该允许我(尽管它还没有)执行以下操作:

  • 获取
    “M”&i
    单元格中的数字(在第一次迭代中是M5)
  • 在A列中查找该数字
  • 找到后,将
    PutHereIfFound
    的值设置为与F6的值相同(因此为偏移量)
  • 如果找到一个数字,则增加i,以便循环继续搜索M6、M7、。。。全部到M20单元
  • 它返回一个
    运行时错误91
    ,表示
    对象变量或未设置变量。调试时,它指向
    Set PuthereIfFound

    这个错误的原因是什么

     Sub FindThis()
        Dim FindThis As Range
        Dim PutHereIfFound As Range
        Dim i As Integer
        Dim f As Integer
    
        i = 5
        f = 5
        Do
            Set FindThis = ActiveSheet.Range("M" & i)
            Set PutHereIfFound = ActiveSheet.Range("N" & i)
                With ActiveSheet.Range("A:A")
                    Set PutHereIfFound = .Find(What:=FindThis, _
                                    After:=.Cells(.Cells.Count), _
                                    LookIn:=xlValues, _
                                    LookAt:=xlWhole, _
                                    SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext, _
                                    MatchCase:=False).Offset(0, 5)
    
                    If Not PutHereIfFound Is Nothing Then
                        i = i + 1
                    Else
                        i = i                       
                    End If                                    
                End With
         Loop While i <= 20
    End Sub
    
    子查找此()
    这是一个模糊的范围
    如范围所示
    作为整数的Dim i
    作为整数的Dim f
    i=5
    f=5
    做
    Set FindThis=ActiveSheet.Range(“M”和i)
    设置PutHereIfFound=ActiveSheet.Range(“N”和i)
    使用ActiveSheet.Range(“A:A”)
    设置PutHereIfFound=.Find(What:=Find此_
    之后:=.Cells(.Cells.Count)_
    LookIn:=xlValues_
    看:=xlother_
    搜索顺序:=xlByRows_
    SearchDirection:=xlNext_
    匹配案例:=False)。偏移量(0,5)
    如果没有发现,那就什么都不是了
    i=i+1
    其他的
    i=i
    如果结束
    以
    
    循环,而我进一步我的评论,您的代码可以这样优化

     Sub FindThis()
        Dim ws As Worksheet
        Dim FindThis As String
        Dim aCell As Range
        Dim i As Long
    
        Set ws = Sheets("Sheet1")
    
        With ws
            For i = 5 To 20
                FindThis = .Range("M" & i).Value
    
                Set aCell = .Columns(1).Find(What:=FindThis, LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
    
                If Not aCell Is Nothing Then
                    '~~> Do whatever you want here with the F Value
                    PutHereIfFound = aCell.Offset(, 5).Value
    
                    Debug.Print PutHereIfFound
                End If
            Next i
        End With
    End Sub
    

    在回答您关于
    对象变量或未设置变量的问题时
    错误,这意味着
    查找未找到此
    ,而
    查找
    返回

    也许有更好的办法。一旦你从
    F
    中获得了值,你想做什么?好吧,把它们放到PutHereIfFound中,这是循环的第一次迭代中的N5。好吧,如果我知道你想在找到匹配项时用F列值填充N列?如果是,那么你也可以用
    Vlookup
    公式来实现:)