excel vba two way match, can this be tweaked to use two way matching?

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
So I had a great deal of help coming up with this code which works flawlessly but I was wanting to know if anyone can see a way to do two changes, 1) Make it match both column A and column L so be a match and B if no match is found then add the record to the next available row?

Thanks in advance

Code:
Sub UpdateRun()    Application.ScreenUpdating = False
    Application.EnableEvents = False
	Dim Wb As Workbook, Wbk As Workbook, Ws As Worksheet, Nws As Worksheet, RDPiv As Worksheet
		Set Wb = ThisWorkbook
		Set Ws = Wb.Sheets("Today")
		
    If Ws.Range("E3") > 0 Then
    
            Set Wbk = Workbooks.Open(Fl.Range("B9").Value)
            Set Nws = Wbk.Sheets("Data")
            Set RDPiv = Wbk.Sheets("Pivot")
    Dim path As String, Val As String, i As Long, v1, v2
                
'----------------------------Update status, cause, and comments for current records---------------------------
    v1 = Ws.Range("A3", Ws.Range("A" & Rows.Count).End(xlUp)).Value
    v2 = Nws.Range("A3", Nws.Range("A" & Rows.Count).End(xlUp)).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v2, 1)
            Val = v2(i, 1)
            If Not .Exists(Val) Then
                .Add Val, i + 2
            End If
        Next i
        For i = 1 To UBound(v1, 1)
            Val = v1(i, 1)
            If .Exists(Val) Then
                Nws.Cells(Nws.Range("A:A").Find(Val, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row, "B") = Ws.Cells(i + 2, "B")
                Nws.Cells(Nws.Range("A:A").Find(Val, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row, "C") = Ws.Cells(i + 2, "C")
                Nws.Cells(Nws.Range("A:A").Find(Val, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row, "T") = Ws.Cells(i + 2, "T")
            End If
        Next i
    End With
    '-----------------Refresh's Pivot table data-----------------------
   RDPiv.Activate
   ActiveWorkbook.RefreshAll
      Wbk.Close True
    Wb.Save
    End If
    Wb.RefreshAll
    Application.EnableEvents = True
    Application.ScreenUpdating = True


    End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
How about
Code:
   v1 = Ws.Range("A3", Ws.Range("A" & Rows.Count).End(xlUp).Offset(, 11)).Value
   v2 = Nws.Range("A3", Nws.Range("A" & Rows.Count).End(xlUp).Offset(, 11)).Value
   With CreateObject("Scripting.Dictionary")
      For i = 1 To UBound(v2, 1)
          Val = v2(i, 1) & "|" & v2(i, 12)
          If Not .Exists(Val) Then
              .Add Val, i + 2
          End If
      Next i
      For i = 1 To UBound(v1, 1)
         Val = v1(i, 1) & "|" & v1(i, 12)
         If .Exists(Val) Then
             Nws.Cells(.Item(Val), "B") = Ws.Cells(i + 2, "B")
             Nws.Cells(.Item(Val), "C") = Ws.Cells(i + 2, "C")
             Nws.Cells(.Item(Val), "T") = Ws.Cells(i + 2, "T")
         Else
            Nws.Range("A" & Rows.Count).End(xlUp).Offset(1).Value = v1(i, 1)
         End If
      Next i
   End With
 
Upvote 0
The update seems to work perfectly but adding the records is only adding the first column, I need to add A:X when the record isn't there.

Yours looks simpler than mine BTW....... One Day Fluff, I told you I hope to be like you.

So How would you tweak it to encompass A:X when adding a new record?
 
Upvote 0
Try
Code:
            Ws.Rows(i + 2).Copy Nws.Range("A" & Rows.Count).End(xlUp).Offset(1)
 
Upvote 0
Hmmmmm This brings everything including formatting etc and causes a bunch of pop ups......
 
Upvote 0
Ok, how about
Code:
           Nws.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 25).Value = Ws.Range("A" & i + 2).Resize(, 25).Value
 
Upvote 0
Thats It Man, You are awesome!!!!!

For anyone interested heres the final codes.
Thanks Fluff

Code:
Sub UpdateRun()    Application.ScreenUpdating = False
             Call Dec
    If Ws.Range("E3") > 0 Then
    
            Set Wbk = Workbooks.Open(Fl.Range("B9").Value)
            Set Nws = Wbk.Sheets("Data")
            Set RDPiv = Wbk.Sheets("Pivot")
    Dim path As String, Val As String, i As Long, v1, v2
                
'----------------------------Update status, cause, and comments for current records---------------------------
 v1 = Ws.Range("A3", Ws.Range("A" & Rows.Count).End(xlUp).Offset(, 11)).Value
   v2 = Nws.Range("A3", Nws.Range("A" & Rows.Count).End(xlUp).Offset(, 11)).Value
   With CreateObject("Scripting.Dictionary")
      For i = 1 To UBound(v2, 1)
          Val = v2(i, 1) & "|" & v2(i, 12)
          If Not .Exists(Val) Then
              .Add Val, i + 2
          End If
      Next i
      For i = 1 To UBound(v1, 1)
         Val = v1(i, 1) & "|" & v1(i, 12)
         If .Exists(Val) Then
             Nws.Cells(.Item(Val), "B") = Ws.Cells(i + 2, "B")
             Nws.Cells(.Item(Val), "C") = Ws.Cells(i + 2, "C")
             Nws.Cells(.Item(Val), "T") = Ws.Cells(i + 2, "T")
         Else
           Nws.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 24).Value = Ws.Range("A" & i + 2).Resize(, 24).Value
         End If
      Next i
   End With
    '-----------------Refresh's Pivot table data-----------------------
   RDPiv.Activate
   ActiveWorkbook.RefreshAll
      Wbk.Close True
    Wb.Save
    End If
    Wb.RefreshAll
    Application.ScreenUpdating = True


    End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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