2014-01-31 16 views
0

我正在創建用於在清單電子表格上使用的用戶界面。在這個例子中,我試圖顯示選定用戶的細節以進行編輯或刪除。使用基於下拉選擇的電子表格數據填充文本框excel

我打算使用循環遍歷一列用戶名,當它擊中選定的用戶名時停止,並使用從電子表格獲得的值填充顯示的實體(這些詳細信息與用戶名位於同一行)

下面是代碼的樣子的例子。

Dim nameRange As Range 
Set nameRange = Range("A2:A100") 

For Each x In nameRange 
    If x = UserFrameUserDropdown.Value Then 
     UserFrame_FName.Value = **<code to go here>** 
     UserFrame_LName.Value = **<code to go here>** 
     UserFrame_Country.Value = **<code to go here>** 
     UserFrame_State.Value = **<code to go here>** 
     UserFrame_Position.Value = **<code to go here>** 
     Exit Sub 
    End If 
Next 

任何幫助表示讚賞。

回答

0

如果要填充到您的工作表中的數據表中的坐在整齊地旁邊的用戶名一欄,您可以使用偏移把數據

Dim nameRange As Range 
Set nameRange = Range("A2:A100") 

For Each x In nameRange 
    If x = UserFrameUserDropdown.Value Then 
     UserFrame_FName.Value = x.offset(0, 1) 
     UserFrame_LName.Value = x.offset(0, 2) 
     UserFrame_Country.Value = x.offset(0, 3) 
     UserFrame_State.Value = x.offset(0, 4) 
     UserFrame_Position.Value = x.offset(0, 5) 
     Exit Sub 
    End If 
Next 

這裏假設你的數據Fname,Lname,Country,State和Position分別位於B,C,D,E和F列,與用戶名相對應的行是

+0

我已將代碼添加到UserFrameUserDropdown_Change子項中,但是當值更改時沒有填充。 可能是因爲x沒有被定義爲一個單元格嗎? – uberthane

+0

Private Sub UserFrameUserDropdown_Change() *您的代碼在這裏* End Sub – Silenxor

+0

多數民衆贊成在哪裏。 我改變FName參數線路此基於另一答案: UserFrame_FName.Value =纈氨酸(x.offset(0,2)) 結果是FName參數盒中有一個0。 – uberthane

相關問題