Sub Findnext()
Dim Name As String
Dim f As range
Dim ws As Worksheet
Dim s As Integer
Name = surname.Value
'currently only searching one instance...doesn't loop and find the rest
Me.ListBox1.Clear
Set f = Cells.Find(what:=Name, LookIn:=xlValues)
Set findnext = f
With ListBox1
Do
Debug.Print findnext.Address
Set findnext = Cells.findnext(findnext)
.AddItem f.Value
.List(0, 1) = f.Offset(0, 1).Value
.List(0, 2) = f.Offset(0, 2).Value
.List(0, 3) = f.Offset(0, 3).Value
.List(0, 4) = f.Offset(0, 4).Value
.List(0, 5) = f.Offset(0, 5).Value
.List(0, 6) = f.Offset(0, 6).Value
Loop While findnext.Address <> f.Address
End With
End Sub
how do i make this code loop so that it would find multiple f values? essentailly, I have a search button and it promopts "There are 3 instances" and in the list box, it should list out the 3 instances (eg. same name).
I tried using For each f and next f in the code above, but it still only picks one f.value and doesn't pick any other cells with same name....
EDIT:
i've added the loop function but now in the list box, it only lists the person's name instead of listing all the offset values. is the offset not applied to the loop? or is it because it's only looking for f? which is the name it's looking for?
EDIT: The coding I have done so far...
Private Sub CommandButton1_Click()
MsgBox "Directorate has been added", vbOKOnly
Dim ctrl As control
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
'Pass this CheckBox to the subroutine below:
TransferValues ctrl
End If
Next
TransferMasterValue
End Sub
Sub TransferValues(cb As MSForms.CheckBox)
Dim ws As Worksheet
Dim emptyRow As Long
If cb Then
'Define the worksheet based on the CheckBox.Name property:
Set ws = Sheets(Left(cb.Name, 15))
emptyRow = WorksheetFunction.CountA(ws.range("A:A")) + 1
With ws
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 6).Value = officenumber.Value
.Cells(emptyRow, 7).Value = cellnumber.Value
End With
End If
End Sub
Sub TransferMasterValue()
Dim allChecks As String
Dim ws As Worksheet
'Iterate through the checkboxes concatenating a string of all names
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl Then
allChecks = allChecks & ctrl.Name & ""
End If
End If
Next
'If you have at least one transfer to the Master sheet
If Len(allChecks) > 0 Then
Set ws1 = Sheets("Master")
emptyRow = WorksheetFunction.CountA(range("A:A")) + 1
With ws1
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 7).Value = officenumber.Value
.Cells(emptyRow, 8).Value = cellnumber.Value
.Cells(emptyRow, 6).Value = Left(allChecks, Len(allChecks) - 1)
End With
End If
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub
Private Sub CommandButton3_Click()
surname.Value = ""
firstname.Value = ""
tod.Value = ""
program.Value = ""
email.Value = ""
officenumber.Value = ""
cellnumber.Value = ""
PACT.Value = False
PrinceRupert.Value = False
WPM.Value = False
Montreal.Value = False
TET.Value = False
TC.Value = False
US.Value = False
Other.Value = False
End Sub
Private Sub ListBox1_Click()
Dim r As Long
With Me.ListBox1
With Me
.surname.Value = .ListBox1.List(.ListBox1.ListIndex, 0)
.firstname.Value = .ListBox1.List(.ListBox1.ListIndex, 1)
.tod.Value = .ListBox1.List(.ListBox1.ListIndex, 2)
.program.Value = .ListBox1.List(.ListBox1.ListIndex, 3)
.email.Value = .ListBox1.List(.ListBox1.ListIndex, 4)
.officenumber.Value = .ListBox1.List(.ListBox1.ListIndex, 5)
.cellnumber.Value = .ListBox1.List(.ListBox1.ListIndex, 6)
End With
End With
End Sub
Private Sub Search_Click() 'only searches in master tab right now need to search from all worksheets
Dim Name As String
Dim f As range
Dim r As Long
Dim ws As Worksheet
Dim s As Integer
Dim FirstAddress As String
Name = surname.Value
With ws
Set f = range("A:A").Find(what:=Name, LookIn:=xlValues)
If Not f Is Nothing Then
With Me
firstname.Value = f.Offset(0, 1).Value
tod.Value = f.Offset(0, 2).Value
program.Value = f.Offset(0, 3).Value
email.Value = f.Offset(0, 4).Text
officenumber.Value = f.Offset(0, 5).Text
cellnumber.Value = f.Offset(0, 6).Text
End With
findnext
FirstAddress = f.Address
Do
s = s + 1
Set f = range("A:A").findnext(f)
Loop While Not f Is Nothing And f.Address <> FirstAddress
If s > 1 Then
Select Case MsgBox("There are " & s & " instances of " & Name, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
findnext
Case vbCancel
End Select
End If
Else: MsgBox Name & "Not Listed"
End If
End With
End Sub
Sub findnext()
Dim Name As String
Dim f As range
Dim ws As Worksheet
Dim s As Integer
Dim findnext As range
Name = surname.Value
Me.ListBox1.Clear
Set f = range("A:A").Find(what:=Name, LookIn:=xlValues)
Set findnext = f
With ListBox1
Do
Debug.Print findnext.Address
Set findnext = range("A:A").findnext(findnext)
.AddItem findnext.Value
.List(0, 1) = findnext.Offset(0, 1).Value
.List(0, 2) = findnext.Offset(0, 2).Value
.List(0, 3) = findnext.Offset(0, 3).Value
.List(0, 4) = findnext.Offset(0, 4).Value
.List(0, 5) = findnext.Offset(0, 5).Value
.List(0, 6) = findnext.Offset(0, 6).Value
.List(0, 7) = findnext.Offset(0, 6).Value
Loop While findnext.Address <> f.Address
End With
End Sub
You need to you Find then FindNext in a loop. You know you are done looping when your FindNext finds the very first thing you found again. It will cycle like that.
Dim firstFind As Range, subsequentFinds As Range
Set firstFind = Range("D3:D500").Find("search string", , xlValues)
Set subsequentFinds = firstFind
Do
Debug.Print subsequentFinds.Address
Set subsequentFinds = Cells.FindNext(subsequentFinds)
Loop While subsequentFinds.Address <> firstFind.Address
Related
Hi I have the following code:
Private Sub Search_Click()
Dim Name As String
Dim f As Range
Dim r As Long
Dim ws As Worksheet
Dim s As Integer
Dim FirstAddress As String
Dim str() As String
Name = surname.Value
With ws
Set f = Range("A:A").Find(what:=Name, LookIn:=xlValues)
If Not f Is Nothing Then
With Me
firstname.Value = f.Offset(0, 1).Value
tod.Value = f.Offset(0, 2).Value
program.Value = f.Offset(0, 3).Value
email.Value = f.Offset(0, 4).Text
SetCheckBoxes f.Offset(0, 5) '<<< replaces code below
officenumber.Value = f.Offset(0, 6).Text
cellnumber.Value = f.Offset(0, 7).Text
r = f.Row '<<<<<<<<< using this to locate the row of "found"
End With
findnext
FirstAddress = f.Address
Do
s = s + 1
Set f = Range("A:A").findnext(f)
Loop While Not f Is Nothing And f.Address <> FirstAddress
If s > 1 Then
Select Case MsgBox("There are " & s & " instances of " & Name, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
findnext
Case vbCancel
End Select
End If
Else: MsgBox Name & "Not Listed"
End If
End With
End Sub
and i want to use an update button:
private Sub update_Click()
Dim Name As String
Dim f As Range
Dim ws As Worksheet
With ws
Set f = .Cells(r, 1) '<<<<<<<<<<<<< Mismatch type error
f.Value = surname.Value
f.Offset(0, 1).Value = firstname.Value
f.Offset(0, 2).Value = tod.Value
f.Offset(0, 3).Value = program.Value
f.Offset(0, 4).Value = email.Value
f.Offset(0, 5).Value = GetCheckBoxes
f.Offset(0, 6).Value = officenumber.Value
f.Offset(0, 7).Value = cellnumber.Value
End With
End Sub
so i want to locate the row of the found cell and replace all the cells to whatever is written in the textbox (updating the previous infomration with new information) - however i get an error on set f = .cells(r,1) how can i fix this?
Try to use global variable r: instead declaring Dim r As Long in Private Sub Search_Click(), use Public r As Long outside all functions in the very top of module :
Public r As Long
Public Sub Search_Click()
'your code
End Sub
Public Sub update_Click()
'your code
End Sub
Now, after calling Search_Click r would be inizialized and then you could call update_Click.
P.S. Don't forget to remove Dim r As Long from Private Sub Search_Click().
BTW, in your update_Click and Search_Click subs you haven't initialized ws variable: Set ws = ThisWorkbook.Worksheets("Sheet1"). After adding this line change Range("A:A") to .Range("A:A") in Search_Click
i want have an add command and a search command. i thought if i reciprocate the add command then i can use it as a search command but it gives an invalid procedure call or argument.
add command:
Sub TransferMasterValue()
Dim allchecks As String
Dim ws As Worksheet
'Iterate through the checkboxes concatenating a string of all names
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl Then
allchecks = allchecks & ctrl.Name & " "
End If
End If
Next
'If you have at least one transfer to the Master sheet
If Len(allchecks) > 0 Then
Set ws1 = Sheets("Master")
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
With ws1
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 7).Value = officenumber.Value
.Cells(emptyRow, 8).Value = cellnumber.Value
.Cells(emptyRow, 6).Value = Left(allchecks, Len(allchecks) - 1)
End With
End If
End Sub
Search Command:
Private Sub Search_Click()
Dim Name As String
Dim f As Range
Dim r As Long
Dim ws As Worksheet
Dim s As Integer
Dim FirstAddress As String
Dim ctrl As control
Dim allchecks As String
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl.value = true Then
allchecks = allchecks & ctrl.Name & " "
End If
End If
Next
Name = surname.Value
With ws
Set f = Range("A:A").Find(what:=Name, LookIn:=xlValues)
If Not f Is Nothing Then
With Me
firstname.Value = f.Offset(0, 1).Value
tod.Value = f.Offset(0, 2).Value
program.Value = f.Offset(0, 3).Value
email.Value = f.Offset(0, 4).Text
officenumber.Value = f.Offset(0, 6).Text
cellnumber.Value = f.Offset(0, 7).Text
EDIT:
If Len(allchekcs)>0 then
If f.Offset(0, 5).Value = Left(allchecks, Len(allchecks) - 1) Then ctrl.Value = True
end if
EDIT: so i've added the if Len(allchecks)>0 then command, and it doesn't give error 5 but it still doesn't checkmark the userform checkboxes - how can i fix this? right now when the information is added to column 6, it's added as "Montreal Ottawa Toronto Vancouver" and perhaps that's why it's not picking it up? because there's multiple checkbox names in one cell? in order for ctrl.value = true to work the cell value has to equal to one ctrl.name? is there a way i can separate so it picks up using ctrl?
To separate the values in the cell, use the Split command:
For each ctrl in UserForm1.Controls
For i = 0 to UBound(Split(CellValue," "))
If Split(CellValue, " ")(i) = ctrl.Name Then ctrl.Value = True
Next i
Next
Where CellValue is the Value of the cell that contains the allchecks value.
So I have the following code below: and what i want to focus on is the Private Sub Search_Click(). Currently when I search someone's surname it populate the textboxes automatically. Is it possible to have a search box that populates the checkboxes automatically? so for example, if the person belongs to 6/8 checkboxes, and i click search 6/8 checkboxes would be checkmarked? and is it possible to do the same thing with ListBox1_Click()? so when i click on person's name from listbox, it also popoulates the checkbocxes automatically depending on which checkbox that person belongs to?
EDIT
Private Sub Search_Click() 'only searches in master tab right now need to search from all worksheets
Dim Name As String
Dim f As Range
Dim r As Long
Dim ws As Worksheet
Dim s As Integer
Dim FirstAddress As String
Name = surname.Value
For Each ws In ActiveWorkbook.Sheets
With ws
Set f = Range("A:A").Find(what:=Name, LookIn:=xlValues)
If Not f Is Nothing Then
If cb.Name = ws.Name Then
cb.Value = True
End If
Next
With Me
firstname.Value = f.Offset(0, 1).Value
tod.Value = f.Offset(0, 2).Value
program.Value = f.Offset(0, 3).Value
email.Value = f.Offset(0, 4).Text
officenumber.Value = f.Offset(0, 5).Text
cellnumber.Value = f.Offset(0, 6).Text
Is this what it would look like...? I can't get it to work though?
Private Sub CommandButton1_Click()
MsgBox "Directorate has been added", vbOKOnly
Dim ctrl As control
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
'Pass this CheckBox to the subroutine below:
TransferValues ctrl
End If
Next
TransferMasterValue
End Sub
Sub TransferValues(cb As MSForms.CheckBox)
Dim ws As Worksheet
Dim emptyRow As Long
If cb Then
'Define the worksheet based on the CheckBox.Name property:
Set ws = Sheets(Left(cb.Name, 15))
emptyRow = WorksheetFunction.CountA(ws.range("A:A")) + 1
With ws
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 6).Value = officenumber.Value
.Cells(emptyRow, 7).Value = cellnumber.Value
End With
End If
End Sub
Sub TransferMasterValue()
Dim allChecks As String
Dim ws As Worksheet
'Iterate through the checkboxes concatenating a string of all names
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl Then
allChecks = allChecks & ctrl.Name & ""
End If
End If
Next
'If you have at least one transfer to the Master sheet
If Len(allChecks) > 0 Then
Set ws1 = Sheets("Master")
emptyRow = WorksheetFunction.CountA(range("A:A")) + 1
With ws1
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 7).Value = officenumber.Value
.Cells(emptyRow, 8).Value = cellnumber.Value
.Cells(emptyRow, 6).Value = Left(allChecks, Len(allChecks) - 1)
End With
End If
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub
Private Sub CommandButton3_Click()
surname.Value = ""
firstname.Value = ""
tod.Value = ""
program.Value = ""
email.Value = ""
officenumber.Value = ""
cellnumber.Value = ""
PACT.Value = False
PrinceRupert.Value = False
WPM.Value = False
Montreal.Value = False
TET.Value = False
TC.Value = False
US.Value = False
Other.Value = False
End Sub
Private Sub ListBox1_Click()
Dim r As Long
With Me.ListBox1
With Me
.surname.Value = .ListBox1.List(.ListBox1.ListIndex, 0)
.firstname.Value = .ListBox1.List(.ListBox1.ListIndex, 1)
.tod.Value = .ListBox1.List(.ListBox1.ListIndex, 2)
.program.Value = .ListBox1.List(.ListBox1.ListIndex, 3)
.email.Value = .ListBox1.List(.ListBox1.ListIndex, 4)
.officenumber.Value = .ListBox1.List(.ListBox1.ListIndex, 5)
.cellnumber.Value = .ListBox1.List(.ListBox1.ListIndex, 6)
End With
End With
End Sub
Private Sub Search_Click() 'only searches in master tab right now need to search from all worksheets
Dim Name As String
Dim f As range
Dim r As Long
Dim ws As Worksheet
Dim s As Integer
Dim FirstAddress As String
Name = surname.Value
With ws
Set f = range("A:A").Find(what:=Name, LookIn:=xlValues)
If Not f Is Nothing Then
With Me
firstname.Value = f.Offset(0, 1).Value
tod.Value = f.Offset(0, 2).Value
program.Value = f.Offset(0, 3).Value
email.Value = f.Offset(0, 4).Text
officenumber.Value = f.Offset(0, 5).Text
cellnumber.Value = f.Offset(0, 6).Text
End With
findnext
FirstAddress = f.Address
Do
s = s + 1
Set f = range("A:A").findnext(f)
Loop While Not f Is Nothing And f.Address <> FirstAddress
If s > 1 Then
Select Case MsgBox("There are " & s & " instances of " & Name, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
findnext
Case vbCancel
End Select
End If
Else: MsgBox Name & "Not Listed"
End If
End With
End Sub
Sub findnext()
Dim Name As String
Dim f As range
Dim ws As Worksheet
Dim s As Integer
Dim findnext As range
Name = surname.Value
Me.ListBox1.Clear
Set f = range("A:A").Find(what:=Name, LookIn:=xlValues)
Set findnext = f
With ListBox1
Do
Debug.Print findnext.Address
Set findnext = range("A:A").findnext(findnext)
.AddItem findnext.Value
.List(0, 1) = findnext.Offset(0, 1).Value
.List(0, 2) = findnext.Offset(0, 2).Value
.List(0, 3) = findnext.Offset(0, 3).Value
.List(0, 4) = findnext.Offset(0, 4).Value
.List(0, 5) = findnext.Offset(0, 5).Value
.List(0, 6) = findnext.Offset(0, 6).Value
.List(0, 7) = findnext.Offset(0, 6).Value
Loop While findnext.Address <> f.Address
End With
End Sub
It doesnt seem like a coding problem, I saw the code where you set the textboxes text. Basically, instead of setting textboxes text, you'd be setting the checkbox. the code to to that is ChkCheck.value = true.
leave a comment if theres a deeper issue and ill make an edit
edit
To search all sheets,
I would put a loop in there like
for each ws in ActiveWorkbook.Sheets
and then put your search in there.
and then, after the if f is not nothing then
loop through all controls, and check to see if the controls name = the sheets name. -
if ctrl.name = ws.name then
ctrl.value = true
end if
like that
so then each time the sheets loop runs, if it finds the specific name, the checkbox accosiated with the particular sheet will check.
I'm in the process of making a userform. I have managed to setup a find function using the code below which then also loops and counts the number of cases in the spreadsheet.
I have also created a function to find the next item which is operated by a separate command button but it does not display the records in the userform so it can be amended.
Does anyone have any ideas on how to fix this?
Private Sub FindNext_Click()
Cells.FindNext(After:=ActiveCell).Activate
End Sub
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range("e65536").End(xlUp))
Dim f As Integer
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
c.Select
With Me
.TextBox2.Value = c.Offset(0, 1).Value
.TextBox3.Value = c.Offset(0, 2).Value
.TextBox4.Value = c.Offset(0, 3).Value
.TextBox5.Value = c.Offset(0, 4).Value
.TextBox6.Value = c.Offset(0, 5).Value
.TextBox7.Value = c.Offset(0, 6).Value
.TextBox8.Value = c.Offset(0, 7).Value
.TextBox9.Value = c.Offset(0, 8).Value
.TextBox10.Value = c.Offset(0, 9).Value
.TextBox11.Value = c.Offset(0, 10).Value
.TextBox12.Value = c.Offset(0, 11).Value
.TextBox13.Value = c.Offset(0, 12).Value
.TextBox14.Value = c.Offset(0, 13).Value
.TextBox20.Value = c.Offset(0, 14).Value
.TextBox21.Value = c.Offset(0, 15).Value
.TextBox15.Value = c.Offset(0, 16).Value
.TextBox22.Value = c.Offset(0, 17).Value
.TextBox16.Value = c.Offset(0, 18).Value
.TextBox18.Value = c.Offset(0, 19).Value
.TextBox19.Value = c.Offset(0, 20).Value
.update.Enabled = True
.Add.Enabled = False
f = 0
End With
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
Case vbCancel
End Select
Me.Height = frmMax
End If
Else: MsgBox strFind & " not listed"
End If
End With
End Sub
Thanks
You need to encapsulate the update code into its own method (sub) then you can call it for both Find and Find Next. Like:
Private Sub FindNext_Click()
Dim nextCell As Range
Set nextCell = Cells.FindNext(After:=ActiveCell)
'FindNext loops round to the initial cell if it finds no other so we test for it
If Not nextCell.Address(external:=true) = ActiveCell.Address(external:=true) Then
updateFields anchorCell:=nextCell
End If
End Sub
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range("e65536").End(xlUp))
Dim f As Integer
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
updateFields anchorCell:=c
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
Case vbCancel
End Select
Me.Height = frmMax
End If
Else: MsgBox strFind & " not listed"
End If
End With
End Sub
Private Sub updateFields(anchorCell As Range)
anchorCell.Select
With Me
.TextBox2.Value = anchorCell.Offset(0, 1).Value
.TextBox3.Value = anchorCell.Offset(0, 2).Value
.TextBox4.Value = anchorCell.Offset(0, 3).Value
.TextBox5.Value = anchorCell.Offset(0, 4).Value
.TextBox6.Value = anchorCell.Offset(0, 5).Value
.TextBox7.Value = anchorCell.Offset(0, 6).Value
.TextBox8.Value = anchorCell.Offset(0, 7).Value
.TextBox9.Value = anchorCell.Offset(0, 8).Value
.TextBox10.Value = anchorCell.Offset(0, 9).Value
.TextBox11.Value = anchorCell.Offset(0, 10).Value
.TextBox12.Value = anchorCell.Offset(0, 11).Value
.TextBox13.Value = anchorCell.Offset(0, 12).Value
.TextBox14.Value = anchorCell.Offset(0, 13).Value
.TextBox20.Value = anchorCell.Offset(0, 14).Value
.TextBox21.Value = anchorCell.Offset(0, 15).Value
.TextBox15.Value = anchorCell.Offset(0, 16).Value
.TextBox22.Value = anchorCell.Offset(0, 17).Value
.TextBox16.Value = anchorCell.Offset(0, 18).Value
.TextBox18.Value = anchorCell.Offset(0, 19).Value
.TextBox19.Value = anchorCell.Offset(0, 20).Value
.Update.Enabled = True
.Add.Enabled = False
End With
End Sub
Afternoon
I'm a mere novice of an amateur in the world of VB.
I'm currently creating a userform in Excel and to search for records I decided to use a listbox option to allow a user to scroll through the search results.
However, I've encountered a run-time 380 error invalid property value due to the listbox exceeding ten entries.
I have managed to find a solution using rowsource command but I can't find how to use it in my code. Any advice is welcome and if anyone can think of a better way I would be grateful.
`enter code here
Dim MyData As Range
Dim c As Range
Dim rFound As Range
Dim r As Long
Dim rng As Range
Const frmMax As Long = 640
Const frmHt As Long = 210
Const frmWidth As Long = 280
Dim sFileName As String
Dim oCtrl As MSForms.Control
Private Sub Add_Click()
Set c = Range("a65536").End(xlUp).Offset(1, 0)
Application.ScreenUpdating = False
With Me
c.Value = .TextBox1.Value
c.Offset(0, 1).Value = .TextBox2.Value
c.Offset(0, 2).Value = .TextBox3.Value
c.Offset(0, 3).Value = .TextBox4.Value
c.Offset(0, 4).Value = .TextBox5.Value
c.Offset(0, 5).Value = .TextBox6.Value
c.Offset(0, 6).Value = .TextBox7.Value
c.Offset(0, 7).Value = .TextBox8.Value
c.Offset(0, 8).Value = .TextBox9.Value
c.Offset(0, 9).Value = .TextBox10.Value
c.Offset(0, 10).Value = .TextBox11.Value
ClearControls
End With
Application.ScreenUpdating = True
End Sub
Private Sub Find_Click()
Worksheets("Master").Activate
Dim strFind As String
Dim FirstAddress As String
Dim rSearch As Range
Set rSearch = Range("a1", Range("e65536").End(xlUp))
Dim f As Integer
strFind = Me.TextBox1.Value
With rSearch
Set c = .Find(strFind, LookIn:=xlValues)
If Not c Is Nothing Then
c.Select
With Me
.TextBox2.Value = c.Offset(0, 1).Value
.TextBox3.Value = c.Offset(0, 2).Value
.TextBox4.Value = c.Offset(0, 3).Value
.TextBox5.Value = c.Offset(0, 4).Value
.TextBox6.Value = c.Offset(0, 5).Value
.TextBox7.Value = c.Offset(0, 6).Value
.TextBox8.Value = c.Offset(0, 7).Value
.TextBox9.Value = c.Offset(0, 8).Value
.TextBox10.Value = c.Offset(0, 9).Value
.update.Enabled = True
.Add.Enabled = False
f = 0
End With
FirstAddress = c.Address
Do
f = f + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
If f > 1 Then
Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")
Case vbOK
FindAll
Case vbCancel
End Select
Me.Height = frmMax
End If
Else: MsgBox strFind & " not listed"
End If
End With
If Sheet2.AutoFilterMode Then Sheet2.Range("A8").AutoFilter
End Sub
Private Sub TextBox11_Change()
End Sub
Private Sub update_Click()
Application.ScreenUpdating = False
If rng Is Nothing Then GoTo skip
For Each c In rng
If r = 0 Then c.Select
r = r - 1
Next c
skip:
Set c = ActiveCell
c.Value = Me.TextBox1.Value
c.Offset(0, 1).Value = Me.TextBox2.Value
c.Offset(0, 2).Value = Me.TextBox3.Value
c.Offset(0, 3).Value = Me.TextBox4.Value
c.Offset(0, 4).Value = Me.TextBox5.Value
c.Offset(0, 5).Value = Me.TextBox6.Value
c.Offset(0, 6).Value = Me.TextBox7.Value
c.Offset(0, 7).Value = Me.TextBox8.Value
c.Offset(0, 8).Value = Me.TextBox9.Value
c.Offset(0, 9).Value = Me.TextBox10.Value
c.Offset(0, 10).Value = Me.TextBox11.Value
With Me
.update.Enabled = False
.Add.Enabled = True
ClearControls
End With
If Sheet1.AutoFilterMode Then Sheet1.Range("A8").AutoFilter
Application.ScreenUpdating = True
On Error GoTo 0
End Sub
Sub FindAll()
Worksheets("Master").Activate
Dim strFind As String
Dim rFilter As Range
Set rFilter = Sheet2.Range("a1", Range("Z65536").End(xlUp))
Set rng = Sheet2.Range("a1", Range("a65536").End(xlUp))
strFind = Me.TextBox1.Value
With Sheet2
If Not .AutoFilterMode Then .Range("A2").AutoFilter
rFilter.AutoFilter Field:=1, Criteria1:=strFind
Set rng = rng.Cells.SpecialCells(xlCellTypeVisible)
Me.ListBox1.Clear
For Each c In rng
With Me.ListBox1
.AddItem c.Value
.List(.ListCount - 1, 1) = c.Offset(0, 1).Value
.List(.ListCount - 1, 2) = c.Offset(0, 2).Value
.List(.ListCount - 1, 3) = c.Offset(0, 3).Value
.List(.ListCount - 1, 4) = c.Offset(0, 4).Value
.List(.ListCount - 1, 5) = c.Offset(0, 5).Value
.List(.ListCount - 1, 6) = c.Offset(0, 6).Value
.List(.ListCount - 1, 7) = c.Offset(0, 7).Value
.List(.ListCount - 1, 8) = c.Offset(0, 8).Value
.List(.ListCount - 1, 9) = c.Offset(0, 9).Value
.List(.ListCount - 1, 10) = c.Offset(0, 10).Value
End With
Next c
End With
End Sub
Private Sub ListBox1_Click()
If Me.ListBox1.ListIndex = -1 Then 'not selected
MsgBox " No selection made"
ElseIf Me.ListBox1.ListIndex >= 1 Then 'User has selected
r = Me.ListBox1.ListIndex
With Me
.TextBox1.Value = ListBox1.List(r, 0)
.TextBox2.Value = ListBox1.List(r, 1)
.TextBox3.Value = ListBox1.List(r, 2)
.TextBox4.Value = ListBox1.List(r, 3)
.TextBox5.Value = ListBox1.List(r, 4)
.TextBox6.Value = ListBox1.List(r, 5)
.TextBox7.Value = ListBox1.List(r, 6)
.TextBox8.Value = ListBox1.List(r, 7)
.TextBox9.Value = ListBox1.List(r, 8)
.TextBox10.Value = ListBox1.List(r, 9)
.update.Enabled = True 'allow amendment or
.Add.Enabled = False 'don't want duplicate
End With
End If
End Sub
Sub ClearControls()
With Me
For Each oCtrl In .Controls
Select Case TypeName(oCtrl)
Case "TextBox": oCtrl.Value = Empty
Case "OptionButton": oCtrl.Value = False
End Select
Next oCtrl
End With
End Sub
Private Sub UserForm_Click()
End Sub
You might take a look at the ListView Control (Right-click on the toolbox and search for additional controls, look for Microsoft ListView Control, version 6.0).
Not being the most modern and polished, it may still be very fitting for your immediate needs.
Some sample might look like this:
You build the columns by adding the ColumnHeaders first. Then you add ListItems (=first column) which also each allocates a set óf SubItems (=2nd to last column, index from 1).
Dim l As ListItem
With Me.ListView1
.FullRowSelect = True
.LabelEdit = lvwManual
.View = lvwReport
For i = 1 To 11
.ColumnHeaders.Add , , CStr(i)
Next
.HideColumnHeaders = False
Set l = .ListItems.Add(, , c.Text)
For i = 1 To 10
l.SubItems(i) = c.Offset(0, i).Text
Next
End With