Hi everyone,
I have a data set that is updated on regular basis by multiple users. Hence, the number of rows of this data set keeps on changing. However, the number of columns remain the same. The data set looks like following:
Please note that it is just a 2% of real data as the real data contains nearly 4000+ rows.
I needed few rows out of this database (required rows highlighted yellow in the above table) for my analysis. That is why I created a pivot table out of this data sheet to extract my desired columns. You can see pivot table below:
However, the tricky part in which I got stuck is to create duplicate rows for each single row present in this pivot table. The pivot table if you can see contains health facilities and each of this health facility has a unique "ID". What I intend to add is 23 more duplicate rows for each single ID. All the data of pivot table rows remain same in the final outcome sheet, except for two new columns of "Year" and "Month" added against each row (as shown in the below table). Year will contain 2020/2021 and month will contain all 12 months. In total 24 rows need to be created against each single ID through some vba command. Simplifying it further, we currently have 12 rows in pivot table or your can say that 12 unique ID's. We need to add 23 new rows against each ID and hence, total of 24*12=288 rows will be created along with year and month column. Below image shows the outcome for a single ID.
Will really appreciate your support to automate the task shown in the last table.
I have a data set that is updated on regular basis by multiple users. Hence, the number of rows of this data set keeps on changing. However, the number of columns remain the same. The data set looks like following:
ID | Ucode | Province | Division | District | CodeDiv_ID | Tehsil | UC | UCID | NameHF | AddressHF | PhoneHF | formal | IsActive | Freq | Level | Type | Sector | AssignedPersonName | IDsup | DaillyTurnover | DateIncluded | IsFocalPerson | FPName | FPPhone | HMISCode | Remarks |
9830 | 14203007 | PUNJAB | SARGODHA | KHUSHAB | 31 | QUAIDA ABAD | QUAIDA ABAD | 1776 | Mazhar Hospital Quaidabad | 0 | 1 | 1 | Quarterly | Secondary | Zero | Private | 1202 | 80 | 01-01-17 | 1 | Faisal Aziz | 454920089 | ||||
9371 | 14104017 | PUNJAB | SARGODHA | SARGODHA | 31 | SARGODHA | CHAK # 46 SB | 1644 | Iqra Medical complex | 0 | 0 | 1 | Quarterly | Primary | Zero | Private | Dr. Muhammad Riaz | 1202 | 65 | 01-01-17 | 1 | Hammad Khalil | 3006055995 | |||
9156 | 14302013 | PUNJAB | SARGODHA | MIANWALI | 31 | MAINWALI | MARI INDUS | 1806 | BHu Mari Indus | 0 | 1 | 1 | Quarterly | Primary | Zero | Government | DR MIAN KASHIF ALI | 1202 | 70 | 01-01-16 | 1 | Samiullah | 3339837516 | |||
9159 | 14302002 | PUNJAB | SARGODHA | MIANWALI | 31 | MAINWALI | BAN HAFAZ GI | 1795 | BHU Laloo Khel | 0 | 1 | 1 | Quarterly | Primary | Zero | Government | DR MIAN KASHIF ALI | 1202 | 65 | 01-01-16 | 1 | Samiullah | 3339837516 | |||
9158 | 14303001 | PUNJAB | SARGODHA | MIANWALI | 31 | PIPLAN | ALOWALI | 1823 | BHU Dhoke Ayub | 0 | 1 | 1 | Quarterly | Primary | Zero | Government | DR MIAN KASHIF ALI | 1202 | 48 | 01-01-16 | 1 | Samiullah | 3339837516 | |||
9155 | 14301010 | PUNJAB | SARGODHA | MIANWALI | 31 | EISAKHAIL | SULTAN KHAIL | 1788 | BHU Sultan Khel | 0 | 1 | 1 | Quarterly | Primary | Zero | Government | DR MIAN KASHIF ALI | 1202 | 65 | 01-01-16 | 1 | Samiullah | 3339837516 | |||
10231 | 14404001 | PUNJAB | SARGODHA | BHAKKAR | 31 | MANKERA | 67 ML | 1872 | BHU 67 ML | 0 | 1 | 1 | Quarterly | Primary | Zero | Government | 1202 | 50 | 01-01-17 | 1 | Faisal Gul | 3156849656 | ||||
10232 | 14401001 | PUNJAB | SARGODHA | BHAKKAR | 31 | BHAKKAR | 61ML | 1837 | BHU Saraye Muhajir | 0 | 1 | 1 | Quarterly | Primary | Zero | Government | 1202 | 80 | 17-01-01 | 1 | Faisal Gul | 3156849656 | ||||
10234 | 15102001 | PUNJAB | FAISALABAD | FAISALABAD | 25 | FSD CITY | UC 177 | 1894 | ZCD 204 RB | 204 RB | 0 | 1 | 1 | Quarterly | Primary | Zero | Government | 1257 | 0 | 01-07-17 | 1 | |||||
10235 | 15102025 | PUNJAB | FAISALABAD | FAISALABAD | 25 | FSD CITY | UC 201 | 1918 | ZCD Mananwala | Mananwala | 0 | 1 | 1 | Quarterly | Primary | Zero | Government | 1257 | 75 | 01-07-17 | 1 | Dr.Iqra | ||||
10236 | 15102032 | PUNJAB | FAISALABAD | FAISALABAD | 25 | FSD CITY | UC 208 | 1925 | GD Bolley de Jhuggi | Bolley de Ghuggi | 0 | 1 | 1 | Quarterly | Primary | Zero | Government | 1257 | 100 | 01-07-17 | 1 | Dr.Umair | ||||
10237 | 15102055 | PUNJAB | FAISALABAD | FAISALABAD | 25 | FSD CITY | UC 231 | 1948 | GD Factory area | Factory Area,Faisalabad | 0 | 1 | 1 | Quarterly | Primary | Zero | Government | 1257 | 100 | 01-07-17 | 1 | Dr.Shaisata | ||||
Please note that it is just a 2% of real data as the real data contains nearly 4000+ rows.
I needed few rows out of this database (required rows highlighted yellow in the above table) for my analysis. That is why I created a pivot table out of this data sheet to extract my desired columns. You can see pivot table below:
file.xlsx | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | |||
1 | IsActive | 1 | |||||||||||
2 | |||||||||||||
3 | ID | Ucode | District | Tehsil | UC | NameHF | Freq | Level | Type | DateIncluded | DaillyTurnover | ||
4 | 9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 01-01-16 | 65 | ||
5 | 9156 | 14302013 | MIANWALI | MAINWALI | MARI INDUS | BHu Mari Indus | Quarterly | Primary | Zero | 01-01-16 | 70 | ||
6 | 9158 | 14303001 | MIANWALI | PIPLAN | ALOWALI | BHU Dhoke Ayub | Quarterly | Primary | Zero | 01-01-16 | 48 | ||
7 | 9159 | 14302002 | MIANWALI | MAINWALI | BAN HAFAZ GI | BHU Laloo Khel | Quarterly | Primary | Zero | 01-01-16 | 65 | ||
8 | 9371 | 14104017 | SARGODHA | SARGODHA | CHAK # 46 SB | Iqra Medical complex | Quarterly | Primary | Zero | 01-01-17 | 65 | ||
9 | 9830 | 14203007 | KHUSHAB | QUAIDA ABAD | QUAIDA ABAD | Mazhar Hospital Quaidabad | Quarterly | Secondary | Zero | 01-01-17 | 80 | ||
10 | 10231 | 14404001 | BHAKKAR | MANKERA | 67 ML | BHU 67 ML | Quarterly | Primary | Zero | 01-01-17 | 50 | ||
11 | 10232 | 14401001 | BHAKKAR | BHAKKAR | 61ML | BHU Saraye Muhajir | Quarterly | Primary | Zero | 17-01-01 | 80 | ||
12 | 10234 | 15102001 | FAISALABAD | FSD CITY | UC 177 | ZCD 204 RB | Quarterly | Primary | Zero | 01-07-17 | 0 | ||
13 | 10235 | 15102025 | FAISALABAD | FSD CITY | UC 201 | ZCD Mananwala | Quarterly | Primary | Zero | 01-07-17 | 75 | ||
14 | 10236 | 15102032 | FAISALABAD | FSD CITY | UC 208 | GD Bolley de Jhuggi | Quarterly | Primary | Zero | 01-07-17 | 100 | ||
15 | 10237 | 15102055 | FAISALABAD | FSD CITY | UC 231 | GD Factory area | Quarterly | Primary | Zero | 01-07-17 | 100 | ||
16 | Grand Total | ||||||||||||
Pivot |
However, the tricky part in which I got stuck is to create duplicate rows for each single row present in this pivot table. The pivot table if you can see contains health facilities and each of this health facility has a unique "ID". What I intend to add is 23 more duplicate rows for each single ID. All the data of pivot table rows remain same in the final outcome sheet, except for two new columns of "Year" and "Month" added against each row (as shown in the below table). Year will contain 2020/2021 and month will contain all 12 months. In total 24 rows need to be created against each single ID through some vba command. Simplifying it further, we currently have 12 rows in pivot table or your can say that 12 unique ID's. We need to add 23 new rows against each ID and hence, total of 24*12=288 rows will be created along with year and month column. Below image shows the outcome for a single ID.
ID | Ucode | District | Tehsil | UC | NameHF | Freq | Level | Type | DateIncluded | DaillyTurnover | Year | Month |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Jan |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Feb |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Mar |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Apr |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | May |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Jun |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Jul |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Aug |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Sep |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Oct |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Nov |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2020 | Dec |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Jan |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Feb |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Mar |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Apr |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | May |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Jun |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Jul |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Aug |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Sep |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Oct |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Nov |
9155 | 14301010 | MIANWALI | EISAKHAIL | SULTAN KHAIL | BHU Sultan Khel | Quarterly | Primary | Zero | 42370 | 65 | 2021 | Dec |
Will really appreciate your support to automate the task shown in the last table.