VBA code help

stromnoexcel

New Member
Joined
Dec 4, 2023
Messages
3
Office Version
  1. 365
Platform
  1. 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?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Welcome to MrExcel. To keep things simple, I'll continue with the same approach you are using. Many of your errors are syntax errors, and those will begin to disappear as you learn VBA

VBA Code:
Sub Example()
    Dim v2 As Worksheet
    Dim LastRow As Long                               'Integer
    Dim lngIdx As Long
    Dim a As Variant
    Dim J As Long

    '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)

    Set v2 = Worksheets("v2")                         'assign worksheet to variable v2

    With v2
        LastRow = .Cells(.Rows.Count, 4).End(xlUp).Row    'determine last row

        For lngIdx = LastRow To 2 Step -1             'step thru backwards
            a = .Cells(lngIdx, 4).Value
            If IsNumeric(a) And a > 1 Then            'except 1
                Application.ScreenUpdating = False    'turn off screen updating

                'Insert new rows
                For J = 1 To a
                    .Rows(lngIdx).Insert Shift:=xlUp
                Next J
                Application.ScreenUpdating = True     'turn on screen updating
            End If
        Next lngIdx
    End With
End Sub


(Tip: when posting code, please try to use 'code tags' to format the code as I have done above


as it makes the code easier to read.)
 
Upvote 0
Welcome to MrExcel. To keep things simple, I'll continue with the same approach you are using. Many of your errors are syntax errors, and those will begin to disappear as you learn VBA

VBA Code:
Sub Example()
    Dim v2 As Worksheet
    Dim LastRow As Long                               'Integer
    Dim lngIdx As Long
    Dim a As Variant
    Dim J As Long

    '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)

    Set v2 = Worksheets("v2")                         'assign worksheet to variable v2

    With v2
        LastRow = .Cells(.Rows.Count, 4).End(xlUp).Row    'determine last row

        For lngIdx = LastRow To 2 Step -1             'step thru backwards
            a = .Cells(lngIdx, 4).Value
            If IsNumeric(a) And a > 1 Then            'except 1
                Application.ScreenUpdating = False    'turn off screen updating

                'Insert new rows
                For J = 1 To a
                    .Rows(lngIdx).Insert Shift:=xlUp
                Next J
                Application.ScreenUpdating = True     'turn on screen updating
            End If
        Next lngIdx
    End With
End Sub


(Tip: when posting code, please try to use 'code tags' to format the code as I have done above


as it makes the code easier to read.)
Thank you very much for your help! :)
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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