.CompareMode = 1
For i = 1 To UBound(x)
If Not .Exists(x(i, 1)) Then
.Item(x(i, 1)) = 1
If Not Evaluate("ISREF('" & x(i, 1) & "'!A1)") Then
Sheets.Add(after:=Sheets(Sheets.Count)).Name = x(i, 1)
End If
End If
Next i: x = .keys
End With
what is the compare mode and what are the keys?
Option Explicit
Sub consolidateData()
Const SHEET1_NAME = "Sheet1"
Const SHEET2_NAME = "Sheet2"
Const FINAL_SHEET_NAME = "Final"
Dim final As Worksheet
Dim lastCell As Range
Dim i As Long, j As Long
Dim dbc As Variant
Dim shp As shape
Application.ScreenUpdating = False
On Error Resume Next
Set final = Sheets(FINAL_SHEET_NAME)
On Error GoTo 0
If Not final Is Nothing Then
Application.DisplayAlerts = False
final.Delete
Application.DisplayAlerts = True
End If
Sheets(SHEET1_NAME).Copy after:=Sheets(Sheets.Count)
Set final = Sheets(Sheets.Count)
final.Name = FINAL_SHEET_NAME
With final
.Range("F2").Value = "NATURE OF PYT"
Set lastCell = .Cells(.Rows.Count, 1).End(xlUp)
.Columns("G:H").Insert xlToLeft
.Range("G2:H2").Value = Array("INVOICE NUMBER", "OUTSTANDING AMOUNT")
.Range(lastCell(2, 1), .Cells.SpecialCells(xlCellTypeLastCell)).Clear
For i = lastCell.Row To 3 Step -1
With .Cells(i, "F")
If InStr(1, .Value, ",") > 0 Then
dbc = Split(.Value, ",")
.Offset(1).EntireRow.Insert
For j = 1 To UBound(dbc)
.EntireRow.Copy
.Offset(1).EntireRow.Insert xlDown
.Offset(1).Value = Trim(dbc(j))
.Offset(1).Font.Color = vbBlue
.Font.Size = 14
.Offset(1).Font.Size = 16
Next j
.Value = Trim(dbc(0))
.Font.Color = vbBlue
.Offset(UBound(dbc) + 1, 10).Formula = _
"=SUM(" & .Offset(0, 2).Resize(UBound(dbc) + 1).Address & ")"
Else
.Value = Trim(.Value)
End If
End With
Next i
Application.CutCopyMode = False
Set lastCell = .Cells(.Rows.Count, "F").End(xlUp)
With .Range("G3", .Cells(lastCell.Row, "G"))
.Formula = _
"=IFERROR(IF(F3="""","""",VLOOKUP(TEXT(F3,0)," & _
SHEET2_NAME & "!$A:$I,9,FALSE)),""NOT FOUND"")"
.Value = .Value
End With
With .Range("H3", .Cells(lastCell.Row, "H"))
.Formula = _
"=IFERROR(IF(F3="""",IF(P3="""","""",P3),VLOOKUP(TEXT(F3,0)," & _
SHEET2_NAME & "!$A:$I,6,FALSE)),""NOT FOUND"")"
.Value = .Value
End With
With .Range("E3", .Cells(lastCell.Row, "E"))
.Formula = "=TRIM(IFERROR(MID(G3,FIND(""^^"",SUBSTITUTE(G3,""/"",""^^""," & _
"LEN(G3)-LEN(SUBSTITUTE(G3,""/"",""""))))+1,LEN(G3)),""""))"
.Value = .Value
End With
.Columns("F").Interior.ColorIndex = 0
.Columns("P").ClearContents
For Each shp In .Shapes
If shp.Type = 8 Then
shp.Delete
End If
Next shp
End With
Application.ScreenUpdating = True
Call Uppercase
Call notfound
Call nilemmagic
End Sub
Sub Uppercase()
Dim x As Variant
Dim LR As Long
LR = Cells(Rows.Count, "E").End(xlUp).Row 'Last row number
For x = LR To 3 Step -1
'Loop to cycle through each cell in the specified range.
Range("E" & x).Value = UCase(Range("E" & x).Value) 'Change the text in the range to uppercase letters.
If Range("A" & x).Value = "" Then 'Check is value in A row empty
Range("A" & x).EntireRow.Delete 'Delete entire row
End If
Next x
End Sub
Sub notfound()
Dim x As Variant
Dim LR As Long
LR = Cells(Rows.Count, "E").End(xlUp).Row 'Last row number
For x = LR To 3 Step -1
'Loop to cycle through each cell in the specified range.
If Range("E" & x).Value = "" Then 'Check whether Value in E is empty
Range("E" & x).Value = "Not Found " 'Puts "Not Found in In it "
End If
Next x
End Sub
Sub nilemmagic()
Dim x, i&: Application.ScreenUpdating = 0
With Sheets("Final")
x = .Range("E3", .Cells(Rows.Count, 5).End(xlUp)).Value
End With
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 1 To UBound(x)
If Not .Exists(x(i, 1)) Then
.Item(x(i, 1)) = 1
If Not Evaluate("ISREF('" & x(i, 1) & "'!A1)") Then
Sheets.Add(after:=Sheets(Sheets.Count)).Name = x(i, 1)
End If
End If
Next i: x = .keys
End With
With Sheets("Final")
With .Range("A2:O" & .Cells(Rows.Count, 1).End(xlUp).Row)
.AutoFilter
For i = 0 To UBound(x)
.AutoFilter Field:=5, Criteria1:=x(i)
.SpecialCells(12).Copy Sheets(x(i)).Range("A2")
' Sheets(x(i)).Shapes(1).Delete
Next i
.AutoFilter
End With
End With: Application.ScreenUpdating = 1
End Sub
Humble regards ,
Bookmarks