VBA - Inserting n rows to allow for extended range

UlyssesFR

New Member
Joined
Jun 30, 2020
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone

Given the following source range on Worksheets(SOURCE):
B738D ACCESS Lav Moc Overview- Input File_CEDRIC_TESTMACRO.xlsm
AB
1Part NumberFunction Code
2PART NUMBER A1.01
3PART NUMBER B1.01
4PART NUMBER C1.02
5PART NUMBER D1.02
6PART NUMBER E1.02
7PART NUMBER F1.02
8PART NUMBER G1.02
9PART NUMBER H1.02
10PART NUMBER I1.02
11PART NUMBER J1.02
12PART NUMBER K1.04
13PART NUMBER L1.04
14PART NUMBER M1.04
15PART NUMBER N1.04
16PART NUMBER O1.05
17PART NUMBER P1.05
18PART NUMBER Q1.05
19PART NUMBER R1.05
20PART NUMBER S1.05
21PART NUMBER T1.05
22PART NUMBER U1.05
SOURCE


And the following recipient table on Worksheets(RECIPIENT):
B738D ACCESS Lav Moc Overview- Input File_CEDRIC_TESTMACRO.xlsm
ABC
1Function CodePart NumberTests
21.01Test1
3Test2
4Test3
5Test4
6Test5
7Test6
81.02Test1
9Test2
10Test3
11Test4
12Test5
13Test6
141.03Test1
15Test2
16Test3
17Test4
18Test5
19Test6
201.04Test1
21Test2
22Test3
23Test4
24Test5
25Test6
261.05Test1
27Test2
28Test3
29Test4
30Test5
31Test6
RECIPIENT


I would like to achieve this:
B738D ACCESS Lav Moc Overview- Input File_CEDRIC_TESTMACRO.xlsm
ABCD
1Function CodePart NumberTests
21.01PART NUMBER ATest1'VBA code looks at the Function Code in worksheets(RECIPIENT).Columns(A) then returns the matching range of Part Numbers from worksheets(SOURCE).Columns(A) in worksheets(RECIPIENT).Columns(B). 'If the matching range of Part Numbers for a particular Function Code is larger than 6 rows, then n rows need to be inserted to adjust for the difference: i.e. Function Code 1.02 has 8 matching Part Numbers - PART NUMBER C to PART NUMBER J - therefore we need to insert n = 8 - 6 = 2 extra rows before the next Function Code 1.03 to allow for PART NUMBER I and J. 'If the matching range Part Numbers for a particular Function Code is smaller than or equal to 6, then no need to insert new rows and the matching Part Numbers can just be copied in. 'If no matching Part Numbers are found for a particular Function Code, go to next Function Code. 'Worksheets(RECIPIENT).Columns(C) range Test1 to Test6 need to stay "anchored" at the top when inserted new rows.
3PART NUMBER BTest2
4Test3
5Test4
6Test5
7Test6
81.02PART NUMBER CTest1
9PART NUMBER DTest2
10PART NUMBER ETest3
11PART NUMBER FTest4
12PART NUMBER GTest5
13PART NUMBER HTest6
14PART NUMBER I
15PART NUMBER J
161.03Test1
17Test2
18Test3
19Test4
20Test5
21Test6
221.04PART NUMBER KTest1
23PART NUMBER LTest2
24PART NUMBER MTest3
25PART NUMBER NTest4
26Test5
27Test6
281.05PART NUMBER OTest1
29PART NUMBER PTest2
30PART NUMBER QTest3
31PART NUMBER RTest4
32PART NUMBER STest5
33PART NUMBER TTest6
34PART NUMBER U
RESULT


Where can I start? I wish my boss would give me enough time to suss this one out by myself, but unfortunately, he doesn't :(

Any help or pointers would be greatly appreciated!

Kind regards.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Probably there is a better macro and maybe faster.
But check if the following works for you.

Also comment on how many records you have on both sheets.
In the sheet "RECIPIENT" for each FucntionCode there are always 6 records?

Try this:

VBA Code:
Sub Inserting_n_rows()
  Dim sh1 As Worksheet, sh2 As Worksheet, fCode As Variant
  Dim i As Long, j As Long, nCount As Long, nhas As Long, ini As Long
  
  Application.ScreenUpdating = False
  Set sh1 = Sheets("SOURCE")
  Set sh2 = Sheets("RECIPIENT")
  For i = sh2.Range("C" & Rows.Count).End(3).Row To 2 Step -1
    nhas = i - sh2.Range("A" & i).End(3).Row + 1
    j = sh2.Range("A" & i).End(3).Row
    fCode = sh2.Range("A" & j)
    nCount = WorksheetFunction.CountIf(sh1.Range("B:B"), fCode)
    If nCount > 0 Then
      If nhas < nCount Then
        sh2.Range("A" & i + 1 & ":A" & i + (nCount - nhas)).EntireRow.Insert
      End If
      ini = sh1.Range("B:B").Find(fCode, , xlValues, xlWhole, xlByRows, xlNext, False).Row
      sh2.Range("B" & j).Resize(nCount).Value = sh1.Range("A" & ini).Resize(nCount).Value
    End If
    i = i - nhas + 1
  Next
End Sub
 
Upvote 0
Wow, amazing!

I did start going the CountIf route but it was nowhere as streamlined / elegant as this.

I went as far as inserting the needed amount of rows but then hit a brick wall when trying to copy the matching data.

I haded a bit of code to loop through all my source sheets and it works a treat.

Thank you vvvvvery much!

VBA Code:
Sub Distribute_Part_Numbers()

Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim fCode As Variant
Dim i As Long
Dim j As Long
Dim nCount As Long
Dim nhas As Long
Dim ini As Long
Dim WB_TWB As Workbook
Dim WS As Worksheet
  
Application.ScreenUpdating = False
  
Set sh1 = Sheets("COMBINED")
Set WB_TWB = ThisWorkbook
  
    For Each WS In WB_TWB.Worksheets
        With WB_TWB.Worksheets(WS.Name)
            If WS.Name Like "FB*" Then
                .Activate
                For i = Sheets(WS.Name).Range("E" & Rows.count).End(3).Row To 2 Step -1
                    nhas = i - Sheets(WS.Name).Range("A" & i).End(3).Row + 1
                    j = Sheets(WS.Name).Range("A" & i).End(3).Row
                    fCode = Sheets(WS.Name).Range("A" & j)
                    nCount = WorksheetFunction.CountIf(sh1.Range("D:D"), fCode)
                        If nCount > 0 Then
                                If nhas < nCount Then
                                    Sheets(WS.Name).Range("A" & i + 1 & ":A" & i + (nCount - nhas)).EntireRow.Insert
                                End If
                            ini = sh1.Range("D:D").Find(fCode, , xlValues, xlWhole, xlByRows, xlNext, False).Row
                            Sheets(WS.Name).Range("C" & j).Resize(nCount).Value2 = sh1.Range("A" & ini).Resize(nCount).Value2
                        End If
                    i = i - nhas + 1
                Next
            End If
        End With
    Next WS
    
Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
I won't lie though, I'm having a hard time wrapping my head around how the code works, especially the
VBA Code:
.Find()
part


I'll have a closer look at it when/if I have a bit of spare time at work, but in the meantime, I will blindly use it :)
 
Upvote 0
:mad::mad::mad::mad::mad:

I may have spoken too soon...

I've been trying now for a few hours to replicate the first time the code ran (and seemingly worked) but for some reason, it won't work again.

As I hinted above, I have 5 "Recipient" Sheets, named FB1, FB2... to FB5 (not several Source sheets as I mistakenly mentioned above)

All of them have the exact same format:
B738D ACCESS Lav MoC Overview (Assys Only)_20200727_1518.xlsm
ABCDE
1Function CodeFunction Sub-CategoryAssy P/NComponent P/NTests
21.01Side Wall PanelN254L4051-000Structural Integrity
3N254L4055-000Flammability & Smoke
4Handling & Abuse
5Environmental Conditions
6Endurance
7Vibration
8USPHS
91.02Aisle Wall PanelN254L4052-000Structural Integrity
10N254L4058-000Flammability & Smoke
11Handling & Abuse
12Environmental Conditions
13Endurance
14Vibration
15USPHS
161.03Rear Wall PanelStructural Integrity
17Flammability & Smoke
18Handling & Abuse
19Environmental Conditions
20Endurance
21Vibration
22USPHS
231.04Ceiling PanelN254L4034-000Structural Integrity
24N254L4054-000Flammability & Smoke
25N254L4059-000Handling & Abuse
26N254L4060-000Environmental Conditions
27Endurance
28Vibration
29USPHS
301.05Floor AssyN254L4053-000Structural Integrity
31N254L4057-000Flammability & Smoke
32Handling & Abuse
33Environmental Conditions
34Endurance
35Vibration
36USPHS
FB1


The function code in column A runs up to 1.99 for FB1, up to 2.99 for FB2... and up to 5.99 for FB3.

The source sheet is as follow
B738D ACCESS Lav MoC Overview (Assys Only)_20200727_1518.xlsm
ABCDE
1PART NUMBERDESCRIPTIONMATERIALFUNCTION CODEUSED IN
2N254L4051-000PANEL, FWD ASSYN/A1.01N254L4005-000
3N254L4055-000PANEL, AFT ASSYN/A1.01N254L4003-000
4N254L4052-000PANEL, AISLE FWD ASSYN/A1.02N254L4005-000
5N254L4058-000PANEL, AISLE AFT ASSYN/A1.02N254L4003-000
6N254L4034-000CEILING PANEL ASSYN/A1.04N254L4001-000
7N254L4054-000PANEL, TOP FWD ASSYN/A1.04N254L4005-000
8N254L4059-000PANEL, TOP AFT ASSYN/A1.04N254L4003-000
9N254L4060-000PANEL ASSYN/A1.04N254L4003-000
10N254L4053-000PANEL, BASE FWD ASSYN/A1.05N254L4005-000
11N254L4057-000PANEL, BASE AFT ASSYN/A1.05N254L4003-000
12N254L4030-000PANEL ASSYN/A1.06N254L4001-000
13N254L4035-000CLOSEOUT PANEL ASSYN/A1.06N254L4001-000
14N254L4064-000PANEL ASSYN/A1.06N254L4001-000
15N254L4007-000PRM LAVATORY BONDED ASSY, EXPANDING SECTIONN/A1.07N254L4006-000
16N254L4010-000SLIDING PANEL ASSYN/A1.07N254L4001-000
17N254L4027-000BI-FOLD PANEL ASSYN/A1.07N254L4001-000
18N254L4032-000BUMPER ASSYN/A1.07N254L4002-000
19N254L4032-002BUMPER ASSYN/A1.07N254L4002-000
20N254L4033-000BUMPER ASSYN/A1.07N254L4004-000
21N254L4061-000PANEL, TOP, EXPANDING SECTION ASSYN/A1.07N254L4007-000
22N254L4062-000SLIDING PANEL BONDED ASSYN/A1.07N254L4010-000
23N254L4063-000PANEL ASSY, AFT, EXPANDING SECTIONN/A1.07N254L4007-000
24N254L4026-000TOILET SHROUD ASSYN/A1.08N254L4001-000
25N254L4046-000ROLL ASSYN/A1.08N254L4004-000
26N254L4065-000BRACKET ASSY, TOILET SHROUDN/A1.08N254L4002-000
27N254L4009-000DOOR ASSYN/A1.09N254L4006-000
28N254L4043-000DOOR BONDED ASSYN/A1.09N254L4009-000
29N254L4006-000PRM LAVATORY GENERAL ASSY, EXPANDING SECTIONN/A1.99N254L4001-000
30N240L4001-000MICROSWITCH DISCONNECT ASSYN/A2.06N240L1001-000
31N254L4056-000MICRO SWITCH ASSYN/A2.06N254L4010-000
32N240L4002-000GROUND STUD BRACKET ASSYN/A2.07N240L1001-000
33N332L4001-000MAIN LIGHT ASSYN/A2.99N240L1001-000
34N212L4001-000HOSE COUPLER ASSYN/A3.01N212L1001-000
35N212L4002-000PLENUM, VENTILATION ASSYN/A3.01N212L1002-000
36N381L4001-000DISCONNECT WELDED ASSYN/A3.03N381L1001-000
37N383L4001-000OVERFLOW PIPE ASSYN/A3.07N383L1001-000
38N380L4001-000VACUUM WASTE ASSYN/A3.99N380L1001-000
39N254L4013-000VANITY UNIT ASSYN/A4.01N254L4004-000
40N254L4015-000DOOR ASSY, SINKN/A4.01N254L4013-000
41N254L4019-000VANITY UNIT BONDED ASSYN/A4.01N254L4013-000
42N254L4047-000BRACKET ASSYN/A4.01N254L4004-000
43N254L4048-000ANGLE ASSYN/A4.01N254L4013-000
44N254L4049-000ANGLE ASSYN/A4.01N254L4013-000
45N254L4017-000DOOR ASSY, ROLL MIDDLEN/A4.02N254L4004-000
46N254L4018-000DOOR ASSY, ROLL TOPN/A4.03N254L4004-000
47N254L4022-000TABLE SUPPORT ASSYN/A4.05N254L4002-000
48N254L4036-000BABY CHANGE TABLE ASSYN/A4.05N254L4001-000
49N254L4037-000PANEL ASSY, BABY CHANGE TABLEN/A4.05N254L4036-000
50N254L4028-000TOILET LID ASSYN/A4.07N254L4029-000
51N254L4029-000TOILET SEAT ASSYN/A4.08N254L4026-000
52N254L4021-000DISPENSER ASSYN/A4.09N254L4004-000
53N254L4044-000WASTE CHUTE ASSYN/A4.13N254L4013-000
COMBINED


Now, for some unknown reason(s), when I now run the code, it works for the recipient sheet FB1, but the recipient sheets FB2 to FB5 remain unchanged except for the fact that their heading in column C changes from "Assy P/N" to "PART NUMBER" (The heading of column A of the source Sheet)

ie

B738D ACCESS Lav MoC Overview (Assys Only)_20200727_1518.xlsm
ABCDE
1Function CodeFunction Sub-CategoryPART NUMBERComponent P/NTests
23.02Water HeaterStructural Integrity
3Flammability & Smoke
4Handling & Abuse
5Environmental Conditions
6Endurance
7Vibration
8USPHS
93.03Piping Potable WaterStructural Integrity
10Flammability & Smoke
11Handling & Abuse
12Environmental Conditions
13Endurance
14Vibration
15USPHS
FB3


I have tried to apply your original code to run individually on each recipient sheet by changing the diming of sh2 = Sheets("FB2") and matching the column references but it still won't work.

I'm at a loss once again
 
Upvote 0
Some good news,

I created a brand new xlsm file and copied the code in, copied/pasted all the content (values only) of each sheet, and it runs just fine and dandy...

I still have no idea what the cause of it malfunctioning was...
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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