stromnoexcel
New Member
- Joined
- Dec 4, 2023
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
Hello,
I'm super duper new to VBA coding with literally 0 past experience past this project I've been assigned at work.
I basically need a code where the macro searches column D for cells >1 and then inserts rows according to the value in the cell while subtracting 1. So if a value is 2, 1 row would be added. If it's 3, 2 rows, and so on.
With good ol' Youtube, I have been able to come up with the following:
"Dim D As Range
Dim v2 As Worksheet
Dim LastRow As Integer
LastRow = Worksheets("v2").Cells(Row.Counts, 4).End(xlUp).Row
'Loop through the data range BACKWARDS, tracking each case where a row will need to be inserted
'Loop through the row numbers in the collection, which is in REVERSE order (adding rows will change the row numbers in the range,
'making forward looping hard)
With v2
For lngIdx = LastRow To 2 Step -1
a = Worksheets("v2").Cells(lngIdx, 4).Value 'except 1
If a > 1 = True Then
For j = 1 To a
Worksheets("v2").Rows(lngIdx).Select
Selection.Insert Shift:=xlDown
Next j
End If
Next lngIdx"
All the sources are added because when I run the macro, the error message "424" [object required] pops up and highlights the line starting with LastRow = ....
From the video, I gathered that were going from the last row up for looping purposes, which may be the reason why the rows are being added to the top row (row in question being shifted down) but I would prefer the rows pop up on the bottom. I can also see how that can throw off the macro since its going down to up so if's not possible, then whatever.
I hope this task isnt impossible
TLDR: how can I make a macro to check each cell in column D and each time a cell's value is >1, entire rows are inserted (preferably below the activated cell) based on the value in the cell minus 1?
I'm super duper new to VBA coding with literally 0 past experience past this project I've been assigned at work.
I basically need a code where the macro searches column D for cells >1 and then inserts rows according to the value in the cell while subtracting 1. So if a value is 2, 1 row would be added. If it's 3, 2 rows, and so on.
With good ol' Youtube, I have been able to come up with the following:
"Dim D As Range
Dim v2 As Worksheet
Dim LastRow As Integer
LastRow = Worksheets("v2").Cells(Row.Counts, 4).End(xlUp).Row
'Loop through the data range BACKWARDS, tracking each case where a row will need to be inserted
'Loop through the row numbers in the collection, which is in REVERSE order (adding rows will change the row numbers in the range,
'making forward looping hard)
With v2
For lngIdx = LastRow To 2 Step -1
a = Worksheets("v2").Cells(lngIdx, 4).Value 'except 1
If a > 1 = True Then
For j = 1 To a
Worksheets("v2").Rows(lngIdx).Select
Selection.Insert Shift:=xlDown
Next j
End If
Next lngIdx"
All the sources are added because when I run the macro, the error message "424" [object required] pops up and highlights the line starting with LastRow = ....
From the video, I gathered that were going from the last row up for looping purposes, which may be the reason why the rows are being added to the top row (row in question being shifted down) but I would prefer the rows pop up on the bottom. I can also see how that can throw off the macro since its going down to up so if's not possible, then whatever.
I hope this task isnt impossible
TLDR: how can I make a macro to check each cell in column D and each time a cell's value is >1, entire rows are inserted (preferably below the activated cell) based on the value in the cell minus 1?