|
Is演算子は、オブジェクトを比較する演算子ですが・・・。
新規ブックの標準モジュールに
'==================================================================
Sub main()
Dim a As Object
Dim b As Object
MsgBox "まず" & vbCrLf & _
"Application Is Application = " & _
(Application Is Application)
MsgBox "次に" & vbCrLf & _
"ThisWorkbook Is ThisWorkbook = " & _
(ThisWorkbook Is ThisWorkbook)
MsgBox "さらに" & vbCrLf & _
"ActiveSheet Is ActiveSheet = " & _
(ActiveSheet Is ActiveSheet)
MsgBox "と、ここまではコードと照らし合わせても" & vbCrLf & _
"なんてことはないのですが・・・"
'
'
'
MsgBox "ここからが問題現象" & vbCrLf & vbCrLf & _
"Range(""a1"") Is Range(""a1"") = " & _
(Range("a1") Is Range("a1")) & vbCrLf & "あれ?"
Set a = Range("a1")
Set b = Range("a1")
MsgBox "これも" & vbCrLf & _
"Dim a As Object,b As Object" & vbCrLf & _
"Set a = Range(""a1"")" & vbCrLf & _
"Set b = Range(""a1"")" & vbCrLf & _
"a Is b = " & (a Is b) & vbCrLf & _
"あらら・・"
With ActiveSheet
On Error Resume Next
.Shapes("Rect1").Delete
On Error GoTo 0
With .Shapes.AddShape(msoShapeRectangle, [b10].Left, [b10].Top, _
[b10].Width, [b10].Height)
.Name = "Rect1"
.Select
End With
DoEvents
MsgBox "With .Shapes.AddShape(msoShapeRectangle, [b10].Left, [b10].Top, _" & vbCrLf & _
" [b10].Width, [b10].Height)" & vbCrLf & _
" .Name = ""Rect1""" & vbCrLf & _
" .Select" & vbCrLf & _
" End With" & vbCrLf & vbCrLf & _
"で、Rect1という四角形を作成しましたが・・・"
MsgBox "Shapes(""Rect1"") Is Shapes(""Rect1"") = " & _
(.Shapes("Rect1") Is .Shapes("Rect1")) & vbCrLf & _
"これもFalse"
End With
End Sub
上記のコードを実行してみて下さい。
Application Is Application
や
ThisWorkbook Is ThisWorkbook
は、予想通りTrueとなるのに、
Range(“A1”) Is Range(“A1”)
や
Shapes(“Rect1”) Is Shapes(“Rect1”)
は、Falseになってしまいます。
不思議ではないですか?
|
|