tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,913
- Office Version
- 365
- 2019
- Platform
- Windows
This is in a standard module:
This is ClsInterface:
This is ClsNonEmpty:
This is ClsEmpty:
The above code does not compile. The error message is:
and points to this line:
in particular, highlighting this word:
If I changed it to:
it works.
Note I DON'T have to change the ClsInterface_RngElement inside the loop, ie
works.
But even if I DO change it to:
it also works.
Can someone please explain what is wrong?
Thanks
Code:
Option Explicit
Public Sub Test()
Dim MyClsInterface As ClsInterface
Select Case Sheet1.Cells(2, 1).Value
Case vbNullString
Set MyClsInterface = ClassFactory(Val:="Empty")
Case Else
Set MyClsInterface = ClassFactory(Val:="Not empty")
End Select
Dim Rng As Range
Dim Rngelement As Range
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
Set Rng = .Range(.Cells(2, 6), .Cells(10, 6))
End With
Set MyClsInterface.Rng = Rng
Set MyClsInterface.Rngelement = Rngelement
Call MyClsInterface.SomeMethod
Next ws
Set ws = Nothing
Set Rng = Nothing
Set Rngelement = Nothing
End Sub
Private Function ClassFactory(ByRef Val As String) As ClsInterface
Select Case Val
Case "Empty"
Set ClassFactory = New ClsEmpty
Case "Not empty"
Set ClassFactory = New ClsNotEmpty
End Select
End Function
This is ClsInterface:
Code:
Option Explicit
Private pRng As Range
Private pRngElement As Range
Public Property Get Rng() As Range
Set Rng = pRng
End Property
Public Property Set Rng(ByVal R As Range)
Set pRng = R
End Property
Public Property Get Rngelement() As Range
Set Rngelement = pRngElement
End Property
Public Property Set Rngelement(ByVal RElement As Range)
Set pRngElement = RElement
End Property
Public Sub SomeMethod()
End Sub
This is ClsNonEmpty:
Code:
Option Explicit
Implements ClsInterface
Private pClsInterface_Rng As Range
Private pClsInterface_RngElement As Range
Public Property Get ClsInterface_Rng() As Range
Set ClsInterface_Rng = pClsInterface_Rng
End Property
Public Property Set ClsInterface_Rng(ByVal R As Range)
Set pClsInterface_Rng = R
End Property
Public Property Get ClsInterface_RngElement() As Range
Set ClsInterface_RngElement = pClsInterface_RngElement
End Property
Public Property Set ClsInterface_RngElement(ByVal RElement As Range)
Set pClsInterface_RngElement = RElement
End Property
Private Sub ClsInterface_SomeMethod()
Dim Coll As Collection
Set Coll = New Collection
Coll.Add "apple"
Coll.Add "orange"
Dim CollElement As Variant
For Each CollElement In Coll
For Each ClsInterface_RngElement In ClsInterface_Rng.Cells
If InStr(1, ClsInterface_RngElement.Text, CollElement, vbBinaryCompare) <> 0 Then MsgBox "hi"
Next ClsInterface_RngElement
Next CollElement
Set CollElement = Nothing
End Sub
This is ClsEmpty:
Code:
Option Explicit
Implements ClsInterface
Private pClsInterface_Rng As Range
Private pClsInterface_RngElement As Range
Public Property Get ClsInterface_Rng() As Range
End Property
Public Property Set ClsInterface_Rng(ByVal R As Range)
End Property
Public Property Get ClsInterface_RngElement() As Range
End Property
Public Property Set ClsInterface_RngElement(ByVal RElement As Range)
End Property
Private Sub ClsInterface_SomeMethod()
End Sub
The above code does not compile. The error message is:
Code:
Variable required -can't assign to this operation
and points to this line:
Code:
For Each ClsInterface_RngElement In ClsInterface_Rng.Cells
in particular, highlighting this word:
Code:
ClsInterface_RngElement
If I changed it to:
Code:
For Each pClsInterface_RngElement In ClsInterface_Rng.Cells
If InStr(1, ClsInterface_RngElement.Text, CollElement, vbBinaryCompare) <> 0 Then MsgBox "hi"
Next pClsInterface_RngElement
it works.
Note I DON'T have to change the ClsInterface_RngElement inside the loop, ie
Code:
If InStr(1, ClsInterface_RngElement.Text, CollElement, vbBinaryCompare) <> 0 Then MsgBox "hi"
works.
But even if I DO change it to:
Code:
If InStr(1, pClsInterface_RngElement.Text, CollElement, vbBinaryCompare) <> 0 Then MsgBox "hi"
it also works.
Can someone please explain what is wrong?
Thanks