I have a list that is copied from one worksheet into a "calculation" sheet, and a second list that is copied from another worksheet into the same "calculation" sheet. Before my macro, I used a =VLOOKUP()
formula to determine if each item had a match in the other list, and visa versa. Right now my code cycles item by item.
Is there a more efficient/time saving way to get the same outcome? (I have a copy of this sub for the counter comparison -- this is A > B, other is B > A)
Here's the code:
Sub GPWireDifference()
'Establishes the Unmatched Great Plains Values list
Set BWGPValues = New Dictionary
'Creates a variable to check if Keys already exist in list
Dim lookup As String
'Creates a variable to store the unmatched amount
Dim amount As Currency
'Sets a variable to count the amount of items in the checked list
lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
'Format all columns in the Calculation sheet to fit their contents
Cells.EntireColumn.AutoFit
'Formatting the numbers to the common "currency" type
Range("B:E").NumberFormat = "$#,##0.00"
Range("D2").Activate
'In the event of the value not matching, send the chain to a separate segment
On Error GoTo ErrorHandler:
'Creates a loop to set the cell values to the results of the VLookup formula
Do Until ActiveCell.Offset(0, -3).Value = ""
ActiveCell.Value = Application.WorksheetFunction. _
IfError(Application.WorksheetFunction. _
VLookup(ActiveCell.Offset(0, -2), Range("C:C"), 1, False), 0)
ActiveCell.Offset(1, 0).Activate
Loop
'This error handler is to create a buffer so the macro doesn't lock itself into the
' error status... Unsure why, but if the buffer wasn't here, it breaks the code
ErrorHandler:
If Not ActiveCell.Offset(0, -3).Value = "" Then
GoTo ErrorHandler2:
End If
'This error handler sets the Key and Item for the list, and stores the values
ErrorHandler2:
If Not ActiveCell.Offset(0, -3).Value = "" Then
lookup = ActiveCell.Offset(0, -3).Value
amount = ActiveCell.Offset(0, -2).Value
'Checks to see if the Key already exists. If so, sets the item value to the
' sum of the existing value and the new value
If BWGPValues.Exists(lookup) Then
BWGPValues(lookup) = BWGPValues(lookup) + amount
Else 'If not, then it adds the key and the item values
BWGPValues.Add lookup, amount
End If
Resume Next 'Returns to the loop
End If
'Creates headers for the comparison rows
Range("D1").Value = "GP to Wires:"
Range("E1").Value = "Wires to GP:"
'Reformats the columns to fit all contents
Cells.EntireColumn.AutoFit
End Sub