我正在Excel VBA中编写一个脚本,我在Userform
工作。在此Userform
中,我有两个ComboBox
,ComboBox1
和ComboBox4
。 ComboBox
都是一个填充在脚本中的下拉列表。
我已经完成了ComboBox4
之后填充ComboBox1
并取决于Private Sub UserForm_Activate()
With ComboBox1
.AddItem "Afghanistan"
.AddItem "Åland Islands"
.AddItem "Albania"
.AddItem "Algeria"
.AddItem "American Samoa"
.AddItem "Andorra"
End With
With ComboBox4
.AddItem "Africa"
.AddItem "Americas"
.AddItem "Asia"
.AddItem "Europe"
.AddItem "MENA"
.AddItem "Other"
End With
End Sub
Private Sub ComboBox1_Change()
If ComboBox1 = "" Then
ComboBox4 = vbNullString: ComboBox4.Enabled = True
Else
Dim index As Integer
index = ComboBox1.ListIndex
Select Case index
Case Is = 0
ComboBox4.Value = "Asia"
ComboBox4.Enabled = False
Case Is = 1
ComboBox4.Value = "Europe"
ComboBox4.Enabled = False
Case Is = 2
ComboBox4.Value = "Europe"
ComboBox4.Enabled = False
Case Is = 3
ComboBox4.Value = "MENA"
ComboBox4.Enabled = False
Case Is = 4
ComboBox4.Value = "Asia"
ComboBox4.Enabled = False
Case Is = 5
ComboBox4.Value = "Europe"
ComboBox4.Enabled = False
End Select
End If
End Sub
上的用户输入。我写了以下代码:
ComboBox1
一切似乎都很好。唯一的问题是Userform
有一种奇怪且烦人的行为:每当我打开ComboBox1
并从<nav class="navbar" style="background-color:orange;">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar" style="color:#000 !important;">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar" style="color:#000;"></span>
<span class="icon-bar" style="color:#000;"></span>
<span class="icon-bar" style="color:#000;"></span>
</button>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="/home" class="dropdown-toggle navbar-dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Home <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/help">help</a></li>
<li><a href="/help">contact</a></li>
</ul>
</li>
</ul>
<div class="navbar-right">
<div class="visible-xs">
<div class="row">
<div class="col-xs-6">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">2016</a>
<ul class="dropdown-menu dropdown-menu-right">
<li><a href="#" data-toggle="modal" data-target="_blank">Q1</a></li>
<li><a href="#" target="_blank">Q2</a></li>
<li><a href="#" target="_blank">Q3</a></li>
<li><a href="#" target="_blank">Q4</a></li>
</ul>
</div>
<div class="col-xs-6">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">2017</a>
<ul class="dropdown-menu dropdown-menu-right">
<li><a href="#" data-toggle="modal" data-target="_blank">Q1</a></li>
<li><a href="#" target="_blank">Q2</a></li>
<li><a href="#" target="_blank">Q3</a></li>
<li><a href="#" target="_blank">Q4</a></li>
</ul>
</div>
</div>
</div>
<div class="hidden-xs">
<ul class="nav navbar-nav">
<li>
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">2016</a>
<ul class="dropdown-menu dropdown-menu-right">
<li><a href="#" data-toggle="modal" data-target="_blank">Q1</a></li>
<li><a href="#" target="_blank">Q2</a></li>
<li><a href="#" target="_blank">Q3</a></li>
<li><a href="#" target="_blank">Q4</a></li>
</ul>
</li>
<li>
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">2017</a>
<ul class="dropdown-menu dropdown-menu-right">
<li><a href="#" data-toggle="modal" data-target="_blank">Q1</a></li>
<li><a href="#" target="_blank">Q2</a></li>
<li><a href="#" target="_blank">Q3</a></li>
<li><a href="#" target="_blank">Q4</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
</nav>
的下拉列表中选择一个选项或开始在其上键入时,它就不会#&}# 39; t选择选项或输入字母,除非我这样做两次。因此,如果我从列表中选择一个选项,它将保持空白,直到我再次选择它;如果我开始打字,我输入的第一个字母永远不会出现,只有那些来自第二个字母。
我不明白发生了什么,并且已经研究过,尽管没有成功。谢谢你的帮助!
答案 0 :(得分:1)
使用ComboBox1_AfterUpdate()
事件代替ComboBox1_Change()
事件
答案 1 :(得分:1)
在您的叙述中,您讲的是ComboBox1
和CombBox2
,而您的代码显示的是ComboBox1
和CombBox4
只是检查你是不是搞乱了
然后,也许您有一些ComboBox2_Change
(或ComboBox4_Change
)会改变您的代码ComboBox1
值,从而干扰ComboBox1_Change
顺便说一下,您可以为ComboBox1_Change
采用更短的代码,如下所示:
Private Sub ComboBox1_Change()
If ComboBox1 = "" Then
ComboBox4 = vbNullString: ComboBox4.Enabled = True
Else
ComboBox4.Enabled = False
Select Case ComboBox1.ListIndex
Case 0, 4
ComboBox4.Value = "Asia"
Case 1, 2, 5
ComboBox4.Value = "Europe"
Case 3
ComboBox4.Value = "MENA"
Case Else
ComboBox4.Enabled = True
End Select
End If
End Sub
您要将所有ComboBox4
次出现更改为ComboBox2