Insert Rows based on cell value and copy formulas

Excel_not_Knower

New Member
Joined
Jan 7, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have a task where I need to split the Jobs into Tasks and these tasks has to be added in separate rows under the main job. Numbers of rows needed to be added is based on cell value.
Please suggest how to make this work?
A2- Main Job
B2-Task No
C2 - how many rows need to be added minus 1 ( as I already have 1 row as main Job). Mean when it says 4 tasks next to A3 JOB = 4-1 = 3 Rows to be added below A3.
And copy all formulas from Range D:Q into new rows

Before:
1664967522985.png
After:
1664967696423.png
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Oh, Forgot to mention. If possible it would be handy if there's going to be an option to make a check. If rows (Tasks) are already been created then skip and check next one.
 
Upvote 0
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, _
  Cancel As Boolean)
    Dim cmdNew As CommandBarButton

    For Each icbc In Application.CommandBars("List Range Popup").Controls
        If icbc.Tag = "brccm" Then icbc.Delete
    Next icbc

    If Not Application.Intersect(Target, Range("SLJ_4[Job Number]")) _
      Is Nothing Then
        Set cmdNew = CommandBars("List Range Popup").Controls.Add
        With cmdNew
            .Caption = "Add Tasks"
            .OnAction = "Insert_Tasks"
            .BeginGroup = True
            .Tag = "brccm"
        End With
    End If
End Sub



Sub Insert_Tasks()


Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim HowMuchRowToInsert As Long
Dim AferWitchRowShouldIInsertRow As Long

On Error Resume Next
HowMuchRowToInsert = InputBox(Prompt:="How Many Rows to Insert?")
On Error GoTo 0

R = ActiveCell.Row

AferWitchRowShouldIInsertRow = R - 3

For I = 1 To HowMuchRowToInsert

Sheets("Ticksheet_1").ListObjects("SLJ_4").ListRows.Add (AferWitchRowShouldIInsertRow + I)


Next

Application.ScreenUpdating = True
Application.DisplayAlerts = True

Call CopyPaste

End Sub


Sub CopyPaste()

Dim LRow As Long


ActiveCell.Activate
ActiveCell.Copy '- What cell value to copy
LRow = Cells(Rows.Count, 1).End(xlUp).Row '- Count rows
For I = 4 To LRow 'Loop from 4th row
    If Cells(I, 1) = "" Then 'If cell is empty then paste new value
    Sheets("Ticksheet_1").Range(Cells(I, 1), Cells(I, 1)).PasteSpecial xlPasteValues
    End If
Next I
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,827
Messages
6,121,812
Members
449,048
Latest member
greyangel23

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