Home » excel » excel vba – How do I pass a range object to a property in VBA?

excel vba – How do I pass a range object to a property in VBA?

Posted by: admin May 14, 2020 Leave a comment

Questions:

though searched thoroughly I don’t get a range passed into a userform via property. I get the error object required. The code is inside the userform, the parameter prange is global. Any help would be appreciated much. Thanks.

Public Property Get MyRng() As Range

Set MyRng = r1

End Property

Public Property Set MyRng(ByRef prange As Range)

Set r1 = prange

End Property
How to&Answers:

That should be the code inside the userform

Option Explicit
Dim mRng As Range
Public Property Get myRng() As Range
    Set myRng = mRng
End Property

Public Property Set myRng(nRg As Range)
    Set mRng = nRg
End Property

Just a simple example

Option Explicit

    Sub Test()
    Dim frm As UserForm1
    Dim rg As Range

        Set frm = New UserForm1
        Set frm.myRng = Range("A1")
        Set rg = frm.myRng
        Debug.Print rg.Address

    End Sub