填充具有动态水平范围的组合框
Populating combobox with dynamic horizontal range
我需要 VBA 代码来查找“A”列中的值并在用户窗体 combox1 的右侧列出相邻的水平非空白单元格。
示例数据:
+-----+----+----+----+---+
| A | B | C | D | E |
+-----+----+----+----+---+
| A1 | 63 | | 55 | 5 |
+-----+----+----+----+---+
须藤代码:
Sub test()
myVal = “A1”
Findme = myVal
Set match = Sheets(“Sheets1”).Range(A:A).Find(Findme)
myRange = foundRange
Userform1.Combobox.value = myRange
Exit Sub
在上面的示例代码中,foundRange 是找到值的行加上“B”到“E”列,减去任何空格。
组合框值:
63 55 56
谢谢!
创建名为 Userform1 的用户窗体,名为 ComboBox 的组合框和名为 CommandButton1 的按钮。如下图所示:
然后在 commandButton 上使用此代码来填充 ComboBox:
Private Sub CommandButton1_Click()
Dim ws1 As Worksheet
Dim i As Long, c As Long
Dim rng As Range, rng2 As Range
Dim cellFound As Range
Dim lastrow As Long, lastcolumn As Long
Set ws1 = ThisWorkbook.Sheets(1)
Findme = "A1"
lastrow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
Set rng = ws1.Range("A:A") 'range to search
Set rng2 = rng(lastrow, 1)
With rng
Set cellFound = .Find(what:=Findme, After:=rng2, LookIn:=xlValues)
If Not cellFound Is Nothing Then
FirstAddress = cellFound.Address
Do
lastcolumn = ws1.Cells(ws1.Range(cellFound.Address).Row, ws1.Columns.Count).End(xlToLeft).Column
For i = Range(cellFound.Address).Column + 1 To lastcolumn
If ws1.Cells(Range(cellFound.Address).Row, i) <> "" Then ComboBox.AddItem ws1.Cells(Range(cellFound.Address).Row, i).Value
Next i
Set cellFound = .FindNext(cellFound)
Loop While Not cellFound Is Nothing And cellFound.Address <> FirstAddress
End If
End With
End Sub
此代码使用了您想用来在 ComboBox ComboBox.AddItem
上添加项目的 .Find
函数。请注意,代码在用户窗体中,否则需要进行一些更改才能将其变为全局。
找到 A1 后,您使用其地址行获取最后一列。并通过 CellFound + 1Column(样本上的 2)循环到最后一列,比较值是否不同于空白。如果不同,则添加到 ComboBox。
lastcolumn = ws1.Cells(ws1.Range(cellFound.Address).Row, ws1.Columns.Count).End(xlToLeft).Column
For i = Range(cellFound.Address).Column + 1 To lastcolumn
If ws1.Cells(Range(cellFound.Address).Row, i) <> "" Then ComboBox.AddItem ws1.Cells(Range(cellFound.Address).Row, i).Value
Next i
然后找到下一个值并做同样的事情,所以A列可以有多个匹配项。
Set cellFound = .FindNext(cellFound)
FindMe
值可以替换为任何值,例如 TextBoxes 或 Cell.Values。
而要搜索的范围rng
可以设置为您正在使用的范围。例如:您的整个工作表
我需要 VBA 代码来查找“A”列中的值并在用户窗体 combox1 的右侧列出相邻的水平非空白单元格。
示例数据:
+-----+----+----+----+---+
| A | B | C | D | E |
+-----+----+----+----+---+
| A1 | 63 | | 55 | 5 |
+-----+----+----+----+---+
须藤代码:
Sub test()
myVal = “A1”
Findme = myVal
Set match = Sheets(“Sheets1”).Range(A:A).Find(Findme)
myRange = foundRange
Userform1.Combobox.value = myRange
Exit Sub
在上面的示例代码中,foundRange 是找到值的行加上“B”到“E”列,减去任何空格。
组合框值:
63 55 56
谢谢!
创建名为 Userform1 的用户窗体,名为 ComboBox 的组合框和名为 CommandButton1 的按钮。如下图所示:
然后在 commandButton 上使用此代码来填充 ComboBox:
Private Sub CommandButton1_Click()
Dim ws1 As Worksheet
Dim i As Long, c As Long
Dim rng As Range, rng2 As Range
Dim cellFound As Range
Dim lastrow As Long, lastcolumn As Long
Set ws1 = ThisWorkbook.Sheets(1)
Findme = "A1"
lastrow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
Set rng = ws1.Range("A:A") 'range to search
Set rng2 = rng(lastrow, 1)
With rng
Set cellFound = .Find(what:=Findme, After:=rng2, LookIn:=xlValues)
If Not cellFound Is Nothing Then
FirstAddress = cellFound.Address
Do
lastcolumn = ws1.Cells(ws1.Range(cellFound.Address).Row, ws1.Columns.Count).End(xlToLeft).Column
For i = Range(cellFound.Address).Column + 1 To lastcolumn
If ws1.Cells(Range(cellFound.Address).Row, i) <> "" Then ComboBox.AddItem ws1.Cells(Range(cellFound.Address).Row, i).Value
Next i
Set cellFound = .FindNext(cellFound)
Loop While Not cellFound Is Nothing And cellFound.Address <> FirstAddress
End If
End With
End Sub
此代码使用了您想用来在 ComboBox ComboBox.AddItem
上添加项目的 .Find
函数。请注意,代码在用户窗体中,否则需要进行一些更改才能将其变为全局。
找到 A1 后,您使用其地址行获取最后一列。并通过 CellFound + 1Column(样本上的 2)循环到最后一列,比较值是否不同于空白。如果不同,则添加到 ComboBox。
lastcolumn = ws1.Cells(ws1.Range(cellFound.Address).Row, ws1.Columns.Count).End(xlToLeft).Column
For i = Range(cellFound.Address).Column + 1 To lastcolumn
If ws1.Cells(Range(cellFound.Address).Row, i) <> "" Then ComboBox.AddItem ws1.Cells(Range(cellFound.Address).Row, i).Value
Next i
然后找到下一个值并做同样的事情,所以A列可以有多个匹配项。
Set cellFound = .FindNext(cellFound)
FindMe
值可以替换为任何值,例如 TextBoxes 或 Cell.Values。
而要搜索的范围rng
可以设置为您正在使用的范围。例如:您的整个工作表