How would I use VBA to find non matching values and input missing values from one workbook to another?

Indominus

Board Regular
Joined
Jul 11, 2020
Messages
160
Office Version
  1. 2016
Platform
  1. Windows
Hello. So I have two workbooks that are used for this project. One referenced as pickorder. Another referenced as Routing. Sheet is DWP. Screenshots are attached. The pickorder is the one that needs to be correct since it gets uploaded to a site. Both lists all route codes in column B. . Sometimes however, a couple are not in the Pickorder but they are in dwp.

So what I need is to identify all values in the DWP sheet in Range("B2:B500"). Then identity all matching values in Range("B2:B500") of Pickorder. If it detects one not in Pickorder then input that missing value into the next open row on the Pickorder workbook in column B. So in this case MX115 and MX116. I also attached a screenshot showing what the final result will look like on the Pickorder sheet after. Thank you to anyone willing to help.
 

Attachments

  • po.JPG
    po.JPG
    19.3 KB · Views: 14
  • dwpmissing.JPG
    dwpmissing.JPG
    20.4 KB · Views: 15
  • pickorder (final result).JPG
    pickorder (final result).JPG
    18.1 KB · Views: 14
Try:
VBA Code:
For Each WB In Application.Workbooks
        If WB.Name Like "*PickOrder*" Then
            Set desWS = WB.Sheets(1)
        End If
    Next WB

This seems to work to identify the Pickorder workbook. The code runs however, it does nothing. The missing route codes are not pasted into the bottom of the Pickorder workbook. In column B
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try:
VBA Code:
Sub FindUnplannedRoutes()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWS As Worksheet, WB As Workbook, arr1 As Variant, arr2 As Variant, Val As String
    Set srcWS = ThisWorkbook.Sheets("DWP")
    For Each WB In Application.Workbooks
        If WB.Name Like "*PickOrder*" Then
            Set desWS = WB.Sheets(1)
        End If
    Next WB
    arr1 = srcWS.Range("B2", srcWS.Range("B" & Rows.Count).End(xlUp)).Value
    arr2 = desWS.Range("B2", desWS.Range("B" & Rows.Count).End(xlUp)).Value
    Set rnglist = CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(arr2, 1)
        Val = arr2(i, 1)
        If Not rnglist.Exists(Val) Then
            rnglist.Add Val, Nothing
        End If
    Next i
    For i = 1 To UBound(arr1, 1)
        Val = arr1(i, 1)
        If Not rnglist.Exists(Val) Then
            With desWS
                .Cells(.Rows.Count, "B").End(xlUp).Offset(1) = Val
            End With
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Sub FindUnplannedRoutes()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWS As Worksheet, WB As Workbook, arr1 As Variant, arr2 As Variant, Val As String
    Set srcWS = ThisWorkbook.Sheets("DWP")
    For Each WB In Application.Workbooks
        If WB.Name Like "*PickOrder*" Then
            Set desWS = WB.Sheets(1)
        End If
    Next WB
    arr1 = srcWS.Range("B2", srcWS.Range("B" & Rows.Count).End(xlUp)).Value
    arr2 = desWS.Range("B2", desWS.Range("B" & Rows.Count).End(xlUp)).Value
    Set rnglist = CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(arr2, 1)
        Val = arr2(i, 1)
        If Not rnglist.Exists(Val) Then
            rnglist.Add Val, Nothing
        End If
    Next i
    For i = 1 To UBound(arr1, 1)
        Val = arr1(i, 1)
        If Not rnglist.Exists(Val) Then
            With desWS
                .Cells(.Rows.Count, "B").End(xlUp).Offset(1) = Val
            End With
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

This works perfectly!!! Thank you so much. It is very much appreciated.
 
Upvote 0

Forum statistics

Threads
1,216,058
Messages
6,128,539
Members
449,457
Latest member
ncguzzo

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