1

I have a Class InsertInfo with the Properrty

Private myPPTRange As Range

Public Property Get PPTRange() As Range
PPTRange = myPPTRange
End Property
Public Property Set PPTRange(ByVal value As Range)
myPPTRange = value
End Property

Now I want to assign a range to it with

Sub test()

Dim objInfo as New InsertInfo

    Set objInfo.PPTRange = ThisWorkbook.Worksheets("Tab").Cells(1,2)

End Sub

Usually assigning a cell to a range works fine in the code but in the situation above I always get the error:

"Objectvariable or with-blockvariable not set"

So to me it looks like it has something to do with the class property but I just cannot see what the problem is. Anyone who can help me with it?

EDIT:

Dim rngTemp as Range
Set rngTemp = ThisWorkbook.Worksheets("Tab").Cells(1,2)

works fine btw. So it really looks like I have to adjust my class property. I already tried a ByRef instead of a ByVal but I still get the Error Message.

Community
  • 1
  • 1
ruedi
  • 5,005
  • 14
  • 49
  • 80

2 Answers2

1

You need to use Set while assigning to a range object.

myPPTRange is a range. So in your class module, change the line myPPTRange = value to Set myPPTRange = value

Siddharth Rout
  • 142,730
  • 17
  • 199
  • 246
  • Do both his Get and Set Properties need to be changed to "Set PPTRange"? – user117294 Nov 16 '21 at 05:07
  • @user117294: Yes. Whenever you are working with objects, you need to use SET in such scenarios. You may want to see [What is the difference between dim and set in vba](https://stackoverflow.com/questions/3872339/what-is-the-difference-between-dim-and-set-in-vba) – Siddharth Rout Nov 16 '21 at 06:25
0

In addition to using SET inside your property, you may also need to define your object in two lines instead of single line definition syntax.

Dim objInfo as InsertInfo
set objInfo = New InsertInfo
Stephen Rauch
  • 44,696
  • 30
  • 102
  • 125