0
我試圖創建一個函數,它根據用戶所需的文本框數量以編程方式將文本框添加到空白的用戶窗體中。以編程方式添加vba文本框中的數據
目前我有一個這樣做的功能,但我無法保存文本框的值。我曾嘗試引用文本框的幾種方法但他們都不工作(儘管在一個單獨的代碼工作之前同樣的方法,但並沒有以編程方式添加問題的文本框)
Function addtxtbox(number_of_textboxes As Integer)
Dim option_names As New UserForm2
Dim names As String
Dim test As String
Dim textbox As Object
Dim submit As Object
For i = 1 To number_of_textboxes
Set textbox = option_names.Controls.Add("Forms.textbox.1")
With textbox
.Left = 30
.Width = 200
.Top = 20 * i
.Left = 20
End With
MsgBox (textbox.Name)'used to find the name of the textboxes
Next
option_names.Show
names = "TextBox1" 'correct name of the 1st textbox according to the msgbox above.
MsgBox (names) 'msgbox is always blank
test = option_names.names 'Compile error: Method or data members not found
'test = textbox.TextBox1.Value 'run time error 418 object does not support this property or method
MsgBox (test)
End Function
什麼是你的'UserForm2'代碼以可預測的順序文本框? (我創建了一個名爲'Click'的窗體,名爲'addtxtbox 4'的UserForm1,以及一個帶'Click'事件形式的空白UserForm2,它簡單地做了一個'Me.Unload'。當我運行它並點擊第一個窗體,它正確添加了4個文本框,顯示了第二個表單,允許我在第一個文本框中輸入內容,然後在單擊表單後,代碼返回到顯示「TextBox1」消息的'addtxtbox'函數,並通過使用'test = option_names.Controls(names)',然後顯示輸入到文本框中的值。) – YowE3K
我的用於userform2的代碼只是一個空白的帶有命令按鈕的用戶窗體。命令按鈕的代碼爲 'Private Sub CommandButton1_Click() Me.Hide End Sub' 除此之外,沒有代碼 – foch
@ YowE3K我接受了您的建議並使用了和顯示代碼文本框數據。謝謝!!! –
foch