Add rows to worksheet based on size of another data range

bhall

New Member
Joined
Sep 27, 2013
Messages
12
I'm trying to automatically add rows to WS1 based on the number of populated rows in range F1:F20 in WS2. for example WS1 (column D) looks like:

1.1.1.3
1.1.1.4
1.1.1.5
etc.

WS2 F1:F20 looks like:
PM
EE
SW
ME
etc.

Basically, I want to automatically add rows below each WBS item in WS1 that contain the labor categories in WS2. I'm okay with VBA solutions (which I believe this requires), I just have a pretty quick turn on getting this done.

Thanks in advance for the help!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
This should work. It assumes both ranges contain contiguous ranges of values.

Code:
Public Sub BuildReport()

    ' set sheet names and range
    Const DEST_SHEET = "Sheet1"
    Const DEST_RANGE = "F1:F20"
    Const SOURCE_SHEET = "Sheet2"
    Const SOURCE_RANGE = "F1:F20"

    
    With ThisWorkbook
        ' get full ranges
        Dim DestRange As Range: Set DestRange = .Worksheets(DEST_SHEET).Range(DEST_RANGE)
        Dim SourceRange As Range: Set SourceRange = .Worksheets(SOURCE_SHEET).Range(SOURCE_RANGE)
        'subset ranges to only those with values (probably an easier way to do this with xldown)
        Set DestRange = DestRange.Cells(1).Resize(Application.WorksheetFunction.CountA(DestRange))
        Set SourceRange = SourceRange.Cells(1).Resize(Application.WorksheetFunction.CountA(SourceRange))
    End With
    
    ' do the insert
    Dim i As Integer
    For i = DestRange.Cells.Count To 1 Step -1
    
        Dim ShiftRange As Range: Set ShiftRange = DestRange.Cells(i).Offset(1, 0).Resize(SourceRange.Cells.Count)
        ShiftRange.EntireRow.Insert Shift:=xlDown
        ShiftRange.Offset(-ShiftRange.Cells.Count, 0).Value = SourceRange.Value
        
    Next
    

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,027
Messages
6,128,367
Members
449,444
Latest member
abitrandom82

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