matching of data to not match the same data again

studentlearner

New Member
Joined
Oct 7, 2021
Messages
30
Office Version
  1. 365
Platform
  1. Windows
So currently I got multiple guys name john in my excel, how do I ensure that my vba doesn't go through the same cell over and over again?

this the code I'm working with

VBA Code:
Sub match_Data()

Dim rSH As Worksheet
Dim sSh As Worksheet
Set rSH = ThisWorkbook.Sheets("Master")
Set sSh = ThisWorkbook.Sheets("Schedule")

Dim fName As String, lName As String

For a = 2 To sSh.Range("A" & Rows.Count).End(xlUp).Row
    fName = sSh.Range("B" & a).Value

   
    For b = 2 To rSH.Range("A" & Rows.Count).End(xlUp).Row
        If rSH.Range("B" & b).Value = fName Then
               
            sSh.Range("A" & a).Value = rSH.Range("A" & b).Value
            sSh.Range("B" & a).Value = rSH.Range("B" & b).Value
            sSh.Range("C" & a).Value = rSH.Range("C" & b).Value

           
       
           
            Exit For
        End If
    Next b
Next a

Debug.Print "Completed"

End Sub


So currently it only calls upon and loop only on the first one only. How do I make it go through all john instead of it just locating one john?
Code IDNameSchedule
1010​
JohnAM
1011​
JohnPM
1033​
MickAM
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this . Note I have loaded all the data into variant arrays which will make the loops much much faster, and it also allows me to delete the name after the first match, because it doesn't delete iton the worksheet.
VBA Code:
Sub match_Data()

Dim rSH As Worksheet
Dim sSh As Worksheet
Set rSH = ThisWorkbook.Sheets("Master")
Set sSh = ThisWorkbook.Sheets("Schedule")

Dim fName As String, lName As String
lastssh = sSh.Range("A" & Rows.Count).End(xlUp).Row
lastrsh = rSH.Range("A" & Rows.Count).End(xlUp).Row
sSharr = sSSH.Range(Cells(1, 1), Cells(lastssh, 3)) ' load all the data in variant arraays
rSHarr = RSSH.Range(Cells(1, 1), Cells(lastrsh, 3))

For a = 2 To lastssh
    fName = sSharr(a, 2)

   
    For b = 2 To lastrsh
        If rSHarr(b, 2) = fName Then
               
            sSharr(a, 1) = rSHarr(b, 1)
            sSharr(a, 2) = rSHarr(b, 2)
            sSharr(a, 3) = rSHarr(b, 3)
            rSHarr(b, 2) = ""  ''''''''''' this is the key line that delete the name you have just matched. Note it doesn't delete on the workhseet
            Exit For
        End If
    Next b
Next a
sSSH.Range(Cells(1, 1), Cells(lastssh, 3)) = aSharr  ' wrtie the results back

Debug.Print "Completed"

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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