Sub Unique_Values_Sumif()
'vba run or not confirmation
Dim chkMsg As String, chkAns As Variant
chkMsg = "This will generate the unique values/id in new sheet column A & get the total sum of last column of the slected range in new sheet colum B."
chkAns = MsgBox(chkMsg, vbYesNo)
Select Case chkAns
Case vbYes
'vba run or not confirmation
'--------------------------------------------------------------
'code here
Application.DisplayAlerts = False
Dim tb As Worksheet
Dim tb2 As Worksheet
Dim actvSheet As String
actvSheet = ActiveSheet.Name
'Set tb = Sheets("sheet1")
'Set tb2 = Sheets("sheet2")
Set tb = Sheets(actvSheet)
'Dim lrow As Long
Dim rSelection As Range, lColNo As Integer, formulalColNo As Integer, fColNo As Integer, formulafColNo As Integer
'lrow = tb.Cells(tb.Rows.Count, 1).End(xlUp).Row
'tb.Range("A2:A" & lrow).Copy tb2.Range("A2")
'Check that a range is selected
If Selection.Columns.count < 2 Then
MsgBox "Please select a range first, you have slected only 1 column, column must be at least 2", vbOKOnly, "Selecton Check"
Exit Sub
End If
'Store the selected range
Set rSelection = Selection
fColNo = rSelection.Column
formulafColNo = fColNo - 2
lColNo = rSelection(rSelection.count).Column
formulalColNo = lColNo - 2
Set tb2 = Worksheets.Add(Type:=xlWorksheet, After:=Application.ActiveSheet)
rSelection.Copy tb2.Range("A3")
On Error Resume Next
With tb2
.Range("A3:A5000").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
.Range("B1:Z5000").ClearContents
.Range("A3").CurrentRegion.RemoveDuplicates 1
With .Range(.Range("B3"), .Range("A3").End(xlDown).Offset(, 1))
'.FormulaR1C1 = "=SUMIF(Sheet1!C[-1]:C[-1],RC[-1],Sheet1!C:C)"
.FormulaR1C1 = "=SUMIF('" & actvSheet & "'!C[" & formulafColNo & "]:C[" & formulafColNo & "],RC[-1],'" & actvSheet & "'!C[" & formulalColNo & "]:C[" & formulalColNo & "])"
End With
'Delete column
'.Range("C1:Z1").EntireColumn.Delete
'Autofit column
.Columns("A").AutoFit
.Range("B1").Formula = "=SUM(B3:B5000)"
.Range("A1").Formula = "=COUNTA(A3:A5000)"
End With
Application.DisplayAlerts = True
Done:
Exit Sub
'code here
'-----------------------------------------------------------------
'vba run or not confirmation
Case vbNo
GoTo Quit:
End Select
Quit:
'vba run or not confirmation
End Sub