Hi all,
I hope you can help me. This is a 2 part issue.
I have a dropdown ComboBox filter method i would like to use on a UserForm for filtering my data.
But the code does not always work properly
- i get a lot of Run-time errors (1004) "No cells were found" - especially now that I am using it to filter and select dates or times. I don't know if has to do with the format?
- Also, how can the code be modified that the row that is filtered can be used to update my UserForm fields with the filtered data?
Below is my PREVIOUS search function code used for updating my userform - I would like to accomplish the same effect using the ComboBoxes (drop-downs):
UserForm Code:
'SEARCH FUNCTION
Private Sub TextBox1_Change()
Dim rng As Range, e
Dim Ans As Long
With Me
.ListBox3.Clear
If Len(.TextBox1.Value) Then
For Each e In Sheets("DataBase").Cells(1).CurrentRegion.Columns(1).Offset(1).Value
If (e <> "") * (e Like "*" & .TextBox1.Value & "*") Then
.ListBox3.AddItem e
End If
Next
With .ListBox3
If .ListCount > 0 Then .ListIndex = 0
If .ListCount = 0 Then Ans = MsgBox("No Search reults found, continue?", vbYesNo)
If Ans = vbYes Then
If MsgBox("Would you like to create a new record?", vbYesNo) = vbYes Then
Call ActiveControls
End If
'Call btnNew_Click
Me.cbxEquipmentCommon.SetFocus
End If
End With
End If
End With
End Sub
'SEARCH FUNCTION
Private Sub ListBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim ctl As Control
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Or TypeName(ctl) = "Checkbox" Then
ctl.Enabled = True
End If
Next ctl
Me.btnSave.Enabled = True
Me.GUID.Enabled = False
Dim Crit1 As String, FirstRowAdd As String
Dim LastRow As Long, FirstRow As Long
Crit1 = ListBox3.Value
LastRow = Sheets("Database").Range("A" & Rows.Count).End(xlUp).Row
Sheets("DataBase").Range("A4:AQ" & LastRow).AutoFilter Field:=1, Criteria1:=Crit1
FirstRowAdd = FirstVisibleValue(ActiveSheet, 1)
FirstRow = Range(FirstRowAdd).Row
'Populate Userfrom with values from First Filtered Row
With UserForm1
.GUID.Value = Sheets("DataBase").Cells(FirstRow, 1).Value
End With
End Sub
Module Code:
Function FirstVisibleValue(ByRef Sht As Worksheet, ByVal FilterCol As Long)
Dim R As Range
If Sheets("DataBase").AutoFilterMode Then
Set R = Sheets("DataBase").AutoFilter.Range
FirstVisibleValue = R.Offset(1, FilterCol - 1).Resize(R.Rows.Count, 1).SpecialCells(12).Cells(1).Address
End If
End Function
Thank you!
Bookmarks