It might be eayser if I post all the code here, this first part is Userform4 which does the searching of my data and sets up the results in the listbox as well as performs the listbox selection of a row and then launches userform5 which is the code that's giving me issues. I am getting the error "object Variable or with block variable not set" and the code stops with this line highlighted
UserForm5.Show vbModeless
This is UserForm4 getting error as indicated in Red
Private Sub cmbSelect_Click()
If Me.ListBox1.ListIndex = -1 Then
MsgBox " No selection made"
ElseIf Me.ListBox1.ListIndex >= 0 Then
UserForm4.Hide
rw = UserForm4.ListBox1.ListIndex
UserForm5.Show vbModeless
End If
End Sub
Private Sub CommandButton1_Click()
Dim sFindText As String, sFirstAddr As String
Dim i As Long, lFindCol As Long, lCol As Long, lRow As Long
Dim rMyData As Range, rMySearchField As Range, cFound As Range
Dim vArray() As Variant
With Worksheets("Membership Sales")
.Unprotect Password:="taylor97"
End With
'--check User has entered Find_Text
sFindText = Trim(TextBox1.Value)
If sFindText = "" Then Exit Sub 'could add msgbox or label
'--read user's choice of field to search
' (considered using a listbox instead of option buttons)
With Frame1.Controls
lFindCol = 5 'Default if no options buttons True
For i = 0 To .Count - 1
If .Item(i).Value = True Then
Select Case .Item(i).Name
Case "OptionButton1": lFindCol = 5
Case "OptionButton2": lFindCol = 6
Case "OptionButton3": lFindCol = 1
Case "OptionButton4": lFindCol = 20
End Select
Exit For
End If
Next i
End With
With Worksheets("Membership Sales")
Set rMyData = .Range("A2:V500")
Set rMySearchField = rMyData.Resize(, 1).Offset(0, lFindCol - 1)
End With
With rMySearchField
On Error Resume Next
Set cFound = .Find(What:=sFindText, After:=.Cells(.Rows.Count), _
LookIn:=xlFormulas, LookAt:=CheckBox2.Value + 2, _
SearchDirection:=xlNext, MatchCase:=CheckBox1.Value)
On Error GoTo 0
If cFound Is Nothing Then
MsgBox "No Match was found for '" & sFindText & " '", vbExclamation
Else
sFirstAddr = cFound.Address
With rMyData
'--set the boundaries of the array
ReDim vArray(1 To .Columns.Count, 1 To .Rows.Count)
Do Until cFound Is Nothing
'--add matching records from worksheet row to array
lRow = lRow + 1
For lCol = 1 To UBound(vArray, 1)
vArray(lCol, lRow) = .Cells(cFound.Row - .Row + 1, lCol + .Column - 1)
Next lCol
Set cFound = rMySearchField.FindNext(After:=cFound)
If cFound.Address = sFirstAddr Then Exit Do
Loop
'--resize array to fit records stored.
'ReDim Preserve vArray(1 To .Columns.Count, 1 To lRow)
'the If statement was add so the first record would fill listbox horizontily
If lRow = 1 Then lRow = 2
ReDim Preserve vArray(1 To .Columns.Count, 1 To lRow)
End With
'--place the array in the listbox
With Me.ListBox1
.ColumnCount = UBound(vArray, 1)
.ColumnWidths = "50;;80;100" 'use this property to override default col widths
.List = Application.Transpose(vArray)
End With
End If
End With
With Worksheets("Membership Sales")
.Protect Password:="taylor97"
End With
End Sub
Private Sub CommandButton2_Click()
'CLOSE
Me.Hide
Unload Me
End Sub
Private Sub CommandButton3_Click()
'RESET FORM
TextBox1.Value = ""
OptionButton1.Value = True
CheckBox1.Value = False
CheckBox2.Value = False
CheckBox3.Value = True
ListBox1.Clear
TextBox1.SetFocus
End Sub
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then
Label1.Caption = "Surname"
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then
Label1.Caption = "Address Number and Street"
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub OptionButton3_Click()
If OptionButton3.Value = True Then
Label1.Caption = "Registration #"
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub OptionButton4_Click()
If OptionButton4.Value = True Then
Label1.Caption = "KeyFOB Number"
TextBox1.Value = "AD:"
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
CommandButton3.Value = True
End Sub
Private Sub UserForm_Initialize()
ListBox1.Clear
End Sub
This is UserForm5 that has a bunch of TextBox and ComboBox controls to be populated from the Userform4 listbox once launched.
Private Sub UserForm_Initialize()
Dim rw As Long
Dim rMyData As Range
rw = UserForm4.ListBox1.ListIndex
If rw = -1 Then
MsgBox "Nothing selected in ListBox1"
Else
With rMyData.Range(UserForm4.ListBox1.List(rw, 0))
TextBox2.Value = .Offset(0, 4)
TextBox3.Value = .Offset(0, 5)
TextBox40.Value = .Offset(0, 6)
TextBox41.Value = .Offset(0, 7)
TextBox42.Value = .Offset(0, 8)
TextBox43.Value = .Offset(0, 9)
TextBox44.Value = .Offset(0, 10)
TextBox4.Value = .Offset(0, 11)
TextBox5.Value = .Offset(0, 12)
TextBox6.Value = .Offset(0, 13)
End With
End If
End Sub
Bookmarks