Code to find a string but need to handle multiple matches

mecerrato

Board Regular
Joined
Oct 5, 2015
Messages
174
Office Version
  1. 365
Platform
  1. Windows
I have this code provided from a very helpful coder :) it searches for a string within a range, it currently searches 2 columns and then takes you to the first match.

Was wondering if it can be tweaked so that if there is more than 1 match to ask the user if he wants to go to the next match?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Fnd As Range
   
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "F3" Then
      Set Fnd = Range("C11:D" & Rows.Count).Find(Target.Value, , , xlPart, , , False, , False)
      If Not Fnd Is Nothing Then
         Range("C" & Fnd.Row).Activate
         Range("C" & Fnd.Row).Copy
         'Fnd.Activate
         Application.EnableEvents = False
         Range("F3").Value = Null
         Application.EnableEvents = True
      Else
         MsgBox "Not Found"
         Application.EnableEvents = False
         Range("F3").Value = Null
         Application.EnableEvents = True
         
   End If
   End If
End Sub
 

Attachments

  • 2021-02-12 16_14_09-Pipeline Report Master V10.xlsm - Excel.png
    2021-02-12 16_14_09-Pipeline Report Master V10.xlsm - Excel.png
    72 KB · Views: 15

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
if there is more than 1 match to ask the user if he wants to go to the next match?
Before you go to the next match (assuming there is one) what are you going to do with the first one that is copied with this code but not pasted anywhere?
 
Upvote 0
Before you go to the next match (assuming there is one) what are you going to do with the first one that is copied with this code but not pasted anywhere?
I would put the copy command after the user picked the right match.
 
Upvote 0
OK, see how this goes.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Fnd As Range
  Dim FirstAddr As String
  Dim Resp As VbMsgBoxResult
   
  If Target.Address(0, 0) = "F3" Then
    Set Fnd = Range("C11:D" & Rows.Count).Find(What:=Target.Value, LookAt:=xlPart, MatchCase:=False)
    If Not Fnd Is Nothing Then
      FirstAddr = Fnd.Address
      Do
        Range("C" & Fnd.Row).Activate
        Resp = MsgBox(Prompt:="Is this the one you want?", Buttons:=vbYesNoCancel)
          Select Case Resp
            Case vbYes
              Range("C" & Fnd.Row).Copy
            Case vbNo
              Set Fnd = Range("C11:D" & Rows.Count).FindNext(After:=Fnd)
            Case vbCancel
              MsgBox "OK, Cancelled"
          End Select
      Loop Until Resp = vbYes Or Resp = vbCancel Or Fnd.Address = FirstAddr
    Else
      MsgBox "Not Found"
    End If
    Application.EnableEvents = False
    Range("F3").ClearContents
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
Solution
OK, see how this goes.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Fnd As Range
  Dim FirstAddr As String
  Dim Resp As VbMsgBoxResult
  
  If Target.Address(0, 0) = "F3" Then
    Set Fnd = Range("C11:D" & Rows.Count).Find(What:=Target.Value, LookAt:=xlPart, MatchCase:=False)
    If Not Fnd Is Nothing Then
      FirstAddr = Fnd.Address
      Do
        Range("C" & Fnd.Row).Activate
        Resp = MsgBox(Prompt:="Is this the one you want?", Buttons:=vbYesNoCancel)
          Select Case Resp
            Case vbYes
              Range("C" & Fnd.Row).Copy
            Case vbNo
              Set Fnd = Range("C11:D" & Rows.Count).FindNext(After:=Fnd)
            Case vbCancel
              MsgBox "OK, Cancelled"
          End Select
      Loop Until Resp = vbYes Or Resp = vbCancel Or Fnd.Address = FirstAddr
    Else
      MsgBox "Not Found"
    End If
    Application.EnableEvents = False
    Range("F3").ClearContents
    Application.EnableEvents = True
  End If
End Sub
Thank you, that worked great, I just made one modification, Range("F3").ClearContents changed to Range("F3").Value=Null because F3 is a merged cell. I appreciate you writing this code for me :)
 
Upvote 0
Thank you, that worked great ... I appreciate you writing this code for me
You're welcome.

I just made one modification, Range("F3").ClearContents changed to Range("F3").Value=Null because F3 is a merged cell.
That's fine. You could have used this too. :)
VBA Code:
Range("F3").MergeArea.ClearContents
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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