Vba Excel";运行时错误';424';:“所需对象”;调用布尔函数时

Vba Excel";运行时错误';424';:“所需对象”;调用布尔函数时,vba,excel,Vba,Excel,VBA新手,并试图学习尽可能多,所以请不要犹豫,过度通知 目标:在子对象中,当两个对象重叠时,调用一个返回布尔值true的函数。其思想是让用户能够在彼此之间拖放形状,以便轻松创建层次结构 问题:我收到注释行标题中所述的“需要对象”错误。RecA和RecB是函数参数中定义的有问题的形状。名为“重叠”的函数位于模块1中 Public Sub CommandButton1_Click() Dim Function_Result As Boolean Function_Result =

VBA新手,并试图学习尽可能多,所以请不要犹豫,过度通知

目标:在子对象中,当两个对象重叠时,调用一个返回布尔值true的函数。其思想是让用户能够在彼此之间拖放形状,以便轻松创建层次结构

问题:我收到注释行标题中所述的“需要对象”错误。RecA和RecB是函数参数中定义的有问题的形状。名为“重叠”的函数位于模块1中

Public Sub CommandButton1_Click()
    Dim Function_Result As Boolean
    Function_Result = Overlap(RecA, RecB) '<--------!

    If Function_Result = True Then
            MsgBox ("swiggity swooty")
    End If
End Sub
公共子命令按钮1\u单击()
Dim函数_结果为布尔值

函数_Result=Overlap(RecA,RecB)“您没有正确定义您的形状。如果函数使用对象属性来确定重叠,则需要在
CommandButton1\u Click
事件中执行以下操作:

Private Sub CommandButton1_Click()
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1) 'Assumed on worksheet index 1
    Dim RecA As Shape: Set RecA = ws.Shapes("RecA") 'Assumed RecA is the name of your shape?
    Dim RecB As Shape: Set RecB = ws.Shapes("RecB")
    Dim Function_Result As Boolean

    Function_Result = Overlap(RecA, RecB) 

    If Function_Result = True Then
            MsgBox ("swiggity swooty")
    End If
End Sub

这样,您已经在VBA中将形状设置为对象,现在可以引用它们的对象属性。

有效代码:

Private Sub CommandButton1_Click()
    Dim Function_Result As Boolean
    Dim RecA As Shape
    Dim RecB As Shape

    Function_Result = Overlap(RecA, RecB)

    If Function_Result = True Then
            MsgBox "swiggity swooty"
    End If
End Sub
我能够让它比Tyeler的答案简单一点,因为我在我的
重叠
函数本身中设置了
形状(见下文)

函数重叠(RecA为形状,RecB为形状)为布尔值
调暗SHP1左为单个
将SHP1灯光调暗为单灯
将SHP1顶部调暗为单个
将SHP1底部调暗为单个
调暗SHP2左为单
将SHP2灯光调暗为单灯
将Shp2Top变暗为单个
将Shp2Bottom变暗为单个
作为布尔值
将顶点重叠设置为布尔值
Set RecA=Sheet1.形状(“RecA”)
Set RecB=Sheet1.形状(“RecB”)
与雷卡
Shp1Left=.Left
SHP1右=左+宽
Shp1Top=.Top
SHP1底部=顶部+高度
以
有记录
Shp2Left=.Left
Shp2Right=.Left+.Width
Shp2Top=.Top
Shp2Bottom=顶部+高度
以
''''''''''''''''''''''''''''''''''''''''''''''
“它们水平重叠吗?
如果SHP1左>SHP2左,则
如果SHP1左SHP2左,则
HorOverlap=True
如果结束
如果结束
“它们垂直重叠吗?
如果SHP1顶部>SHP2顶部,则
如果SHP1顶部SHP2顶部,则
垂直重叠=真
如果结束
如果结束
重叠=水平重叠和垂直重叠
端函数

这些形状是在哪里定义的?如果这些形状在工作表上,并且这是CommandButton1_Click()中的所有代码(我认为应该是私有的,而不是公共的),那么我可以告诉您VBA不会将RecA和RecB识别为除空变量以外的任何其他变量。至少,发布
重叠
的标题,在何处/如何定义
RecA
RecB
,函数是否使用形状属性进行此确定?像
.Left
.Top
.Width
.Height
?如果我们不知道
重叠
对参数的要求,也不知道您给出的类型和值,我们应该如何帮助您?请您的问题包含相关信息。事件处理程序处理事件,除非您将其公开,否则它们是私有的,这是错误的代码,因为没有其他代码需要直接调用事件处理程序。Public/Private控制过程的可访问性,而不是其局部变量的可访问性。无论过程的可访问性如何,局部变量始终是它们声明所在过程范围的局部变量。这是我的错误。谢谢我还用我的所有代码回答了我自己的问题,以帮助那些可能希望获得更好图片的人。您可以删除
=TRUE
部分的
If…Then
语句,或者使用
If重叠(RecA,RecB)然后
并删除
函数\u结果
部分。这样做,不需要在单击事件中定义
RecA
RecB
,只需在重叠中定义它们,并将函数更改为
Function Overlap()作为布尔值
-如果将它们保留在单击事件中,则可以从其他过程向函数传递不同的形状。
Function Overlap(RecA As Shape, RecB As Shape) As Boolean

Dim Shp1Left As Single
Dim Shp1Right As Single
Dim Shp1Top As Single
Dim Shp1Bottom As Single

Dim Shp2Left As Single
Dim Shp2Right As Single
Dim Shp2Top As Single
Dim Shp2Bottom As Single

Dim HorOverlap As Boolean
Dim VertOverlap As Boolean

Set RecA = Sheet1.Shapes("RecA")
Set RecB = Sheet1.Shapes("RecB")

With RecA
    Shp1Left = .Left
    Shp1Right = .Left + .Width
    Shp1Top = .Top
    Shp1Bottom = .Top + .Height
End With

With RecB
    Shp2Left = .Left
    Shp2Right = .Left + .Width
    Shp2Top = .Top
    Shp2Bottom = .Top + .Height
End With
''''''''''''''''''''''''''''''''''''''''''''''
' do they overlap horizontally?
If Shp1Left > Shp2Left Then
    If Shp1Left < Shp2Right Then
        HorOverlap = True
    End If
End If
If Shp1Left < Shp2Left Then
    If Shp1Right > Shp2Left Then
        HorOverlap = True
    End If
End If

' do they overlap vertically?
If Shp1Top > Shp2Top Then
    If Shp1Top < Shp2Bottom Then
        VertOverlap = True
    End If
End If
If Shp1Top < Shp2Top Then
    If Shp1Bottom > Shp2Top Then
        VertOverlap = True
    End If
End If

Overlap = HorOverlap And VertOverlap

End Function