为什么我会得到excel VBA';s";“所需对象”;提供对象时出错?

为什么我会得到excel VBA';s";“所需对象”;提供对象时出错?,vba,object,excel,required,Vba,Object,Excel,Required,我的主宏调用4个子宏,然后执行一行代码,然后生成“需要对象”错误。我不明白为什么,因为我正在提供一个对象(至少我认为我是) 我的代码如下所示: Sub main_macro() Call Mac1 Call Mac2 Call Mac3 Call Mac4 Range("B" & input1.Row).Value = Range("C" & scenario1.Row) <-- this generates the erro

我的主宏调用4个子宏,然后执行一行代码,然后生成“需要对象”错误。我不明白为什么,因为我正在提供一个对象(至少我认为我是)

我的代码如下所示:

Sub main_macro()
    Call Mac1
    Call Mac2
    Call Mac3
    Call Mac4
    Range("B" & input1.Row).Value = Range("C" & scenario1.Row)     <-- this generates the error
End Sub

Sub Mac1()
   Dim input1 As Range
End Sub

Sub Mac2()
   Dim scenario1 As Range
End Sub

Sub Mac3()
   Set input1 = Range("A:A").Find("location1", LookIn:=xlValues, LookAt:=xlWhole)
End Sub

Sub Mac4()
   Set scenario1 = Range("A:A").Find("location2", LookIn:=xlValues, LookAt:=xlWhole)
End Sub
Sub-main_宏()
打电话给Mac1
打电话给Mac2
打电话给Mac3
打电话给Mac4

Range(“B”和input1.Row).Value=Range(“C”和scenario1.Row)这就是您要尝试的吗

您的变量在过程中声明,其他人无法使用。因此你不需要初始化它们

Option Explicit

Sub main_macro()
    Dim input1 As Range, scenario1 As Range

    With Sheets("Sheet1") '<~~ Change this to the relevant Sheet
        Set input1 = .Range("A:A").Find("location1", LookIn:=xlValues, LookAt:=xlWhole)

        If input1 Is Nothing Then
            MsgBox "location1 not found"
            Exit Sub
        End If

        Set scenario1 = .Range("A:A").Find("location2", LookIn:=xlValues, LookAt:=xlWhole)

        If scenario1 Is Nothing Then
            MsgBox "location2 not found"
            Exit Sub
        End If

        .Range("B" & input1.Row).Value = Range("C" & scenario1.Row).Value
    End With
End Sub
选项显式
子主控_宏()
变暗输入1作为范围,场景1作为范围

使用Sheets(“Sheet1”)时,不要让它们都是宏并调用它们,而是将每个宏中的代码放入主宏中。变量只在子变量内,除非您将其签名为“全局”变量。是的,我实际上在一个宏中就有了所有变量,但由于我多次这样做,我遇到了“过程太大”错误,所以我决定将其分解。看起来像是在模块顶部添加了“public”声明修复了它。谢谢这不是我的意思吗?:)如果使用的是
,正确处理输出也很重要。如下图所示查找