VBA code - understanding the lines

Todd77

New Member
Joined
Aug 20, 2019
Messages
12
Hi all,

I found the following code when searching for a way to insert multiple rows based on a given cell's value. Can someone please help me understand what the code says?

Sub Insert_Row ()

Lastrow = Worksheets(“Sheet1”).Cells(Rows.Count, 1).End(xlUp).Row

For i= Lastrow To 2 Step by -1

a = Worksheets(“Sheet1”).Cells(I,3).Value
for j-1 to a

Worksheets(“Sheet1”).Row)(i).Select
Selection.Insert Shift:=xlDown
Next
Next

Worksheets(“Sheet1”).Cells(1,1).Select

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi
1-this is the correct code
2- put some data in column a,and some numbers in column c each few rows
Code:
Sub Insert_Row()
    Lastrow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    For i = Lastrow To 2 Step by - 1
       a = Worksheets("Sheet1").Cells(i, 3).Value
       For j = 1 To a
       Worksheets("Sheet1").Rows(i).Select
        Selection.Insert Shift:=xlDown
        Next
    Next
Worksheets("Sheet1").Cells(1, 1).Select
End Sub
 
Upvote 0
Code:
Sub Insert_Row()
Worksheets("Sheet1").Activate
Dim Lastrow&: Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = Lastrow To 2 Step by - 1
    Rows(i).Resize(Cells(i, 3)).Insert
Next
Cells(1, 1).Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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