I\'m quite new to VBA and I\'ve been struggling with populating a combobox. I\'m trying to fill a combobox with the contents of the first column in a spreadsheet so I can de
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
The code above is located in my UserForm Code (Right-Click on UserForm, then click 'view code')
I created a Worksheet call LISTS. Each column on that sheet is for a different combo-box. Once I filled it out and got the code working I hid the LISTS worksheet.
Each ComboBox I named CB_XX so note those names in the code
I start the code by defining the length of the lists (note this fails if you only have one item in the list but if you only have one item don't use a combo box)
Once I get the lengths I add the correct columns to the correct comboboxes. Note the +1 in each for/next loop. That is to add a blank at the end of each list to allow the user to empty the selection. Remove the +1 if you don't want that blank. I start at i = 2 to not show the header row on my LISTS sheet.
Here is a super simple example of creating and filling a Forms style combo-box:
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
For example:
EDIT#1
I created a UserForm called Demo containing a combo-box called MyBox
In a Standard Module I put:
Sub DisplayIt()
Demo.Show
End Sub
and in the UserForm code area:
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
Running DisplayIt() produces:
This is based on this tutorial
So I tried the solution Gary's Student suggested, which worked when I created a new workbook with the code he provided, but for some reason the 'subscript out of range' error kept coming up when I implemented it in my project, no matter what I did to rename my worksheets in the workbook, including setting up a sub to list all the worksheets and call the sheet from there.
I opted instead to use an inputbox instead of a combobox, which ended up being a little more straightforward to code. Below is the code for anyone curious.
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
End Sub