Classes in Add-Ins

pp_17

New Member
Joined
Jun 9, 2016
Messages
2
How can I embed this class in an XLAM Add-In.. and make it work?

Dize Class
Code:
Private pSides As IntegerPublic Property Let sides(value As Integer)
    pSides = value
End Property
Public Property Get sides() As Integer
    sides = pSides
End Property
Public Function throw() As Integer
    throw = Int(Rnd() * pSides) + 1
End Function

I've tried doing it, I even get the Dize.xlam in the VBA project inspector:
dize.jpg


However, I don't have any intellisense option when typing:

Code:
Dim myDice as D... (Dize does not appear in the list)

And even if I write

Code:
Dim myDice as Dize

I get the error "User-defined type not defined"

What am I doing wrong?? Any help to get classes in Add-Ins going?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
- Change the name of the project from "VBAProject". Something like "DizeProject" for example.
- Make sure that the Instancing of the class is set to 2 - PublicNotCreatable
- Add a new module and create a function that will create a new instance of your class e.g.:

Code:
Public Function InstantiateDizeClass() As Dize

Set InstantiateDizeClass = New Dize

End Function


- Ensure that you are referencing your DizeProject in the Tools/References dialog in you new Book1 project.
- Use the public function to instantiate the class. I did this:

Code:
Sub Test()

Dim myDice As DizeProject.Dize

Set myDice = DizeProject.InstantiateDizeClass

Debug.Print myDice.throw
Debug.Print myDice.throw
Debug.Print myDice.throw

End Sub

I got it working like this. Hope that helps.

WBD
 
Last edited:
Upvote 0
Oops. Forgot to say I also changed your class to add a constructor:

Code:
Private pSides As Integer
Public Property Let sides(value As Integer)
    pSides = value
End Property
Public Property Get sides() As Integer
    sides = pSides
End Property
Public Function throw() As Integer
    throw = Int(Rnd() * pSides) + 1
End Function
Public Sub Class_Initialize()

pSides = 6

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top