Sub countHH() ' Revised on 2014.10.12 ' Count the number of household members ' For example, the entries are as follows in rows ' 113 ' 113 ' 115 ' 115 ' 115 ' 115 ' 123 ' From this entries, in the sheet "number", this macro makes ' 113 2 ' 115 4 ' 123 1 Dim mySht1 As Worksheet Dim Ly As Integer Dim i As Integer Dim j As Integer Dim k As Integer ' Dim l As Integer Dim h As Integer Set mySht1 = Sheets("Base") ' Indicate the sheet you would like to work with Ly = Selection.Rows.Count ' Count the number of selected rows ' Count from rows title: Row 12. j = 0 k = 5 ' l = 0 h = 2 ' k is just indicating the row for recording household-wise index in the sheet "numer" ' l is for recording number of HH members in production age 16>= and 65 > ' h is for indicating the column with househod index. In this file, the HH index in in column B (2). For i = 13 To Ly + 13 ' Data starts from Row 13 ' This macro assumes that the data starts from the 13th line. Mb_index = Cells(i, h).Value Mb_index1 = Cells(i + 1, h).Value If Mb_index1 <> "" And Mb_index = Mb_index1 Then j = j + 1 'count the number of HH members ' If Cells(i, 11) > 15 And Cells(i, 11) < 65 Then ' l = l + 1 ' End If 'count the number of HH members with age between 16 and 64 ElseIf Mb_index <> Mb_index1 Then j = j + 1 'for counting the sole member of the HH. Remember j starts from "0" ' If Cells(i, 11) > 15 And Cells(i, 11) < 65 Then ' l = l + 1 ' End If 'Do not miss to count the last row Sheets("number").Cells(k, 2) = Mb_index 'household ID Sheets("number").Cells(k, 3) = j ' number of observation ' Sheets("number").Cells(k, 4) = l ' number of members bewteen 16 and 64 k = k + 1 j = 0 ' l = 0 End If mySht1.Activate Next i End Sub