Hello, I have read this user form code eight thousand times and cannot find what I am missing but this code currently will only run if the active sheet is "DATABASE". If I attempt to run it with another sheet active I get Run Time Error 1004: Application-defined or object-defined error.
Const db As String = "Customer Equipment Database.xlsm"
Private Sub OKButt_Click()
If MultiPage1.Value = 0 Then MsgBox CSelectBox.Value
If MultiPage1.Value = 1 Then 'ID number tab is selected
Dim RowRange As Range
Dim RowCnt As Integer, r As Integer
RowCnt = 0
For r = 0 To PSelectBox.ListCount - 1
If PSelectBox.Selected(r) Then
RowCnt = RowCnt + 1
If RowCnt = 1 Then
Set RowRange = Workbooks(db).Worksheets("DATABASE"). _
Range("A2", Range("A2").End(xlDown)).Rows(r + 1)
Else
Set RowRange = _
Union(RowRange, Workbooks(db).Worksheets("DATABASE"). _
Range("A2", Range("A2").End(xlDown)).Rows(r + 1))
End If
End If
Next r
If Not RowRange Is Nothing Then RowRange.Select
End If
Unload Me
End Sub
Private Sub UserForm_Initialize()
'POPULATE CUSTOMER SELECTION BOX
Dim cell As Range
Dim NoDupes As New Collection
Dim Custs As Range: Set Custs = Workbooks(db).Worksheets("DATABASE"). _
Range("A2", Range("A2").End(xlDown))
Dim vaItems As Variant
'add unique customer names to new collection
On Error Resume Next
For Each cell In Custs
NoDupes.Add cell.Value, CStr(cell.Value)
Next cell
On Error GoTo 0
'add the customers to the box
CSelectBox.RowSource = ""
For Each Item In NoDupes
CSelectBox.AddItem Item
Next Item
'sort and re-add customers to the box
vaItems = CSelectBox.list
For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
For j = i + 1 To UBound(vaItems, 1)
If vaItems(i, 0) > vaItems(j, 0) Then
vTemp = vaItems(i, 0)
vaItems(i, 0) = vaItems(j, 0)
vaItems(j, 0) = vTemp
End If
Next j
Next i
CSelectBox.Clear
For i = LBound(vaItems, 1) To UBound(vaItems, 1)
CSelectBox.AddItem vaItems(i, 0)
Next i
'*****************************************************************************************
'POPULATE ID SELECTION BOX
Dim rng As Range
Set rng = Workbooks(db).Worksheets("DATABASE").Range("A2:C2", Range("A2:C2").End(xlDown))
With PSelectBox
.ColumnCount = 3
.RowSource = rng.Address
.ListIndex = 0
End With
End Sub
Bookmarks