0

我的主宏调用 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
4

1 回答 1

1

这是你正在尝试的吗?

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

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
于 2012-05-20T22:49:26.780 回答