vba - return unique value & its count

noidea23

New Member
Joined
Feb 16, 2022
Messages
28
Office Version
  1. 2021
Platform
  1. Windows
hello! using the dataset below,
1645521502315.png

how can i go about using vba to count the number of rows with the same Language and same month/year (day can be different). would like vba to be able to return the unique values and its count e.g. English Jan 2021 - count: 4.
Thank you!
 

Attachments

  • 1645521438904.png
    1645521438904.png
    20.7 KB · Views: 7

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I'm using the below MACRO to get the unique values in new sheet from selected range, also using formulas like sumif, vlookup & countif.
You may check to get your work done with some changes.

VBA Code:
Sub Unique_Values_Sumif()

Application.DisplayAlerts = False
Dim tb As Worksheet
Dim tb2 As Worksheet
Dim actvSheet As String

 actvSheet = ActiveSheet.Name
 Set tb = Sheets(actvSheet)
 
  Dim rSelection As Range, lColNo As Integer, formulalColNo As Integer, fColNo As Integer, formulafColNo As Integer
  '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
  
  Dim VLformulafColNo As Integer, VLformulalColNo As Integer, VLlookupColNo As Integer
  VLformulafColNo = fColNo - 3
  VLformulalColNo = lColNo - 3
  VLlookupColNo = Selection.Columns.Count
  
  
  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('" & actvSheet & "'!C[" & formulafColNo & "]:C[" & formulafColNo & "],RC[-1],'" & actvSheet & "'!C[" & formulalColNo & "]:C[" & formulalColNo & "])"
    End With
     'Autofit column
    .Columns("A").AutoFit
    .Range("B1").Formula = "=SUM(B3:B5000)"
    .Range("A1").Formula = "=COUNTA(A3:A5000)"
    .Range("A2").Value = "ART"
    .Range("B2").Value = "QTY"
    .Range("C2").Value = "VLOOKUP"
    .Range("D2").Value = "COUNT"
    
        With .Range(.Range("c3"), .Range("b3").End(xlDown).Offset(, 1))
        .FormulaR1C1 = "=VLOOKUP(RC[-2],'" & actvSheet & "'!C[" & VLformulafColNo & "]:C[" & VLformulalColNo & "]," & VLlookupColNo & "," & 0 & ")"
        End With
        
    With .Range(.Range("D3"), .Range("C3").End(xlDown).Offset(, 1))
        '------
        .FormulaR1C1 = "=COUNTIF('" & actvSheet & "'!C[" & formulafColNo - 2 & "]:C[" & formulafColNo - 2 & "],RC[-3])"
    End With

End With
Application.DisplayAlerts = True

Done:
Exit Sub

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top