你可以試試這個
這裏是你的「主」子代碼摘錄相關的提問
Option Explicit
Public TPIDRng As Range, branchRng As Range, companyRng As Range, MQRng As Range
Sub main()
Set TPIDRng = GetColumnUsedRange(Worksheets("TPIDLookup"), "A")
Set branchRng = GetColumnUsedRange(Worksheets("TPIDLookup"), "B")
Set companyRng = GetColumnUsedRange(Worksheets("TPIDLookup"), "C")
Set MQRng = GetColumnUsedRange(Worksheets("TPIDLookup"), "D")
With UserForm1 ' change it to whatever name your actual UserForm has
.ComboBox1.List = Application.Transpose(TPIDRng)
.Show
End With
Unload UserForm1
End Sub
Function GetColumnUsedRange(sht As Worksheet, col As String)
With sht
Set GetColumnUsedRange = .Range(.Cells(1, col), .Cells(.Rows.Count, col).End(xlUp))
End With
End Function
和這裏的對應UserForm代碼窗格中的代碼
Private Sub ComboBox1_Change()
With Me
If .ComboBox1.ListIndex = -1 Then
.TextBoxBranch.Text = ""
.TextBoxCompany.Text = ""
.TextBoxMQ.Text = ""
Else
.TextBoxBranch.Text = branchRng.Cells(.ComboBox1.ListIndex + 1, 1).Value
.TextBoxCompany.Text = companyRng.Cells(.ComboBox1.ListIndex + 1, 1).Value
.TextBoxMQ.Text = MQRng.Cells(.ComboBox1.ListIndex + 1, 1).Value
End If
End With
End Sub
Private Sub CommandButton1_Click()
Me.Hide
End Sub
更改名稱(for ran ges,Userform,TextBoxes和Combobox)根據您的需求
John可以請您發佈您的所有代碼?理解你想要完成的事情有點麻煩。 – StormsEdge
替換爲像這樣的每個循環'對於WS.Range中的每個TPID(「B2:B10」)'。只是假設 –