2016-10-11 4 views
-1

VBAの初心者で、できるだけ多くのことを学びたいので、躊躇しないでください。Excel "実行時エラー '424':Object required"ブール関数を呼び出すとき

目的:サブオブジェクト内で、2つのオブジェクトが重なったときにブール値trueを返す関数を呼び出します。アイデアは、階層を簡単に作成するために、ユーザーが形状をドラッグアンドドロップできるようにすることです。

問題:コメント行のタイトルで述べたように、私は「オブジェクト必要」エラーが発生します。 RecAとRecBは、関数の引数で定義されている問題の形状です。 "重複"という名前の関数がModule1にあります。私は私が無駄に(他の可能な解決策と一緒に)試みたFunction_Resultの前set修飾子を使用しての解決策を見つけることができた研究から

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 

機能コードがすべて役に立ちましたら教えてください。私はこの投稿に追加することができます。

Excel 2010

ありがとうございます!

+0

これらの形状はどこに定義されていますか?これらの図形がワークシート上にあり、それがCommandButton1_Click()(私はPublicでなくPrivateであるべきだと思う)のコードであれば、VBAは空の変数以外のものとしてRecAとRecBを認識しない。 – Tyeler

+0

少なくとも、オーバーラップのヘッダーを投稿し、/ How 'RecA'と' RecB'が定義されています –

+0

また、この決定にシェイププロパティを使用する関数はありますか? '.Left'、' .Top'、 '.Width'、' .Height'のように? – Tyeler

答えて

1

形状を正しく定義していません。あなたの関数は、重複を決定するために、オブジェクトのプロパティを使用している場合、あなたはあなたのCommandButton1_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でオブジェクトとしてあなたの形状を設定することができましたオブジェクトプロパティを参照するようになりました。作品

+0

それは私の間違いでした。ありがとう! 私は自分の質問に答えてくれました。私のコードは、それ以降に来た人たちのために、より良い画像を得たいと思うかもしれません。 – JPT

+1

'If ... Then'文の' = TRUE'部分を削除するか、 'IF Overlap(RecA、RecB)Then'を使用して' Function_Result'部分を完全に削除することができます。 –

+0

これはきれいです、ありがとう! – JPT

0

コード:

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 

私は(下見て)私のOverlap機能自体にset私の形状を有しているので、もう少し簡単なTyelerの答えよりもそれを維持することができました。

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 
+0

クリックイベントで 'RecA'と' RecB'を定義する必要はありません。オーバーラップで定義し、 'Function Overlap()as boolean'に変更するだけです。他のプロシージャから関数に異なる形を渡すこともできます。 –

関連する問題