I have a macro that is comparing previous week's data with current weeks data to find differences. There are 7 sheets in the workbook:
New Raw Data (current weeks data imported),
NPI < 10 digits (looking to see if the NPI is less than 10 digits from current week's data), column B should have formula =len(a2) --> not recognizing last row in column A so has 0 after last row
Duplicate NPI (looking to see if there are duplicate NPI numbers in current week's data - conditional formatting to show any duplicates in red is on but the filter button is not on column A,
Blank NPI (looking for providers with no NPI) - is recognizing last row of data
Old Raw Data (previous week's data)
old not in new (comparing to see what differences are from previous vs current) - is recognizing last row of data
new not in old (comparing to see what differences are from current vs previous) - not recognizing last row of data
New Raw Data (current weeks data imported),
NPI < 10 digits (looking to see if the NPI is less than 10 digits from current week's data), column B should have formula =len(a2) --> not recognizing last row in column A so has 0 after last row
Duplicate NPI (looking to see if there are duplicate NPI numbers in current week's data - conditional formatting to show any duplicates in red is on but the filter button is not on column A,
Blank NPI (looking for providers with no NPI) - is recognizing last row of data
Old Raw Data (previous week's data)
old not in new (comparing to see what differences are from previous vs current) - is recognizing last row of data
new not in old (comparing to see what differences are from current vs previous) - not recognizing last row of data
VBA Code:
Sub Macro15()
Dim lr As Long
Sheets(Array("NPI < 10 digits", "Duplicate NPI", "Blank NPI", "Old Raw Data", _
"old not in new", "new not in old")).Select
Sheets("Blank NPI").Activate
Cells.Select
Selection.Delete Shift:=xlUp
Sheets("New Raw Data").Select
Cells.Select
Range("U1").Activate
Selection.Copy
Sheets("Old Raw Data").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("New Raw Data").Select
Cells.Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A1").Select
Dim ws As Worksheet, strFile As String
Set ws = ActiveWorkbook.Sheets("New Raw Data") 'set to current worksheet name
strFile = Application.GetOpenFilename
With ws.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
.TextFileParseType = xlDelimited
.TextFileOtherDelimiter = "|"
.Refresh
End With
Selection.AutoFilter
Range("A1").Select
Columns("AB:AB").Select
ActiveWorkbook.Worksheets("New Raw Data").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("New Raw Data").AutoFilter.Sort.SortFields.Add _
Key:=Range("AB:AB"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("New Raw Data").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("New Raw Data").Range("AB:AB").Copy Destination:=Sheets("NPI < 10 digits").Range("A1")
Sheets("NPI < 10 digits").Select
Range("A:A").Select
Selection.AutoFilter
Range("A1").Select
lr = Range("B" & Rows.Count).End(3).Row
Range("B1").Select
ActiveCell.FormulaR1C1 = "NPI Count"
Range("B2").Select
ActiveCell.FormulaR1C1 = "=LEN(RC[-1])"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B" & lr)
Range("B2:B" & lr).Select
Range("B1").Select
Selection.AutoFilter
ActiveSheet.Range("A:B").AutoFilter Field:=2, Criteria1:="<10", _
Operator:=xlAnd
Sheets("New Raw Data").Range("AB:AB").Copy Destination:=Sheets("Duplicate NPI").Range("A1")
Sheets("Duplicate NPI").Select
Columns("A:A").Select
Selection.FormatConditions.AddUniqueValues
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).DupeUnique = xlDuplicate
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Sheets("New Raw Data").Range("A:AK").Copy Destination:=Sheets("Blank NPI").Range("A1")
Sheets("Blank NPI").Select
Range("A1").Select
Selection.AutoFilter
Sheets("New Raw Data").Range("A:AK").Copy Destination:=Sheets("new not in old").Range("A1")
Sheets("new not in old").Select
Range("A1").Select
Selection.AutoFilter
Sheets("Old Raw Data").Range("A:AK").Copy Destination:=Sheets("old not in new").Range("A1")
Sheets("old not in new").Select
Range("A1").Select
Selection.AutoFilter
Sheets("old not in new").Select
Range("A1").Select
Sheets("new not in old").Select
Columns("AB:AB").Select
Selection.Cut
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Range("A1").Select
ActiveWorkbook.Worksheets("new not in old").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("new not in old").Sort.SortFields.Add Key:=Range( _
"A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("new not in old").Sort
.SetRange Range("A2:AK" & lr)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
lr = Range("A" & Rows.Count).End(3).Row
Selection.AutoFilter
Selection.AutoFilter
ActiveWorkbook.Worksheets("new not in old").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("new not in old").AutoFilter.Sort.SortFields.Add Key _
:=Range("A1:A" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("new not in old").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
lr = Range("A" & Rows.Count).End(3).Row
Sheets("old not in new").Select
Selection.AutoFilter
Columns("AB:AB").Select
Selection.Cut
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Range("A1").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("old not in new").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("old not in new").AutoFilter.Sort.SortFields.Add Key _
:=Range("A1:A" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("old not in new").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
lr = Range("B" & Rows.Count).End(3).Row
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B1").Select
Sheets("new not in old").Select
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B1").Select
Sheets("old not in new").Select
Range("B2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],'new not in old'!C[-1],1,FALSE)"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B" & lr)
Range("B2:B" & lr).Select
Sheets("new not in old").Select
Range("B2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],'old not in new'!C[-1],1,FALSE)"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B" & lr)
Range("B2:B" & lr).Select
Range("B1").Select
End Sub