2016-08-24 244 views
2

某人請指導我如何在VBA中動態創建Date Picker控件? 這是我想要做的。我有一個宏,它根據最終用戶是否希望可見或不可見,將TextBox和ComboBox控件動態添加到VBA用戶窗體中。可見性(以及所有其他控件屬性 - 寬度,高度等)由最終用戶控制,通過更新是/否,針對Excel中「主」表中提供的控件名稱的值。動態創建DTPicker控件

這是我做過什麼對TextBox和ComboBox控件

`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` 

我的問題是,如何動態地添加日期選擇器,就像我加入TextBox和組合框的方式。我使用Controls.Add正確的方法來做到這一點?如果是這樣,我該怎麼做?有人可以幫我解決這個問題! 希望我的問題有道理。

+0

你見過[這](http://stackoverflow.com/questions/12012206/formatting-mm-dd-yyyy-dates-in-textbox-in-vba) –

回答

0

試試這個:

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