跳到主要內容

如何在Excel中的UserForm上基於組合框選擇填充文本框?

假設您有一個包含“名稱”和“職員編號”列的表,如下圖所示。 現在,您需要創建一個嵌入有一個組合框的用戶窗體,該組合框收集內部的所有名稱,並且在從組合框選擇名稱時將填充一個文本框,該文本框將填充相應的職員編號。 本文中的方法可以幫助您輕鬆實現目標。

使用VBA根據用戶窗體上的組合框選擇填充文本框


使用VBA根據用戶窗體上的組合框選擇填充文本框

請執行以下操作,以根據用戶窗體上的組合框選擇為文本框填充相應的值。

1。 按 其他 + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

2。 在裡面 Microsoft Visual Basic for Applications 窗口中,單擊 插入 > 用戶表格。 看截圖:

3.然後將組合框控件和文本框控件插入到創建的用戶窗體中,如下圖所示。

4.右鍵單擊用戶窗體的任何空白,然後單擊 查看代碼 從上下文菜單中。 看截圖:

5.在打開的用戶窗體(代碼)窗口中,請複制並粘貼以下VBA代碼以替換原始代碼。

VBA代碼:根據用戶窗體上的組合框選擇填充文本框

Dim xRg As Range
'Updated by Extendoffice 2018/1/30
Private Sub UserForm_Initialize()
    Set xRg = Worksheets("Sheet5").Range("A2:B8")
    Me.ComboBox1.List = xRg.Columns(1).Value
End Sub
Private Sub ComboBox1_Change()
    Me.TextBox1.Text = Application.WorksheetFunction.VLookup(Me.ComboBox1.Value, xRg, 2, False)
End Sub

備註:在程式碼中,A2:B2 是包含您將在使用者窗體組合方塊和文字方塊中分別填入的值的範圍。 Sheet5 是包含資料範圍的工作表的名稱。 請根據需要更改它們。

6。 按 其他 + Q 退出鍵 Microsoft Visual Basic for Applications 窗口。

7。 點擊 開發者 > 插入 > 命令按鈕(ActiveX控件) 在工作表中插入命令按鈕。

8. 右鍵點選工作表標籤並選擇 查看代碼 從右鍵選單中。 然後將 VBA 程式碼複製並貼上到程式碼視窗中。

VBA程式碼:顯示使用者表單

Private Sub CommandButton1_Click()
'Updated by Extendoffice 2018/1/30
    UserForm1.Show
End Sub

9. 關閉工作簿中的設計模式。

點選命令按鈕開啟指定的使用者窗體。 然後您可以看到所有名稱都收集在組合框中。 從組合方塊中選擇姓名時,對應的員工編號將自動填入文字方塊中,如下圖所示。


相關文章:

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
can the same be don on excel sheet? instead of userform? i am using sheet not userform
This comment was minimized by the moderator on the site
alguem poderia me ajudar estou tentando carregar a combobox e puxar informações para text box so que esta dando erro 381

Private Sub ComboNome_Emissor_Change()
TextArea.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 1)
TextTreinamento_PT.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 3)
TextTreinamento_EC.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 5)
TexStatus_Bloqueio.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 6)


End Sub
This comment was minimized by the moderator on the site
The method is very simple and populates the combo box quite efficiently. However, when I try populating the text box, I get a run-time error '1004': Unable to get the VLookup property of the WorksheetFunction class. Kindly assist with resolving this error.
This comment was minimized by the moderator on the site
I just tried this code and was running into the same issue. I realized I had the xRg variable inside the UserForm block, so it was not being accessed by my ComboBox function. Maybe others are running into the same issue
This comment was minimized by the moderator on the site
Hi,
May I have your Office version? I need the feedback to check for the error.
Thanks for your comment.
This comment was minimized by the moderator on the site
office 2010 32 bit
This comment was minimized by the moderator on the site
Good day,
I have tried the code in Office 2010 32 bit, but no errors occured.
Please make sure the sheet name and cell range in the first VBA code are the same with your actual sheet name and range.
Thanks for your comment.
This comment was minimized by the moderator on the site
Hi,
i have same issue with office 2013. Any solution please
This comment was minimized by the moderator on the site
i have the same problem....
This comment was minimized by the moderator on the site
I too have the same problem
This comment was minimized by the moderator on the site
Hi Chaylon,
Have you replaced the sheet name in the below code line with your actual used worksheet name?
Set xRg = Worksheets("Sheet5").Range("A2:B8")
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations