Sub countpanel() ' Count number of time series in each panel unit ' Foe example, the entries are as follows in rows ' 113 ' 113 ' 115 ' 115 ' 115 ' 115 ' 123 ' From this entries, This macro makes ' 113 2 ' 115 4 ' 123 1 Dim iRowt As Long 'Row number for the top of the selected region Dim iColl As Long 'Column number for the far-left of the selected region Dim iRowb As Long 'Row number for the bottom of the selected region Dim iColr As Long 'Column number for the far-right of the selected region 'We do not use iColr in this macro Dim i As Integer 'For Row index Dim j As Integer 'For counting observations Dim myRng As Range Set myRng = Selection With myRng iRowt = .Row iColt = .Column iRowb = .Rows.Count iColr = .Columns.Count End With 'Caclulate the address of bottom row and far-right column of the selected area iRowb = iRowt + iRowb - 1 iColr = iColt + iColr - 1 j = 0 ' Starting value for counting each classification: 113, etc ' Obviously, start from zero k = iRowt + 1 ' k is the first row for the list. Can be any integer. ' Here I started from the row number of original entry ' Since the first row is the "index" of the daa, I start from iRows + 1 For i = iRowt + 1 To iRowb Firm_index = Cells(i, iColt).Value Firm_index1 = Cells(i + 1, iColt).Value ' obtaining the number (firm index) in each cell If Firm_index1 <> "" And Firm_index1 = Firm_index Then ' The first part orders: Stop the macro if the entry in cell (i, iColt) is empty. j = j + 1 ' counting the observations in each firm index ElseIf Firm_index1 <> Firm_index Then j = j + 1 Sheets("number").Cells(k, iColt) = Firm_index Sheets("number").Cells(k, iColt + 1) = j ' number of observation k = k + 1 j = 0 End If Next i Sheets("number").Cells(iRowt, iColt) = Cells(iRowt, iColt) ' Copy the index of data. Sheets("number").Cells(iRowt, iColt + 1) = "Number of Observations" Sheets("number").Activate End Sub