VBA - How to add empty rows until Row 10-20-30-... after unique values in column A

Jerbu

New Member
Joined
Sep 22, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello Mr.Excel community. For a collaboration at work we are looking at providing labels to our partner in Africa, however, printing those labels based on the excel file provided is proving to be a challenge to my limited VBA knowledge and I was hoping somebody could help me out.

I have a rather large excell file containing in column A a (unique) household identifier and in column B a household member ID. Depending on the number of members in a household, some column A values will happen 1-2-3-... times. For label printing purposes, I would need a label page per household, which comes down to 10 lines. So I need to add a variable number of empty rows to the data, if household ID1 occurs 2 times, I want 8 empty rows added behind it, if household ID2 occurs 7 times, 3 empty rows are to be added etc... all the way down.

I've been breaking my head over this (and learning stuff in the proces), but I just can't seem to get there. Any help would be much appreciated.
 

Attachments

  • Excell.jpg
    Excell.jpg
    122.8 KB · Views: 4

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
give this a try.

VBA Code:
Sub t()
Dim i As Long, cnt As Long
With ActiveSheet
    For i = .Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row To 2 Step -1
        cnt = Application.CountIf(.Range("A:A"), .Cells(i, 1).Value)
        Rows(i + 1).Resize(10 - cnt).Insert
        i = i + 1 - cnt
    Next
End With
End Sub

There is a possibility that it will not work while you data is in table configuration. But you can still try it.
 
Last edited:

Jerbu

New Member
Joined
Sep 22, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Thanks a lot for this! There were some errors at first due to some strange data (>10 houshold members) in my tables, but this script put me on the right path to figure the rest out myself and I have the layout I need now.
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Thanks for the feedback,
Regards, JLG
 

Watch MrExcel Video

Forum statistics

Threads
1,127,121
Messages
5,622,861
Members
415,935
Latest member
kes1973

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
Top