VBA/UserForm Find Data - Multiple Criteria

zodiaceuk

Board Regular
Joined
Nov 20, 2011
Messages
103
Hi Guys,

I’ve hit a stumbling block with this, hoping someone can help please?

I’m looking to use some VBA code in my userform to find data from multiple criteria.

Table
EmpID Date Reason
123. 24/10/19 Off
345. 10/10/19. AL
123. 25/10/19. AL

I have another userform to enter the data into the table.

My problem is, supposing I need to edit/remove the entry,
Is it possible for another userform to be able to find the row based on all 3 criteria, so that this can be edited/removed please?

I’m using this code on elsewhere, this works great but not sure how to extend this to 3 criteria.
Code:
Dim Ran1 as Range
Dim strsrch as String
Dim TBrow as long

Strsrch = me.EmpID.Values
Set Ran1 = Sheets (“Active”). Range(“A:A”).Find(what:strsrch, Lookat:=xlWhole)
If Ran1 is nothing then
Msgbox ‘message details here
Exit sub
End if
TBrow = Ran1.row

Thanks guys
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I hope the following example helps you

Change data in red for your information.
Code:
Private Sub CommandButton1_Click()
  Dim f As Range, strsrch As String, TBrow As Long
  Dim sh As Worksheet, r As Range, cell As String
  Set sh = Sheets("Active")
  Set r = sh.Range("A:A")
  strsrch = Me.EmpID.Value
[COLOR=#008000]  'look for first criteria[/COLOR]
  Set f = r.Find(strsrch, , xlValues, xlWhole)
  If Not f Is Nothing Then
    cell = f.Address
    Do
[COLOR=#008000]      'look for second and third criteria[/COLOR]
      If sh.Cells(f.Row, "[COLOR=#ff0000]B[/COLOR]").Value = CDate([COLOR=#ff0000]TextBox2[/COLOR].Value) And _
         sh.Cells(f.Row, "[COLOR=#ff0000]C[/COLOR]").Value = [COLOR=#ff0000]TextBox3[/COLOR].Value Then
        TBrow = f.Row
        Exit Do
      End If
      Set f = r.FindNext(f)
    Loop While Not f Is Nothing And f.Address <> cell
  End If
  If TBrow = 0 Then
    MsgBox "Does not exists"
  Else
    MsgBox "message details here, row: " & TBrow
  End If
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,803
Members
449,048
Latest member
greyangel23

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