VBA code to find matching values

snjpverma

Well-known Member
Joined
Oct 2, 2008
Messages
1,584
Office Version
  1. 365
Platform
  1. Windows
NamesGuess
John5
Tom5
Addy6
Abby5
Daddy8
Final Result5


There are three people's values that are matching the final Result. Msgbox required to show the names of the person whose value matches the final result.

Output: Msgbox is required as shown below:

1587553706698.png

The above Msgbox has been created manually for demo purpose.

Any help is highly appreciated.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
The main part I am confused about is how to get the "&" before the last value in Macro.

If not a macro, then an excel formula would also do.
 
Upvote 0
What code have you already got?
 
Upvote 0
Thanks for responding, Fluff

VBA Code:
Sub findmatch()
Dim i As Integer, msg As String

msg = "The final result has been correctly guessed by"

For i = 2 To 6
    If Range("b" & i).Value = Range("B9") Then
        msg = msg & " ," & Range("a" & i).Value
    End If
Next i

MsgBox msg
End Sub

As shown in below output, the commas are incorrect.
There should not be a comma before the first name and last name as shown in Post # 1. The comma before last name should be replaced by "&" or "and".

1587558293821.png
 
Upvote 0
Maybe like below?
Amend ranges to suit.

VBA Code:
Sub Quiz()
Dim Guesses, Result As Range
Dim Winners, x As String
Dim Correct, c As Integer
Set Guesses = Range("B2:B5")
Set Result = Range("B9")

x = ","
c = 0
Correct = WorksheetFunction.CountIf(Guesses, Result)
If Correct = 0 Then
    Winners = " nobody"
    GoTo Show
End If

For Each cell In Guesses.Cells
    If cell = Result Then
    c = c + 1
    
    If c = Correct Then x = " &"
       
        Winners = Winners & x & " " & cell.Offset(0, -1)
        
    End If

Next

Show:
MsgBox "The final result has been guessed by" & Winners & "!", vbOKCancel, "RESULT!"

End Sub

Hope that helps.
 
Upvote 0
Here is what I came up with:
VBA Code:
Sub MyMatchingMacro()

    Dim nameRng As Range
    Dim cell As Range
    Dim finRes As Long
    Dim str As String
    Dim lc As Long
    
'   Set the range of names to look through
    Set nameRng = Range("A2:A6")
    
'   Get the value to compare to
    finRes = Range("B10")
    
'   Enter prefix in string
    str = "The final result has been correctly guessed by "
    
'   Loop through all cells
    For Each cell In nameRng
'       Check to see if guess matches final result
        If cell.Offset(0, 1).Value = finRes Then
'           Add name to string
            str = str & cell & ", "
        End If
    Next cell
    
'   Check to see if last two characters are ", "
    If Right(str, 2) = ", " Then
        str = Left(str, Len(str) - 2)
'       Check to see if any other commas are in string
        If InStr(str, ",") > 0 Then
            lc = InStrRev(str, ",")
            str = Left(str, lc - 1) & " &" & Mid(str, lc + 1)
        End If
    Else
        str = "No one guessed correctly"
    End If
    
'   Return message
    MsgBox str
    
End Sub
 
Upvote 0
Thanks snakehips for responding,

I forgot to mentioned that there will always be at least one match in the result. So, we don't need to check if the match found is "0".

Secondly, when I tried your code,

the below result shows comma before the first name.
1587558690686.png



And the below result when only one match was found, the name is preceded by "&".
1587558715577.png
 
Upvote 0
A little late but I think this sorts it too

VBA Code:
Sub Quiz()
Dim Guesses, Result As Range
Dim Winners, x As String
Dim Correct, c As Integer
Set Guesses = Range("B2:B6")
Set Result = Range("B9")

c = 0
Correct = WorksheetFunction.CountIf(Guesses, Result)

For Each cell In Guesses.Cells
    If cell = Result Then
    c = c + 1
    
    Select Case c
        Case 1
        x = ""
        Case Correct
        x = " &"
        Case Else
        x = ","
    End Select
         
        Winners = Winners & x & " " & cell.Offset(0, -1)
      Debug.Print Winners
    End If

Next
MsgBox "The final result has been guessed by" & Winners & "!", vbOKCancel, "RESULT!"

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,113
Messages
6,128,905
Members
449,478
Latest member
Davenil

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