I've written code to compare the data included in 2 huge Excel files and while running the comparison code I encounter a "Run time error '91' - Object Variable or With block variable not set". Anyone know what my error is? The debug button leads me to the code bolded and underlined below:
'Copy/paste values for new count column and copy format of last column to new count column
Columns("AG:AG").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Columns("AF:AF").Select
Application.CutCopyMode = False
Selection.Copy
Columns("AG:AG").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Old Data").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Old Data").AutoFilter.Sort.SortFields.Add Key:=Range("AG1:AG" & lngCount_total), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Old Data").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("AG1").Select
'Copy/paste values for new count column and copy format of last column to new count column
Columns("AG:AG").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Columns("AF:AF").Select
Application.CutCopyMode = False
Selection.Copy
Columns("AG:AG").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Old Data").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Old Data").AutoFilter.Sort.SortFields.Add Key:=Range("AG1:AG" & lngCount_total), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Old Data").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("AG1").Select