excel - 从组合框中选择下一项,然后单击 Excel VBA 按钮

标签 excel combobox vba

在 Excel 工作表上放置一个组合框和命令按钮。 Combobox 会列出一些项目,比如它有 1、2、3、4、5。当第一次加载组合框时,默认情况下会选择第一个值,即 1。

现在使用 VBA 宏,我想从组合框列表中选择下一个值(即 2)并单击命令按钮。

我用谷歌搜索了这个,但不幸的是,没有得到我所期望的。

这是我到目前为止所拥有的,但它并没有达到我的预期(如上所述):Get a error message as Object does not support this property or method on line If Worksheets("QC Update").ComboBox1.SelectedIndex < ComboBox1.Items.Count - 1 Then

Sub Select_Next_Items() 
  If IsEmpty(Range("A9").Value) = True Then 
    If Worksheets("QC Update").ComboBox1.SelectedIndex < ComboBox1.Items.Count - 1 Then 
      ComboBox1.ListIndex = 0 ' select first Item in listbox 
      ComboBox1.ListIndex = ComboBox1.ListCount - 1 ' selects last item 
      Set ComboBox1.SelectedIndex = Worksheets("QC Update").ComboBox1.SelectedIndex + 1 
      Set ComboBox1.ListCount = 0 
    End If     
  End If 
End Sub

最佳答案

希望你们在这里不要感到被冒犯,因为我有一个更清晰,更直接的答案。

ComboBox1.ListIndex = ComboBox1.ListIndex +1

这是一个更完整的。因此,我们必须考虑到它何时位于列表的末尾
If ComboBox1.ListIndex = ComboBox1.ListCount -1 Then
   ComboBox1.ListIndex =1 

Else
   ComboBox1.ListIndex = ComboBox1.ListIndex +1

希望我的回复对大家有帮助,尤其是刚接触Vba,急需使用的 friend 。

如果您有扎实的 C++/java 编程背景,这很容易理解

关于excel - 从组合框中选择下一项,然后单击 Excel VBA 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43274242/

相关文章:

asp.net - 以 Excel 文件形式打开网页,但使用 .xlsx 文件

java - 如何从jtable内的组合框中获取值和选定的值?

excel - 如何将条件格式规则应用于除第一行之外的所有行?

excel - 一次选择高于限制值的所有单元格

sql-server - 尝试在 SSIS 中导入 Excel 文件时出现 "External table is not in the expected format."错误

wpf - 将空字符串绑定(bind)到组合框

java - 如何在类外部使用组合框变量或在组合框方法内部使用变量

Excel:如何加快查找和替换过程?每个工作簿更改 32,000 个链接

Excel 2007 - 1004 运行时错误刷新查询表

vba - 合并表格单元格