一个表是grouphead,另一个表是控制头。
在grouphead表中,有几个唯一记录及其ID。与非流动资产一样,ID为NCA,流动资产具有ID CA等。 在控制台表中,有几个唯一记录以及grouphead id。像Salman和Amir一样,NCA和Abdur Rehman以及Rahim都有身份证。
当我打开userform时,grouphead组合框填充grouphead,groupcode id文本框显示在combobox中选择的项目的id。
还有另一个名为controlhead的组合框。 我想要的是,控制头组合框只会填充那些在grouphead文本框中有id的值。
到目前为止,我的代码如下:
Private Sub ComboBox1_Change()
Dim start As String
Dim start2 As String
Dim sfind As String
Dim sfind2 As String
Dim ws As Worksheet
Dim tbl As ListObject
Dim tbl2 As ListObject
Set ws = Sheets("Summary of Accounts")
Set tbl = ws.ListObjects("grouphead")
Set tbl2 = ws.ListObjects("controlhead")
With Me
If .ComboBox1.Value <> vbNullString Then
sfind = .ComboBox1.Value
start = Application.WorksheetFunction.VLookup(sfind, tbl.DataBodyRange, 2, False)
.TextBox1 = start
End If
End With
With Me
If .TextBox1.Value <> vbNullString Then
sfind2 = .TextBox1.Value
start2 = Application.WorksheetFunction.VLookup(sfind2, tbl2.DataBodyRange, 2, False)
.ComboBox2 = start2
End If
End With
End Sub
我无法填充控制头组合框。
请审核并帮助我。
答案 0 :(得分:0)
我的样本数据
逻辑代码
在行动
<强>代码
Dim tblGH As ListObject
Dim tblCH As ListObject
'~~> Populate combobox1 in userform init event
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Set ws = Sheets("Summary of Accounts")
Set tblGH = ws.ListObjects("grouphead")
Set tblCH = ws.ListObjects("controlhead")
For i = 1 To tblGH.DataBodyRange.Rows.Count
ComboBox1.AddItem tblGH.DataBodyRange.Cells(i, 1)
Next
End Sub
'~~> The click event
Private Sub ComboBox1_Click()
Dim r As Long
r = ComboBox1.ListIndex
If r = -1 Then Exit Sub
'~~> Add to txtbox
TextBox1.Text = tblGH.DataBodyRange.Cells(r + 1, 2)
'~~> Add to 2nd Combo
PopulateCombo
End Sub
Sub PopulateCombo()
ComboBox2.Clear
For i = 1 To tblCH.DataBodyRange.Rows.Count
If tblCH.DataBodyRange.Cells(i, 1) = TextBox1.Text Then _
ComboBox2.AddItem tblCH.DataBodyRange.Cells(i, 2)
Next
End Sub