2

Can someone please guide me on how to create the Date Picker control dynamically in VBA? Here is what I am trying to do. I have a macro that adds TextBox and ComboBox controls dynamically to a VBA userform, based on whether the end user would like it to be visible or not. The visibility(and all the other control properties-width,height, etc) are controlled by the end user, by updating Yes/No, values against the control names provided in a ‘Master’ sheet in excel.

This is what I did for the TextBox and ComboBox controls

`sub test()
 ---- some code

 Dim txtTextBox As MSForms.TextBox
 Dim cmbComboBox As MSForms.ComboBox

 If 'some cell in excel ‘Master’ worksheet' = "ComboBox" Then
 Set cmbComboBox = UserForm.Controls.Add("Forms.ComboBox.1", 'some cell in excel ‘Master’ worksheet')
 cmbComboBox.top = 'some cell in excel ‘Master’ worksheet'
 cmbComboBox.left = 'some cell in excel ‘Master’ worksheet'
 cmbComboBox.Width = 'some cell in excel ‘Master’ worksheet'
 cmbComboBox.height = 'cell in excel ‘Master’ worksheet'

  ----rest of my code
  end sub`

My question is, how do I add the Date picker dynamically, like the way I am adding the TextBox and ComboBox. I using Controls.Add the right way to do it? If so how can I do that? Can someone help me out with this! Hope my question makes sense.

Community
  • 1
  • 1
Navya
  • 23
  • 6

1 Answers1

0

Try this:

Dim dtDatePicker As Object
Set dtDatePicker = UserForm.Controls.Add("MSComCtl2.DTPicker", "dtp", True)
' Do whatever you want with dtDatePicker control here!
Ali
  • 121
  • 7