简体   繁体   中英

VBA - Insert two new rows based on Userform criteria and also copying the excel formula of a cell range

This question is very tricky so I added more Images to make it better to understand ans this is my Excel sheet Looks like Gantt chart before adding anything.

在此处输入图片说明

In this Excel sheet the cells gets filled by a Userform and the cell values"G3" gets reduced based on "booked" value Cell "E4" and "F5"

图片

Required Output: How to add new "members" in Column A based on the "Red" and "Blue" in between the members. Important is the Gantt Chart should also be added as it is for other members.

Output should look like below Image:

在此处输入图片说明

I was trying Row Insert method with below Code but it is only adding a new row but not fulfilling my requirement.

Sub Insert()
    'Select and find where to insert new row
    ActiveSheet.Range("A:A").Find(What:=Me.cboteam.Value, LookIn:=xlFormulas, Lookat:=xlWhole)
    ActiveCell.EntireRow.Insert
    ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown,CopyOrigin:=xlFormatFromLeftOrAbove
 End Sub

Try this:

Sub Insert()
    'Select and find where to insert new row
    Dim rngFound As Range: Set rngFound = ActiveSheet.Range("A:A").Find(What:=Me.cboteam.Value, LookIn:=xlFormulas, Lookat:=xlWhole)
    rngFound.Offset(1, 0).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    rngFound.Offset(1, 0).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Debug.Print rngFound.Offset(1, 0).Resize(2, 1).Address
    rngFound.Resize(3, 1).EntireRow.FillDown
 End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM