Need a VBA for adding rows above text in column B based on the count value of item in column D

pholo2505

New Member
Joined
Nov 3, 2016
Messages
3
Hi Friends,


I'm a member of this forum for a long time and found many VBA codes that fit to my works; thank you very much.
Today, I need another VBA codes but I cannot find it


I need VBA to add the total 20 rows above text in column B (FAC_7), accept for the first row (1063109), based on the count value of items in column D for the above item. So, the rows that need to be added will be the count number in D - 20.


For instance, for FAC_7 1066221, the items count in D will be 5, so the rows that need to be added above 1066221 is 20-5 =15 and for 1069205 is 20-3 = 17.

Thanks in advance,
Pon

Here is my sample data.
WrLQE5R7441-rfJNmCuSopy3wc4BmydetvBFY9-qo2mD-DYAcff9AZzZTLAOAOzczNv1c8iCodv8XTa8l6ooY4MV3uyCVqS1PbKtmghM7VZ1ghMLTx42gMQv5rAjSVl8M48LU05lALmRazl-6KFP8JGB85pGb6Urco8MkE5ULUbjGnCw9_7STAINoXm1Iz4TIKFwGeKfHXoUVZaDhyp63x0gyzFPYQrwWUiix5tWJlOd1h_4-yqjHeNzN0LPTB1r3Ee4U5FAr4zDHSXYmbcOV2HzeXGTss0G3OobQYGvsWBJpSq_NFfsjRjmcjGGr4zOpZZKx8hByzQUYrjXY7V-7IFcm-aDO4kRqOUH1ryGAthM4JaOwTSrfjCSZHqCgaMjju8_TgsQ7TwR2ZTvvDogie7N1mTEEBcFu_nTuhg1438VECPLtWbg_FJlj6m2EKhdxfvblcwJ3BE_cXg9ALJxWYhUCQPy9kRmF5s8mgZMSdCMDqZrtE2_SdXDgnXvclfSC8k8oIxNKfmDnBBf0WoPVIkrEQu-0XutzmLRdhlOmeF9DvcQ4QQLB-rK2qBSzeF8IsUFsWLdE0hkN2xgBXv99kA3FYUiZ86I2djD_3DnOHIOjfTLJNmVYwflWXrqBaNxF3gLh1H46HaTxE1Asi_kipUJNQNyKisY=w1440-h636-no
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Here is data
A B C D E F G H I
ORDER_NO FAC_7 UCC_20_DIGIT UPC QTY DUNS_NUMBER SUPPLIER CRC FRC
0083798794 1063109 00008379879401063109 019585218830 33 103492930 5112010 3596203 2600096
019585218847 33 103492930 5112010 3596206 2600096
019585218892 33 103492930 5112010 3596205 2600096
019585218915 33 103492930 5112010 3596211 2600096
019585218694 34 103492930 5112010 3629677 2600096
0083798794 1066221 00008379879401066221 019585218694 32 103492930 5112010 3629677 7418288
019585218830 33 103492930 5112010 3596203 7418288
019585218915 33 103492930 5112010 3596211 7418288
0083798794 1069205 00008379879401069205 019585218694 34 103492930 5112010 3629677 4300216
019585218830 34 103492930 5112010 3596203 4300216
019585218847 34 103492930 5112010 3596206 4300216
019585218892 34 103492930 5112010 3596205 4300216
019585218915 34 103492930 5112010 3596211 4300216

Pon
 
Upvote 0
Try this
Note: Assume that in column G you have blank cells

Code:
Sub test()
    Dim counter()
    Dim r As Range, wa As Range
    Dim f As Long, m As Long, i As Long
    
    Set r = Range("G2:G" & Range("A" & Rows.Count).End(xlUp).Row)
    ReDim counter(r.SpecialCells(xlCellTypeBlanks).Areas.Count, 1)
    For Each wa In r.SpecialCells(xlCellTypeBlanks).Areas
        m = m + 1
        counter(m, 0) = 18 - wa.Count
        counter(m, 1) = wa.Rows.Count + wa.Cells(1, 1).Row
    Next
    For i = m To 1 Step -1
        f = counter(i, 1)
        Rows(f & ":" & f + counter(i, 0)).Insert Shift:=xlDown ', CopyOrigin:=xlFormatFromLeftOrAbove
    Next
End Sub
 
Upvote 0
Thanks Dante for the code. I modify it a little bit to fit my need and it work just fine - thanks again.

Pon
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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