I have a workbook in which I will import the current month's worksheet and run a code to compare with the previous month and return the differences onto a third worksheet. I was using the following code with no issues, then suddenly out of nowhere I get a "Runtime error '1004: Application-defined or object defined error". I have been tweaking the code to bring categories over from the "New Data" sheet (just to identify the categories on the results ("Changes") sheet), but I never changed anything that I can tell, that would rngC info. Debug highlights what I have in RED below. Any thoughts?
I also need to figure out how to get my italicized text that results from the code under 'Categories, to populate its own line. right now, it populates on the same rows with the changes.
I also need to figure out how to get my italicized text that results from the code under 'Categories, to populate its own line. right now, it populates on the same rows with the changes.
Code:
Sub CompareSheets()
[COLOR=#009933]' constants
' worksheets & ranges
' original[/COLOR]
Const ksWSOriginal = "OldData"
Const ksOriginal = "OriginalTable"
Const ksOriginalKey = "OriginalKey"
[COLOR=#009933]' updated[/COLOR]
Const ksWSUpdated = "NewData"
Const ksUpdated = "UpdatedTable"
Const ksUpdatedKey = "UpdatedKey"
[COLOR=#009933]' changes[/COLOR]
Const ksWSChanges = "CHANGES"
Const ksChanges = "ChangesTable"
[COLOR=#009933]' labels[/COLOR]
Const ksChange = "CHANGE"
Const ksRemove = "REMOVE"
Const ksAdd = "ADD"
[COLOR=#009933]'
' declarations[/COLOR]
Dim rngO As Range, rngOK As Range, rngU As Range, rngUK As Range, rngC As Range
Dim c As Range
Dim I As Long, J As Long, lChanges As Long, lRow As Long, bEqual As Boolean
Call OptimizeCode_Begin
[COLOR=#009933]' start[/COLOR]
Set rngO = Worksheets(ksWSOriginal).Range(ksOriginal)
Set rngOK = Worksheets(ksWSOriginal).Range(ksOriginalKey)
Set rngU = Worksheets(ksWSUpdated).Range(ksUpdated)
Set rngUK = Worksheets(ksWSUpdated).Range(ksUpdatedKey)
[COLOR=#ff0000]Set rngC = Worksheets(ksWSChanges).Range(ksChanges)[/COLOR]
With rngC
If .Rows.Count > 1 Then
Range(.Rows(2), .Rows(.Rows.Count)).ClearContents
Range(.Rows(2), .Rows(.Rows.Count)).Font.ColorIndex = xlColorIndexAutomatic
Range(.Rows(2), .Rows(.Rows.Count)).Font.Bold = False
End If
End With
[COLOR=#009933]' process[/COLOR]
lChanges = 1
[COLOR=#009933]'Categories[/COLOR]
With rngU
For I = 1 To .Rows.Count
If rngUK.Cells(I, 2).Font.Bold Then
For J = 1 To rngU.Columns.Count
rngC.Cells(I, J).Value = rngU.Cells(I, J).Value
rngC.Cells(I, J).Interior.Color = RGB(180, 180, 180)
rngC.Cells(I, J).Font.Color = vbBlack
rngC.Cells(I, J).Font.Bold = True
Next J
End If
Next I
End With
[COLOR=#009933]' 1st pass: updates & deletions[/COLOR]
With rngOK
For I = 1 To .Rows.Count
Set c = rngUK.Find(.Cells(I, 1).Value, , xlValues, xlWhole)
If c Is Nothing Then
[COLOR=#009933]' deletion[/COLOR]
lChanges = lChanges + 1
rngC.Cells(lChanges, 1).Value = ksRemove
For J = 1 To rngO.Columns.Count
rngC.Cells(lChanges, J + 1).Value = rngO.Cells(I, J).Value
rngC.Cells(lChanges, J + 1).Interior.Color = RGB(255, 0, 0)
rngC.Cells(lChanges, J + 1).Font.Color = vbWhite
rngC.Cells(lChanges, J + 1).Font.Bold = True
Next J
Else
bEqual = True
lRow = c.Row - rngUK.Row + 1
For J = 1 To rngO.Columns.Count
If rngO.Cells(I, J).Value <> rngU.Cells(lRow, J).Value Then
bEqual = False
Exit For
End If
Next J
If Not bEqual Then
[COLOR=#009933]' change[/COLOR]
lChanges = lChanges + 1
rngC.Cells(lChanges, 1).Value = ksChange
For J = 1 To rngO.Columns.Count
If rngO.Cells(I, J).Value = rngU.Cells(lRow, J).Value Then
rngC.Cells(lChanges, J + 1).Value = rngO.Cells(I, J).Value
Else
rngC.Cells(lChanges, J + 1).Value = rngU.Cells(lRow, J).Value
rngC.Cells(lChanges, J + 1).Interior.Color = RGB(153, 51, 0)
rngC.Cells(lChanges, J + 1).Font.Color = vbWhite
rngC.Cells(lChanges, J + 1).Font.Bold = True
End If
Next J
End If
End If
Next I
End With
[COLOR=#009933]' 2nd pass: additions[/COLOR]
With rngUK
For I = 1 To .Rows.Count
Set c = rngOK.Find(.Cells(I, 1).Value, , xlValues, xlWhole)
If c Is Nothing Then
[COLOR=#009933]' addition[/COLOR]
lChanges = lChanges + 1
rngC.Cells(lChanges, 1).Value = ksAdd
For J = 1 To rngU.Columns.Count
rngC.Cells(lChanges, J + 1).Value = rngU.Cells(I, J).Value
rngC.Cells(lChanges, J + 1).Interior.Color = RGB(0, 128, 0)
rngC.Cells(lChanges, J + 1).Font.Color = vbWhite
rngC.Cells(lChanges, J + 1).Font.Bold = True
Next J
End If
Next I
End With
[COLOR=#009933]'
' end[/COLOR]
Call OptimizeCode_End
Worksheets(ksWSChanges).Activate
rngC.Cells(2, 3).Select
Set rngC = Nothing
Set rngUK = Nothing
Set rngU = Nothing
Set rngOK = Nothing
Set rngO = Nothing
Beep
[COLOR=#009933]'[/COLOR]
End Sub
Sub OptimizeCode_Begin()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
EventState = Application.EnableEvents
Application.EnableEvents = False
PageBreakState = ActiveSheet.DisplayPageBreaks
ActiveSheet.DisplayPageBreaks = False
End Sub
Sub OptimizeCode_End()
ActiveSheet.DisplayPageBreaks = PageBreakState
Application.EnableEvents = EventState
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Sub ClearDataCompare()
Sheets("CHANGES").Select
Rows("2:1000").Select
Selection.ClearContents
Selection.ClearFormats
Range("B1").Select
End Sub