Need Help Creating First VBA ComboBox...

Lidsavr

Active Member
Joined
Jan 10, 2008
Messages
330
I am running Excel 2007 and writing code that will be running on other computers with Excel 2003.

I am writing my first code for a userform Combobox. I borrowed some code from one of Norie's posts and am attempting to modify for my use (code below).

The ComboBox will use a range of cells that I have copied, pasted into a separate spreadsheet and removed any duplicates. The range may change based on the version of the assembly. I am attempting to get the UserForm to populate the dropdown list and keep getting errors. Here is the code with the line in red where I am currently stuck.

Code:
Option Explicit
Private Sub ComboBox1_Change()
Dim ws As Worksheet
Dim rng As Range
Dim i As Long
Dim ComboBox1 As ComboBox
Dim ListBox1 As ListBox
    ' check item has been selected
    If ComboBox1.ListIndex <> -1 Then
        ListBox1.Clear
        Set ws = Worksheets("PartNums")
        Set rng = ws.Range("A2:A66")
        While rng.Value <> ""
[COLOR="Red"]            ListBox1.AddItem (ws.Name)[/COLOR]
            ListBox1.List(ListBox1.ListCount - 1, 1) = rng.Value
            For i = 2 To 6
                ListBox1.List(ListBox1.ListCount - 1, 2) = rng.Offset(, i + 5).Value
            Next i
             Set rng = rng.Offset(1)
         Wend
'    End If
End Sub

Private Sub PNs_for_ARCT00232()
Dim j As Long
Dim ce1 As String
    Sheets("ARCT00232").Select
    Range("D:D").Select
    Selection.Copy
    Sheets("PartNums").Select
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.DisplayAlerts = False
    Cells.Select
    Selection.Columns.AutoFit
    Columns("A:A").Select
    Selection.RemoveDuplicates Columns:=1, Header:=xlYes
    Range("A2").Select
    Range(Selection, Selection.End(xlDown)).Select
    j = Application.WorksheetFunction.CountA(Selection)
    Range("B1").Select
    ActiveCell = j
    Range("A1").Select
    Application.DisplayAlerts = False
Call ComboBox1_Change
    If TypeName(Selection) = "Range" Then
        UserForm3.Show
    End If
    Range("B1").Select
End Sub

Additionally, the UserForm3 code is (I have not worked through the code this far yet, so i do not know if it works or not:

Code:
Private Sub UserForm3_Initialize()
    ListBox1.ColumnCount = 6
    For Each ws In Worksheets
        ComboBox1.AddItem ws.Name
    Next ws
End Sub

Here is what I need: Look at the code and see what the errors are. Please let me know what they are and what I can do to correct them.

NOTE: My current range is from A1:A66 (although that changes). There is a header row in column so the cells that I want to appear on the dropdown list are A2:A66.

Thank you in advance for any help!

Charles
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top