Excel 需要组合框来自动更新
如上图所示,我有两个组合框:选择维度和可能值列表 用户可以选择不同的维度,每个维度都有一个可能值列表。我的代码部分工作正常。当我第一次选择维度时,可能值的列表会正确显示 问题:选择其他维度时,仍会显示上一维度的可能值列表,而不是新选定维度的值 问:有没有办法解决这个问题?因此,当我在维度之间切换时,可能值的列表也会更新 1:从工作表中获取值并将其添加到“可能值列表”组合框中的函数Excel 需要组合框来自动更新,excel,vba,Excel,Vba,如上图所示,我有两个组合框:选择维度和可能值列表 用户可以选择不同的维度,每个维度都有一个可能值列表。我的代码部分工作正常。当我第一次选择维度时,可能值的列表会正确显示 问题:选择其他维度时,仍会显示上一维度的可能值列表,而不是新选定维度的值 问:有没有办法解决这个问题?因此,当我在维度之间切换时,可能值的列表也会更新 1:从工作表中获取值并将其添加到“可能值列表”组合框中的函数 Public Function DimValuesSearch(strSearch As String) Call
Public Function DimValuesSearch(strSearch As String)
Call loadWbVariables
Dim selectedDimension As String, possibleValue As String
Dim countValues As Long
Dim DimCell As Range
selectedDimension = frmSeg.seg_cbb_selDim.Value
Set DimCell = dtValWs.Rows(1).Find(What:=strSearch)
If selectedDimension = strSearch Then
countValues = 0
While dtValWs.Cells(4 + countValues, DimCell.Column) <> ""
possibleValue = dtValWs.Cells(4 + countValues, DimCell.Column)
frmSeg.seg_cbb_posVal.AddItem possibleValue
countValues = countValues + 1
Wend
End If
End Function
您需要使用维度框的更改事件删除其他组合中的所有项目,并使用所需项目重新加载它 它看起来像这样:
Private Sub ComboBoxDimensions_Change()
Me.ComboBox2.Clear
LoadValuesIntoComboBox2
Me.ComboBox3.Clear
LoadValuesIntoComboBox3
End Sub
Public Sub LoadValuesIntoComboBox2()
'your code to reload the desired data ino ComboBox2
End Sub
Public Sub LoadValuesIntoComboBox3()
'your code to reload the desired data ino ComboBox3
End Sub
它比我想象的要简单,我必须对上面的函数做一个小的修改。以下是新功能:
Public Function DimValuesSearch(strSearch As String)
Call loadWbVariables
Dim selectedDimension As String, possibleValue As String
Dim countValues As Long
Dim DimCell As Range
selectedDimension = frmSeg.seg_cbb_selDim.Value
Set DimCell = dtValWs.Rows(1).Find(What:=strSearch)
If selectedDimension = strSearch Then
countValues = 0
frmSeg.seg_cbb_posVal.Clear 'Added this line here
While dtValWs.Cells(4 + countValues, DimCell.Column) <> ""
possibleValue = dtValWs.Cells(4 + countValues, DimCell.Column)
frmSeg.seg_cbb_posVal.AddItem possibleValue
countValues = countValues + 1
Wend
End If
End Function
您需要使用维度框的更改事件删除其他组合中的所有项目,并使用所需项目重新加载该组合如果这样做没有帮助,请提供一个。我不确定在这种情况下应该在何处插入.Clear函数。你能再解释一下吗?我还需要提供更多的代码吗?它不起作用,它只是清除了所有内容:/@soraia635当然,您需要编写填充组合框的代码。我使用上面描述的调用函数来实现这一点。这就是加载值组合框的原因。
Public Function DimValuesSearch(strSearch As String)
Call loadWbVariables
Dim selectedDimension As String, possibleValue As String
Dim countValues As Long
Dim DimCell As Range
selectedDimension = frmSeg.seg_cbb_selDim.Value
Set DimCell = dtValWs.Rows(1).Find(What:=strSearch)
If selectedDimension = strSearch Then
countValues = 0
frmSeg.seg_cbb_posVal.Clear 'Added this line here
While dtValWs.Cells(4 + countValues, DimCell.Column) <> ""
possibleValue = dtValWs.Cells(4 + countValues, DimCell.Column)
frmSeg.seg_cbb_posVal.AddItem possibleValue
countValues = countValues + 1
Wend
End If
End Function