vba 使用外部数据的两列组合框
vba two columns combobox using external data
我最近创建了一个工作簿,其中包含一个带有组合框的用户窗体,名为 "combobox1"
我有一个代码可以按照下面所示的方向从其他工作簿中获取数据 "B2:B...."
现在我想让它如何在组合框中有一个拖列,另一列应该从同一目录中获取数据,但 exp 的范围:"A1:A...."
我需要你的帮助
谢谢
[Private Sub UserForm_Initialize()
`Dim ListItems As Variant, i As Integer
`Dim SourceWB As Workbook
With Me.ComboBox1
.Clear ' remove existing entries from the listbox
' turn screen updating off,
' prevent the user from seeing the source workbook being opened
Application.ScreenUpdating = False
' open the source workbook as ReadOnly
Set SourceWB = Workbooks.Open("C:\Users\Mohsen\Desktop\new prj\Data base\partlist.xls", _
False, True)
ListItems = SourceWB.Worksheets(1).Range("B2:B1468").Value
' get the values you want
SourceWB.Close False ' close the source workbook without saving changes
Set SourceWB = Nothing
Application.ScreenUpdating = True
ListItems = Application.WorksheetFunction.Transpose(ListItems)
' convert values to a vertical array
For i = 1 To UBound(ListItems)
.AddItem ListItems(i) ' populate the listbox
Next i
.ListIndex = -1 ' no items selected, set to 0 to select the first item
End With
End Sub
您的问题不清楚第二列数据的来源,因此我假设第一个组合框列来自 SourceWB、Sheet1、B 列,第二个组合框列来自同一个 sheet 在 B 列左侧的列中。您可以更改这些以适合。
我还编写了代码来识别 B 列中的最后一个数据行。这将防止不必要地搜索 1468 行。再一次,如果这没有帮助,请更改。
Option Explicit
Private Sub UserForm_Initialize()
Dim ListItems As Variant
Dim i As Integer
Dim SourceWB As Workbook
Dim listVal As Range
Dim srcLastRow As Long
'for testing purposes
Dim srcName As String
srcName = "C:\Users\Mohsen\Desktop\new prj\Data base\partlist.xls"
With Me.ComboBox1
'Set the number of columns by code
.ColumnCount = 2
.Clear
Application.ScreenUpdating = False
Set SourceWB = Workbooks.Open(srcName, False, True)
'find the last row of data to prevent searching 1468 rows unnecessarily
srcLastRow = SourceWB.Sheets(1).Cells(Rows.Count, "B").End(xlUp).Row
For Each listVal In SourceWB.Sheets(1).Range("B2:B" & srcLastRow)
.AddItem listVal.Value
'Offset(0,-1) gets second column of data from cell to the left
.List(.ListCount - 1, 1) = listVal.Offset(0, -1).Value
Next listVal
SourceWB.Close False
Set SourceWB = Nothing
Application.ScreenUpdating = True
.ListIndex = -1
End With
End Sub
查看 Combobox1 的属性 window,了解您可能需要在代码中设置的其他属性。
我最近创建了一个工作簿,其中包含一个带有组合框的用户窗体,名为 "combobox1" 我有一个代码可以按照下面所示的方向从其他工作簿中获取数据 "B2:B...." 现在我想让它如何在组合框中有一个拖列,另一列应该从同一目录中获取数据,但 exp 的范围:"A1:A...." 我需要你的帮助 谢谢
[Private Sub UserForm_Initialize()
`Dim ListItems As Variant, i As Integer
`Dim SourceWB As Workbook
With Me.ComboBox1
.Clear ' remove existing entries from the listbox
' turn screen updating off,
' prevent the user from seeing the source workbook being opened
Application.ScreenUpdating = False
' open the source workbook as ReadOnly
Set SourceWB = Workbooks.Open("C:\Users\Mohsen\Desktop\new prj\Data base\partlist.xls", _
False, True)
ListItems = SourceWB.Worksheets(1).Range("B2:B1468").Value
' get the values you want
SourceWB.Close False ' close the source workbook without saving changes
Set SourceWB = Nothing
Application.ScreenUpdating = True
ListItems = Application.WorksheetFunction.Transpose(ListItems)
' convert values to a vertical array
For i = 1 To UBound(ListItems)
.AddItem ListItems(i) ' populate the listbox
Next i
.ListIndex = -1 ' no items selected, set to 0 to select the first item
End With
End Sub
您的问题不清楚第二列数据的来源,因此我假设第一个组合框列来自 SourceWB、Sheet1、B 列,第二个组合框列来自同一个 sheet 在 B 列左侧的列中。您可以更改这些以适合。
我还编写了代码来识别 B 列中的最后一个数据行。这将防止不必要地搜索 1468 行。再一次,如果这没有帮助,请更改。
Option Explicit
Private Sub UserForm_Initialize()
Dim ListItems As Variant
Dim i As Integer
Dim SourceWB As Workbook
Dim listVal As Range
Dim srcLastRow As Long
'for testing purposes
Dim srcName As String
srcName = "C:\Users\Mohsen\Desktop\new prj\Data base\partlist.xls"
With Me.ComboBox1
'Set the number of columns by code
.ColumnCount = 2
.Clear
Application.ScreenUpdating = False
Set SourceWB = Workbooks.Open(srcName, False, True)
'find the last row of data to prevent searching 1468 rows unnecessarily
srcLastRow = SourceWB.Sheets(1).Cells(Rows.Count, "B").End(xlUp).Row
For Each listVal In SourceWB.Sheets(1).Range("B2:B" & srcLastRow)
.AddItem listVal.Value
'Offset(0,-1) gets second column of data from cell to the left
.List(.ListCount - 1, 1) = listVal.Offset(0, -1).Value
Next listVal
SourceWB.Close False
Set SourceWB = Nothing
Application.ScreenUpdating = True
.ListIndex = -1
End With
End Sub
查看 Combobox1 的属性 window,了解您可能需要在代码中设置的其他属性。