Comparing Two Columns with VBA - accounting for no differences

amoscoso

New Member
Joined
Aug 15, 2017
Messages
1
Hi,

I have a macro that is able to find differences between two columns that are side by side and then spits out the differences onto another sheet. The macro works well when there are differences, but if the two columns are the same, then I get a 400 error. What do I need to do so that my code essentially has an "iferror" built it or so that if there are no differences between the columns, I can have a pop up that says "No new plan and program names" instead of getting an error? Thank you!

Dim LastRow As Long
Dim LastRowB As Long


Application.ScreenUpdating = False




Set ws1 = Sheets("Plan and Program Assign Metrics")
Set ws2 = Sheets("Plan and Program Database")
Set ws3 = Sheets("Plan and Program Pivot Table")


ws3.Visible = xlSheetVisible
ws2.Visible = xlSheetVisible


ws3.PivotTables("ProgramTable").PivotCache.Refresh


LastRowB = Cells(Rows.Count, "B").End(xlUp).Row
LastRow = Cells(Rows.Count, "A").End(xlUp).Offset(-1).Row


Application.CutCopyMode = False




ws3.Range("B6:B" & LastRowB).Clear
ws2.Range("A3:A" & LastRow).Copy
ws3.Range("B6").PasteSpecial Paste:=xlPasteValues




ar = Range("a6").CurrentRegion 'Change input to suit


ReDim var(1 To UBound(ar, 1), 1 To 1)


With CreateObject("scripting.dictionary")
.comparemode = 1
For i = 1 To UBound(ar, 1)
.Item(ar(i, 2)) = Empty
Next
For i = 1 To UBound(ar, 1)
If Not .exists(ar(i, 1)) Then
n = n + 1
var(n, 1) = ar(i, 1)
End If
Next
End With
ws1.[A4].Resize(n).Value = var 'Change output to suit


ws3.Visible = xlSheetHidden
ws2.Visible = xlSheetHidden
ws1.Activate


Application.ScreenUpdating = True


MsgBox "Raw Data Refreshed and New Plans and Programs Listed"




End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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