CREATE NEW ROWS BASED ON CELL VALUE

amit96

New Member
Joined
Feb 15, 2022
Messages
27
Office Version
  1. 2013
Platform
  1. Windows
Hello everyone :)

I'm trying to write in VBA a code that will do:

1. Take a cell value in column K (there is a number there) and according to this number open new rows below the row I am on and copy the data from the first row to all the other rows that have been opened.

Example -
If in column K9 the number 5 appears, then open 5 new rows below, and copy all the data from row K9 to all the new rows created.
If in position K10 the number 7 appears, then open 5 new rows below, and copy all the data from row K10 to all the new rows created.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Do you want the script to run when you enter a number in column K or when you click a button?
 
Upvote 0
when enter a number in column K
In fact column K has a LIST in each row, and inside the LIST there is a list of numbers.
So if you choose 4, they will open 4 rows.
And so on
 
Upvote 0
I assume you mean insert new rows.
Your subject title says Create but then you say Open.
Try this:
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  3/19/2022  6:19:43 AM  EDT
If Target.Column = 11 Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Rows(Target.Row).Offset(1).Resize(Target.Value).Insert
Rows(Target.Row).Offset(1).Resize(Target.Value).Value = Rows(Target.Row).Value
End If
End Sub
 
Upvote 0
I assume you mean insert new rows.
Your subject title says Create but then you say Open.
Try this:
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  3/19/2022  6:19:43 AM  EDT
If Target.Column = 11 Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Rows(Target.Row).Offset(1).Resize(Target.Value).Insert
Rows(Target.Row).Offset(1).Resize(Target.Value).Value = Rows(Target.Row).Value
End If
End Sub
Hi!
It worked but there is one problem.
The paste in the new lines is done in values and not in formulas.
I need the paste to be in the formulas.
And if that's possible, I'll really appreciate it if the code would color the new lines gray.

Really thanks for the help it helps me a lot!
 
Upvote 0
This is beyond my knowledgebase.
I will continue to monitor this thread to see what I can learn.
Copying rows with formulas and then doing what you want is something I'm not sure how to do.
 
Upvote 0
Try this:
Not sure if your formulas will provide the results you want:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  3/20/2022  4:31:57 AM  EDT
If Target.Column = 11 Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Rows(Target.Row).Copy
Rows(Target.Row).Offset(1).Resize(Target.Value).Insert
Rows(Target.Row).Offset(1).Resize(Target.Value).PasteSpecial
Application.CutCopyMode = False

End If
End Sub
 
Upvote 0
Try this:
Not sure if your formulas will provide the results you want:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  3/20/2022  4:31:57 AM  EDT
If Target.Column = 11 Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Rows(Target.Row).Copy
Rows(Target.Row).Offset(1).Resize(Target.Value).Insert
Rows(Target.Row).Offset(1).Resize(Target.Value).PasteSpecial
Application.CutCopyMode = False

End If
End Sub
1647771366145.png
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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