如果值单元格 = 组合框 - 在用户窗体组合框中显示 - VBA
with push bottom If value cell = Combobox - Show in userform combobox - VBA
我要完成的代码如下:
按"CommandButton1",如果单元格等于"ComboBox1"的值,则选中并显示在"ComboBox1"。
我的代码(不完整):
Private Sub UserForm_Initialize()
Dim xRg As Range
Set xRg = Worksheets("LookupLists").Range("A1:B5")
Me.ComboBox1.List = xRg.Columns(1).Value
End Sub
Private Sub CommandButton1_Click()
If Sheets("Sheet1").Range("A1") = "" Then
Beep
Else
If Sheets("Sheet1").Range("A1") = xRg Then
'What code should I write here to if
'"Sheets("Sheet1").Range("A1")=Worksheets("LookupLists").Range("A1:B5")" show the amount in comboBox1?
End If
End If
End Sub
如何匹配组合框中隐藏的第 2 列中的值并突出显示找到的项目
并不容易理解您真正想做的事情。
由于您指的是两列数据范围 A:B
但在组合框中仅显示第一个范围列(可能标识 名称或 ID ),我假设如下:
- After clicking the
CommandButton1
control and
- under the condition that a given cell value (
A1
) equals the 2nd value column of ComboBox1
(possibly hidden),
- the corresponding
ComboBox1
item (displaying e.g. names) should be selected (highlighted).
应用方法
CommandButton1_Click
事件过程中的基本代码行是将可能的 匹配 位置分配给变量 pos
:
pos = Application.Match(myVal, Application.Index(Me.ComboBox1.List, 0, 2), 0)
因为 ►Application.Match
需要一个 一维 数组,而 ComboBox1.List
是 2 -dimensional,Application.Index
函数必须从组合列表中提取 2nd(隐藏)组合框列(包含值),从而获得一维数组现在作为论据。
...Application.Index(Me.ComboBox1.List, 0, 2)
' c.f。注意 *)
- ) 注意上面
Application.Index
函数中的行索引必须设置为0
,这样列索引►2
只能隔离第二列。
如果找到 匹配 位置,变量 pos
接收数字项目编号,可用于 select 通过重置找到的项目组合框 .ListIndex
(减去 1,因为框索引从零开始)。
如果在组合框中没有找到相应的值,pos
会返回一个错误,因此有必要通过检查这个变量来避免错误消息►IsError()
函数(这就是为什么 pos
必须声明为 Variant 而不仅仅是 Long 以避免类型不匹配错误。)
用户表单代码
Option Explicit
Private Sub UserForm_Initialize()
Dim xrg As Range
Set xrg = ThisWorkbook.Worksheets("LookupLists").Range("A1:B5")
With Me.ComboBox1
.List = xrg.Value2
.ColumnCount = 2 ' << provide for data of both columns
.ColumnWidths = .Width ' << show only first column in full box width
End With
End Sub
Private Sub CommandButton1_Click()
Dim pos As Variant ' item position has to be variant to allow IsError()
Dim myVal
myVal = ThisWorkbook.Worksheets("Sheet1").Range("A1")
If myVal = "" Then
Beep
Else ' try to get a match and its position
pos = Application.Match(myVal, Application.Index(Me.ComboBox1.List, 0, 2), 0)
If IsError(pos) Then pos = 0 ' avoid error msg if not found :-)
Me.ComboBox1.ListIndex = pos - 1 ' goto item no: index is zero based!
End If
End Sub
我要完成的代码如下: 按"CommandButton1",如果单元格等于"ComboBox1"的值,则选中并显示在"ComboBox1"。
我的代码(不完整):
Private Sub UserForm_Initialize()
Dim xRg As Range
Set xRg = Worksheets("LookupLists").Range("A1:B5")
Me.ComboBox1.List = xRg.Columns(1).Value
End Sub
Private Sub CommandButton1_Click()
If Sheets("Sheet1").Range("A1") = "" Then
Beep
Else
If Sheets("Sheet1").Range("A1") = xRg Then
'What code should I write here to if
'"Sheets("Sheet1").Range("A1")=Worksheets("LookupLists").Range("A1:B5")" show the amount in comboBox1?
End If
End If
End Sub
如何匹配组合框中隐藏的第 2 列中的值并突出显示找到的项目
并不容易理解您真正想做的事情。
由于您指的是两列数据范围 A:B
但在组合框中仅显示第一个范围列(可能标识 名称或 ID ),我假设如下:
- After clicking the
CommandButton1
control and- under the condition that a given cell value (
A1
) equals the 2nd value column ofComboBox1
(possibly hidden),- the corresponding
ComboBox1
item (displaying e.g. names) should be selected (highlighted).
应用方法
CommandButton1_Click
事件过程中的基本代码行是将可能的 匹配 位置分配给变量 pos
:
pos = Application.Match(myVal, Application.Index(Me.ComboBox1.List, 0, 2), 0)
因为 ►Application.Match
需要一个 一维 数组,而 ComboBox1.List
是 2 -dimensional,Application.Index
函数必须从组合列表中提取 2nd(隐藏)组合框列(包含值),从而获得一维数组现在作为论据。
...Application.Index(Me.ComboBox1.List, 0, 2)
' c.f。注意 *)
- ) 注意上面
Application.Index
函数中的行索引必须设置为0
,这样列索引►2
只能隔离第二列。
如果找到 匹配 位置,变量 pos
接收数字项目编号,可用于 select 通过重置找到的项目组合框 .ListIndex
(减去 1,因为框索引从零开始)。
如果在组合框中没有找到相应的值,pos
会返回一个错误,因此有必要通过检查这个变量来避免错误消息►IsError()
函数(这就是为什么 pos
必须声明为 Variant 而不仅仅是 Long 以避免类型不匹配错误。)
用户表单代码
Option Explicit
Private Sub UserForm_Initialize()
Dim xrg As Range
Set xrg = ThisWorkbook.Worksheets("LookupLists").Range("A1:B5")
With Me.ComboBox1
.List = xrg.Value2
.ColumnCount = 2 ' << provide for data of both columns
.ColumnWidths = .Width ' << show only first column in full box width
End With
End Sub
Private Sub CommandButton1_Click()
Dim pos As Variant ' item position has to be variant to allow IsError()
Dim myVal
myVal = ThisWorkbook.Worksheets("Sheet1").Range("A1")
If myVal = "" Then
Beep
Else ' try to get a match and its position
pos = Application.Match(myVal, Application.Index(Me.ComboBox1.List, 0, 2), 0)
If IsError(pos) Then pos = 0 ' avoid error msg if not found :-)
Me.ComboBox1.ListIndex = pos - 1 ' goto item no: index is zero based!
End If
End Sub