Creating a new control

MrPloppy

New Member
Joined
Feb 9, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi all -

For a project I'm doing, I've created a "Heat Map", which is basically 10 coloured labels in a Frame and a few labels. I've done this by putting 10 labels (etc) on a form (at design time) where I want the heat map, then programmatically changing the width / height of those labels to fit inside the Frame. I then set the colours of those labels to different colours depending upon some data I have.

I'm happy with how it looks, but now I realise I need this Heat Map on a number of other forms, so I'd like to convert it into a Class Module, so I can just call them up programmatically whenever I need it. I'm happy with doing this, but I wouldn't mind a bit of a starter with it. I've googled this, and I can't find anything.

Does anyone have any pointer to some example code / tutorial where I can create a Class Module which contains a number of Controls (like Labels, Frames and so on), and how you initialise them and place that Class on a Form?

Many thanks,

Mr P.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You can't create a class containing controls I'm afraid (In VBA anyway). You could either just export the form that you have created and then you can import it into any projects that need it, or you'd have to create some code that creates the form the way you want (whether as a class or a normal module) and then use that in each project.
 
Upvote 0
I would write a custom class clsHeatMap. In clsHeatMap, put a .Frame property

VBA Code:
' in clsHeatMap
Dim pFrame as MS.Forms.Frame

Property Get Frame() as Ms.Forms.Frame
    Set Frame = pFrame
End Property
Property Set Frame(inFrame as Ms.Forms.Frame)
    Dim i as Long
    Set pFrame = inFrame
    With pFrame
        For 1 = 1 to 10
            With .Controls.Add("Forms.Label.1")
                ' set property of new control
             End With
        Next i
    End With
End Property

When, in your Userform's code, you set a empty frame as the .Frame property of a clsHeatMap object, the appropriate labels would be created.
VBA Code:
' in userform code module

Set myHeatMap as New clsHeatMap

Set myHeatMap.Frame = Userform1.Frame1
 
Upvote 0
Solution

Forum statistics

Threads
1,214,984
Messages
6,122,601
Members
449,089
Latest member
Motoracer88

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