如何填充组合框
How to Populate a Combobox
我是 VBA 的新手,我一直在努力填充组合框。
我正在尝试用电子表格中第一列的内容填充组合框,以便我可以根据组合框选择删除关联的数据行。
在提出这个问题时,我已经查看了这里和其他地方的几个问题,但我没有找到任何有效的方法。
- populate combobox in VBA with array elements
- How do I populate a combo box from a column in my excel spread sheet?
- http://www.techrepublic.com/blog/microsoft-office/populate-a-userform-combo-box-in-excel-with-a-dynamic-list/
下面是我试过的代码。我有些迷茫,因为我一直试图拼凑其他问题的不同答案以使它起作用,但无济于事。我希望组合框用第 1 列的值填充,但它仍然是空白的。
尝试 #1 这涉及创建动态范围:
=OFFSET(PC_DataSheet!$A,0,0, COUNTA(PC_DataSheet!$A:$A536)-1,1)
Private Sub UserForm1_Initialize()
Dim rngPCNumber As Range
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
For Each rngPCNumber In ws.Range("PCNumber")
Me.PC_ListComboBox.AddItem rngPCNumber.Value
Next rngPCNumber
End Sub
尝试 #2
Private Sub UserForm1_Initialize()
Dim arr() As Variant
arr = Worksheets("Sheet1").Range("C2:" & lrow).Value
PC_ListComboBox.List = arr
End Sub
尝试 #3
Private Sub UserForm1_Initialize()
Dim vArr As Variant
Dim i As Integer
vArr = Sheet1.Range("A:1").Value
With PC_ListComboBox.Clear
For i = LBound(vArr) To UBound(vArr)
.AddItem vArr(i)
Next i
End With
End Sub
如有任何帮助,我们将不胜感激!
编辑: 我试过将 Gary 的学生 建议的代码插入我的 UserForm_Initialize() Sub,但是当我尝试打开用户窗体时,出现以下错误消息:
Run-time error '9': Subscript out of range
当我点击调试时,它突出显示了这段代码:
'Opens PC UserForm when pressed.
Private Sub AddPCButton_Click()
UserForm.Show 'This line is the line highlighted by the debugger.
End Sub
我不知道是什么原因造成的...当我使用建议的代码时,我收到一条错误消息,但当我删除代码时,用户窗体功能完美无缺。
这是包含和不包含建议代码的 Private Sub UserForm_Initialize()。
'Clears and Initializes the form when first loaded.
Private Sub UserForm_Initialize()
'Empties combo boxes.
PC_OSTypeComboBox = ""
PC_HDTypeComboBox = ""
'Populates combo boxes.
With PC_OSTypeComboBox
.Clear
.AddItem "Windows 8"
.AddItem "Windows 7"
.AddItem "Windows Vista"
.AddItem "Windows XP"
.AddItem "Windows 2000"
.AddItem "Windows 98"
.AddItem "Windows NT"
.AddItem "Windows 95"
End With
With PC_HDTypeComboBox
.Clear
.AddItem "SATA"
.AddItem "IDE"
.AddItem "SCSI"
End With
End Sub
这包括建议的代码:
'Clears and Initializes the form when first loaded.
Private Sub UserForm_Initialize()
Dim N As Long, i As Long
With Sheets("Sheet1")
N = .Cells(Rows.Count, 1).End(xlUp).Row
End With
With PC_NumberComboBox
.Clear
For i = 1 To N
.AddItem Sheets("Sheet1").Cells(i, 1).Value
Next i
End With
'Empties combo boxes.
PC_OSTypeComboBox = ""
PC_HDTypeComboBox = ""
'Populates combo boxes.
With PC_OSTypeComboBox
.Clear
.AddItem "Windows 8"
.AddItem "Windows 7"
.AddItem "Windows Vista"
.AddItem "Windows XP"
.AddItem "Windows 2000"
.AddItem "Windows 98"
.AddItem "Windows NT"
.AddItem "Windows 95"
End With
With PC_HDTypeComboBox
.Clear
.AddItem "SATA"
.AddItem "IDE"
.AddItem "SCSI"
End With
End Sub
这是创建和填充 Forms 样式组合框的超级简单示例:
Sub FormsStyleComboBox()
ActiveSheet.DropDowns.Add(411, 14.25, 124.5, 188.25).Select
N = Cells(Rows.Count, "A").End(xlUp).Row
strng = Range("A1:A" & N).Address
Selection.ListFillRange = strng
End Sub
例如:
编辑#1
我创建了一个名为 Demo 的用户窗体,其中包含一个名为 MyBox
的组合框
在标准模块中我输入:
Sub DisplayIt()
Demo.Show
End Sub
并且在UserForm代码区:
Private Sub UserForm_Initialize()
Dim N As Long, i As Long
With Sheets("Sheet1")
N = .Cells(Rows.Count, 1).End(xlUp).Row
End With
With MyBox
.Clear
For i = 1 To N
.AddItem Sheets("Sheet1").Cells(i, 1).Value
Next i
End With
End Sub
运行 DisplayIt() 产生:
这是基于this tutorial
所以我尝试了 Gary 的学生建议的解决方案,当我使用他提供的代码创建一个新工作簿时,该解决方案有效,但由于某种原因,'subscript out of range' 错误仍然存在当我在我的项目中实现它时出现,无论我做了什么来重命名工作簿中的工作 sheets,包括设置一个 sub 来列出所有工作sheets 并调用 sheet 从那里开始。
我选择使用输入框而不是组合框,这样编码起来会更直接一些。以下是任何好奇的代码。
Private Sub DeletePCButton_Click()
'Assigns variables for delete sequence.
Dim PCNumberEntry As String
Dim Confirm As Integer
Dim r As Range
Dim c As Range
Dim cellsToDelete As Range
Dim m As Integer
'Asks for PC entry to be deleted.
PCNumberEntry = InputBox("Enter the number of the PC you wish to remove:", "Delete PC Entry", "PC 1", vbOKCancel)
'Closes inputbox when cancel is pressed.
If PCNumberEntry = "" Then
Exit Sub
End If
'Searches worksheet column "A" and finds any existing PC entries.
Set r = Sheet1.Range("A:A")
For Each c In r
'Checks for matching entry in worksheet to begin delete sequence.
If (c.Value) = PCNumberEntry Then
m = True
'Asks for confirmation from user before deleting entry.
Confirm = MsgBox("Warning! Once deleted, an entry cannot be restored! Only proceed if you are sure you wish to delete a row.", vbYesNo Or vbExclamation)
'Cancels delete operation when "No" button is pressed.
If Confirm = vbNo Then
Exit Sub
End If
'Deletes entry and informs user of successful operation.
If cellsToDelete Is Nothing Then
Set cellsToDelete = c
Call cellsToDelete.EntireRow.delete
MsgBox ("The entry was deleted.")
End If
End If
Next c
'Displays error message if no matching entry is found.
If m = False Then
MsgBox ("No entry with that number was found!")
End If
On Error Resume Next
结束子
Private Sub UserForm_Initialize()
Dim CS As Integer
Dim CR As Integer
Dim RF As Integer
Dim PW As Integer
Dim CD As Integer
CS = ActiveWorkbook.Sheets("LISTS").Columns(2).End(xlDown).Row
CR = ActiveWorkbook.Sheets("LISTS").Columns(3).End(xlDown).Row
RF = ActiveWorkbook.Sheets("LISTS").Columns(4).End(xlDown).Row
PW = ActiveWorkbook.Sheets("LISTS").Columns(5).End(xlDown).Row
CD = ActiveWorkbook.Sheets("LISTS").Columns(6).End(xlDown).Row
With CB_CS
.Clear
For i = 2 To CS + 1
.AddItem ActiveWorkbook.Sheets("LISTS").Cells(i, 2).Value
Next i
End With
With CB_CR
.Clear
For i = 2 To CR + 1
.AddItem ActiveWorkbook.Sheets("LISTS").Cells(i, 3).Value
Next i
End With
With CB_RF
.Clear
For i = 2 To RF + 1
.AddItem ActiveWorkbook.Sheets("LISTS").Cells(i, 4).Value
Next i
End With
With CB_PW
.Clear
For i = 2 To PW + 1
.AddItem ActiveWorkbook.Sheets("LISTS").Cells(i, 5).Value
Next i
End With
With CB_CD
.Clear
For i = 2 To CD + 1
.AddItem ActiveWorkbook.Sheets("LISTS").Cells(i, 6).Value
Next i
End With
End Sub
以上代码位于我的用户窗体代码中(Right-Click 在用户窗体上,然后单击 'view code')
我创建了一个工作sheet 调用列表。 sheet 上的每一列对应不同的 combo-box。一旦我填写完它并让代码工作,我就隐藏了列表工作sheet。
我命名的每个 ComboBox CB_XX 所以请在代码中记下这些名称
我通过定义列表的长度来开始编写代码(请注意,如果列表中只有一项,则此操作会失败,但如果只有一项,则不要使用组合框)
获得长度后,我将正确的列添加到正确的组合框中。注意每个 for/next 循环中的 +1。即在每个列表的末尾添加一个空白,让用户清空选择。如果您不想留空,请删除 +1。我从 i = 2 开始,不在我的列表 sheet.
上显示 header 行
我是 VBA 的新手,我一直在努力填充组合框。 我正在尝试用电子表格中第一列的内容填充组合框,以便我可以根据组合框选择删除关联的数据行。
在提出这个问题时,我已经查看了这里和其他地方的几个问题,但我没有找到任何有效的方法。
- populate combobox in VBA with array elements
- How do I populate a combo box from a column in my excel spread sheet?
- http://www.techrepublic.com/blog/microsoft-office/populate-a-userform-combo-box-in-excel-with-a-dynamic-list/
下面是我试过的代码。我有些迷茫,因为我一直试图拼凑其他问题的不同答案以使它起作用,但无济于事。我希望组合框用第 1 列的值填充,但它仍然是空白的。
尝试 #1 这涉及创建动态范围:
=OFFSET(PC_DataSheet!$A,0,0, COUNTA(PC_DataSheet!$A:$A536)-1,1)
Private Sub UserForm1_Initialize()
Dim rngPCNumber As Range
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
For Each rngPCNumber In ws.Range("PCNumber")
Me.PC_ListComboBox.AddItem rngPCNumber.Value
Next rngPCNumber
End Sub
尝试 #2
Private Sub UserForm1_Initialize()
Dim arr() As Variant
arr = Worksheets("Sheet1").Range("C2:" & lrow).Value
PC_ListComboBox.List = arr
End Sub
尝试 #3
Private Sub UserForm1_Initialize()
Dim vArr As Variant
Dim i As Integer
vArr = Sheet1.Range("A:1").Value
With PC_ListComboBox.Clear
For i = LBound(vArr) To UBound(vArr)
.AddItem vArr(i)
Next i
End With
End Sub
如有任何帮助,我们将不胜感激!
编辑: 我试过将 Gary 的学生 建议的代码插入我的 UserForm_Initialize() Sub,但是当我尝试打开用户窗体时,出现以下错误消息:
Run-time error '9': Subscript out of range
当我点击调试时,它突出显示了这段代码:
'Opens PC UserForm when pressed.
Private Sub AddPCButton_Click()
UserForm.Show 'This line is the line highlighted by the debugger.
End Sub
我不知道是什么原因造成的...当我使用建议的代码时,我收到一条错误消息,但当我删除代码时,用户窗体功能完美无缺。 这是包含和不包含建议代码的 Private Sub UserForm_Initialize()。
'Clears and Initializes the form when first loaded.
Private Sub UserForm_Initialize()
'Empties combo boxes.
PC_OSTypeComboBox = ""
PC_HDTypeComboBox = ""
'Populates combo boxes.
With PC_OSTypeComboBox
.Clear
.AddItem "Windows 8"
.AddItem "Windows 7"
.AddItem "Windows Vista"
.AddItem "Windows XP"
.AddItem "Windows 2000"
.AddItem "Windows 98"
.AddItem "Windows NT"
.AddItem "Windows 95"
End With
With PC_HDTypeComboBox
.Clear
.AddItem "SATA"
.AddItem "IDE"
.AddItem "SCSI"
End With
End Sub
这包括建议的代码:
'Clears and Initializes the form when first loaded.
Private Sub UserForm_Initialize()
Dim N As Long, i As Long
With Sheets("Sheet1")
N = .Cells(Rows.Count, 1).End(xlUp).Row
End With
With PC_NumberComboBox
.Clear
For i = 1 To N
.AddItem Sheets("Sheet1").Cells(i, 1).Value
Next i
End With
'Empties combo boxes.
PC_OSTypeComboBox = ""
PC_HDTypeComboBox = ""
'Populates combo boxes.
With PC_OSTypeComboBox
.Clear
.AddItem "Windows 8"
.AddItem "Windows 7"
.AddItem "Windows Vista"
.AddItem "Windows XP"
.AddItem "Windows 2000"
.AddItem "Windows 98"
.AddItem "Windows NT"
.AddItem "Windows 95"
End With
With PC_HDTypeComboBox
.Clear
.AddItem "SATA"
.AddItem "IDE"
.AddItem "SCSI"
End With
End Sub
这是创建和填充 Forms 样式组合框的超级简单示例:
Sub FormsStyleComboBox()
ActiveSheet.DropDowns.Add(411, 14.25, 124.5, 188.25).Select
N = Cells(Rows.Count, "A").End(xlUp).Row
strng = Range("A1:A" & N).Address
Selection.ListFillRange = strng
End Sub
例如:
编辑#1
我创建了一个名为 Demo 的用户窗体,其中包含一个名为 MyBox
的组合框在标准模块中我输入:
Sub DisplayIt()
Demo.Show
End Sub
并且在UserForm代码区:
Private Sub UserForm_Initialize()
Dim N As Long, i As Long
With Sheets("Sheet1")
N = .Cells(Rows.Count, 1).End(xlUp).Row
End With
With MyBox
.Clear
For i = 1 To N
.AddItem Sheets("Sheet1").Cells(i, 1).Value
Next i
End With
End Sub
运行 DisplayIt() 产生:
这是基于this tutorial
所以我尝试了 Gary 的学生建议的解决方案,当我使用他提供的代码创建一个新工作簿时,该解决方案有效,但由于某种原因,'subscript out of range' 错误仍然存在当我在我的项目中实现它时出现,无论我做了什么来重命名工作簿中的工作 sheets,包括设置一个 sub 来列出所有工作sheets 并调用 sheet 从那里开始。
我选择使用输入框而不是组合框,这样编码起来会更直接一些。以下是任何好奇的代码。
Private Sub DeletePCButton_Click()
'Assigns variables for delete sequence.
Dim PCNumberEntry As String
Dim Confirm As Integer
Dim r As Range
Dim c As Range
Dim cellsToDelete As Range
Dim m As Integer
'Asks for PC entry to be deleted.
PCNumberEntry = InputBox("Enter the number of the PC you wish to remove:", "Delete PC Entry", "PC 1", vbOKCancel)
'Closes inputbox when cancel is pressed.
If PCNumberEntry = "" Then
Exit Sub
End If
'Searches worksheet column "A" and finds any existing PC entries.
Set r = Sheet1.Range("A:A")
For Each c In r
'Checks for matching entry in worksheet to begin delete sequence.
If (c.Value) = PCNumberEntry Then
m = True
'Asks for confirmation from user before deleting entry.
Confirm = MsgBox("Warning! Once deleted, an entry cannot be restored! Only proceed if you are sure you wish to delete a row.", vbYesNo Or vbExclamation)
'Cancels delete operation when "No" button is pressed.
If Confirm = vbNo Then
Exit Sub
End If
'Deletes entry and informs user of successful operation.
If cellsToDelete Is Nothing Then
Set cellsToDelete = c
Call cellsToDelete.EntireRow.delete
MsgBox ("The entry was deleted.")
End If
End If
Next c
'Displays error message if no matching entry is found.
If m = False Then
MsgBox ("No entry with that number was found!")
End If
On Error Resume Next
结束子
Private Sub UserForm_Initialize()
Dim CS As Integer
Dim CR As Integer
Dim RF As Integer
Dim PW As Integer
Dim CD As Integer
CS = ActiveWorkbook.Sheets("LISTS").Columns(2).End(xlDown).Row
CR = ActiveWorkbook.Sheets("LISTS").Columns(3).End(xlDown).Row
RF = ActiveWorkbook.Sheets("LISTS").Columns(4).End(xlDown).Row
PW = ActiveWorkbook.Sheets("LISTS").Columns(5).End(xlDown).Row
CD = ActiveWorkbook.Sheets("LISTS").Columns(6).End(xlDown).Row
With CB_CS
.Clear
For i = 2 To CS + 1
.AddItem ActiveWorkbook.Sheets("LISTS").Cells(i, 2).Value
Next i
End With
With CB_CR
.Clear
For i = 2 To CR + 1
.AddItem ActiveWorkbook.Sheets("LISTS").Cells(i, 3).Value
Next i
End With
With CB_RF
.Clear
For i = 2 To RF + 1
.AddItem ActiveWorkbook.Sheets("LISTS").Cells(i, 4).Value
Next i
End With
With CB_PW
.Clear
For i = 2 To PW + 1
.AddItem ActiveWorkbook.Sheets("LISTS").Cells(i, 5).Value
Next i
End With
With CB_CD
.Clear
For i = 2 To CD + 1
.AddItem ActiveWorkbook.Sheets("LISTS").Cells(i, 6).Value
Next i
End With
End Sub
以上代码位于我的用户窗体代码中(Right-Click 在用户窗体上,然后单击 'view code')
我创建了一个工作sheet 调用列表。 sheet 上的每一列对应不同的 combo-box。一旦我填写完它并让代码工作,我就隐藏了列表工作sheet。
我命名的每个 ComboBox CB_XX 所以请在代码中记下这些名称
我通过定义列表的长度来开始编写代码(请注意,如果列表中只有一项,则此操作会失败,但如果只有一项,则不要使用组合框)
获得长度后,我将正确的列添加到正确的组合框中。注意每个 for/next 循环中的 +1。即在每个列表的末尾添加一个空白,让用户清空选择。如果您不想留空,请删除 +1。我从 i = 2 开始,不在我的列表 sheet.
上显示 header 行