微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

切换到另一个工作表后组合框不更新

如何解决切换到另一个工作表后组合框不更新

所以我仍然是 VBA 的菜鸟,我制作了两个 ComboBox,第一个 ComboBox 用于工作簿中的工作表列表,第二个 ComboBox 是从特定列读取的名称列表。第一次尝试时它工作正常,但是,当通过使用第一个组合框切换到另一个工作表时,第二个 ComboBox 仍然具有我打开的前一个工作表中的旧名称列表。我一直在寻找关于如何在从第一个 ComboBox 中选择新工作表后更新第二个 ComboBox解决方案,我想知道是否有任何方法可以让第二个 ComboBox 刷新?这是我处理两个文本框的代码

这是用户窗体启动时的函数,我首先用工作簿中的工作表填充第一个 ComboBox,然后填充第二个 ComboBox

Private Sub UserForm_Initialize()

'Worksheet object
Dim sht As Worksheet

'Sets the ComboBox1 to "Select a Project"
ComboBox1.Value = "Select a Project"

'Name of the Sheet That is Excluded
Dim prjSum As String
prjSum = "Pjct Resource Summary"


'For every sheet in the workbook
For Each sht In ActiveWorkbook.Sheets
    If Not (sht.name = prjSum) Then 'Excluding PRS
        ComboBox1.AddItem (sht.name) 'Add to the ComboBox1 list
    End If
Next sht

'Populate ComboBox2 starting from E4 to the last name in E column
ComboBox2.List = ActiveSheet.Range("E4",Range("E4").End(xlDown)).Value
End Sub

这里我只是从它们各自的单元格中设置文本框。我尝试在此处填充我的第二个 ComboBox,但由于某种原因,我的 ComboBox不显示任何内容显示工作簿中第一个工作表中的名称

Private Sub ComboBox2_Change()

currWrkSheet = ComboBox1.Value


If currWrkSheet = "Select a Project" Then
    MsgBox ("Select a Project")
    Exit Sub
End If



'ComboBox2.List = ActiveSheet.Range("E4",Range("E4").End(xlDown)).Value

'Once getting the value of the comboBox,it should show the values from the corresponding texBoxes/comboBoxes
TextBox2.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,2).Value 'BU
TextBox3.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,3).Value 'Function
TextBox4.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,4).Value 'Resource Type
TextBox5.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,6).Value 'Project Role
TextBox6.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,7).Value 'Project Responsibility

'TextBoxes for the Months
TextBox7.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,8).Value
TextBox8.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,9).Value
TextBox9.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,10).Value
TextBox10.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,11).Value
TextBox11.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,12).Value
TextBox12.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,13).Value
TextBox13.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,14).Value
TextBox14.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,15).Value
TextBox15.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,16).Value
TextBox16.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,17).Value
TextBox17.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,18).Value
TextBox18.Value = Worksheets(currWrkSheet).Cells(ComboBox2.ListIndex + 4,19).Value
End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?