Private Sub CommandButton1_Click()
Dim i As Integer
Dim finalRow, uqfinalRow As Long 'finalRow for the email Table, uqfinalRow for the unique company code values
finalRow = Range("E" & Rows.Count).End(xlUp).Row
For i = 2 To finalRow
Range("A" & i).Select 'Lookup for Company Code
ActiveCell.FormulaR1C1 = "=XLOOKUP(RC[3],C[11],C[10],""Not Found"",0)"
Range("B" & i).Select 'Lookup for First Name
ActiveCell.FormulaR1C1 = _
"=PROPER(IF(IF(ISERROR(FIND(""."",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1))))),TRUE)=FALSE,LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""."",RC5))))-1),IF(IF(IF(ISERROR(FIND(""."",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1))))),TRUE)=TRUE,IF(ISERROR(FIND(""_"",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1)" & _
")))),TRUE))=FALSE,LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""_"",RC5))))-1),IF(IF(IF(IF(ISERROR(FIND(""."",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1))))),TRUE)=TRUE,IF(ISERROR(FIND(""_"",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1))))),TRUE))=TRUE,IF(ISERROR(FIND(""-"",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND" & _
"(""@"",RC5))))-1))))),TRUE))=FALSE,LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""-"",RC5))))-1),LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1)))))" & ""
Range("C" & i).Select 'Lookup for Last Name
ActiveCell.FormulaR1C1 = _
"=PROPER(IF(IF(ISERROR(FIND(""."",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1))))),TRUE)=FALSE,RIGHT(LEFT(RC5,FIND(""@"",RC5)-1),LEN(LEFT(RC5,FIND(""@"",RC5)-1))-FIND(""."",RC5)),IF(IF(IF(ISERROR(FIND(""."",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1))))),TRUE)=TRUE,IF(ISERROR(FIND(""_"",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-F" & _
"IND(""@"",RC5))))-1))))),TRUE))=FALSE,RIGHT(LEFT(RC5,FIND(""@"",RC5)-1),LEN(LEFT(RC5,FIND(""@"",RC5)-1))-FIND(""_"",RC5)),IF(IF(IF(IF(ISERROR(FIND(""."",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1))))),TRUE)=TRUE,IF(ISERROR(FIND(""_"",((LEFT(RC5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1))))),TRUE))=TRUE,IF(ISERROR(FIND(""-"",((LEFT(R" & _
"C5,LEN(RC5)-LEN((RIGHT(RC5,LEN(RC5)-FIND(""@"",RC5))))-1))))),TRUE))=FALSE,RIGHT(LEFT(RC5,FIND(""@"",RC5)-1),LEN(LEFT(RC5,FIND(""@"",RC5)-1))-FIND(""-"",RC5)),""""))))" & ""
Range("D" & i).Select 'Lookup for Company Name
ActiveCell.FormulaR1C1 = "=PROPER(LEFT(REPLACE(RC[1],1,FIND(""@"",RC[1]),""""),FIND(""."",REPLACE(RC[1],1,FIND(""@"",RC[1]),""""))-1))"
Next
'Copy the filter results (Unique Company names) from column D to column L
Range("D:D").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("L1"), Unique:=True
Range("L:L").Sort Key1:=Range("L2"), order1:=xlAscending, Header:=xlNo 'Sort thr result
'Count the unique company names
uqfinalRow = Range("L" & Rows.Count).End(xlUp).Row 'Count the number of unique values (company names)
' '''''Auto Sr. Quotation Table
Range("K2").Activate 'Starting cell
For i = 1 To uqfinalRow - 1
If ActiveCell.Offset(-1, 0).Value = "Code" Then 'If you find "code" in the K1
ActiveCell.Offset(0, 0).Value = "1" 'Start the serial number from 1 in K2
Else
ActiveCell.Offset(0, 0).Value = "=@INDIRECT(ADDRESS(ROW()-1,COLUMN()))+1" 'Add +1 on the above number
End If
ActiveCell.Offset(1, 0).Select 'Select the next cell in the row
Next i
Range("B2:D" & finalRow).Select 'Copy and paste values for the range A to D (company code, F name, L name, company name)
Selection.Copy
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
Range("K2:K" & uqfinalRow).Select 'Copy and paste values for the range K (company code, company name)
Selection.Copy
Range("K2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
Range("A" & finalRow).Offset(1, 0).Select
End Sub