I want to create a macro that will autofill a new cell below the existing row for columns D, E and H. The macro I created currently does not work.
There is an example of what the macro should do.
I want to create a macro that will autofill a new cell below the existing row for columns D, E and H. The macro I created currently does not work.
There is an example of what the macro should do.
Try this code
Copy the Excel VBA code![]()
Sub autofill_row() Dim i As Long, lrow As Long With Worksheets("Sheet1") lrow = .Range("D" & .Rows.Count).End(xlUp).Row For i = 3 To lrow + 1 If .Range("D" & i) = "" Then .Rows(i).Insert .Range("D" & i - 1 & ":E" & i - 1).AutoFill Destination:=.Range("D" & i - 1 & ":E" & i), Type:=xlFillDefault .Range("H" & i - 1).AutoFill Destination:=.Range("H" & i - 1 & ":H" & i), Type:=xlFillDefault End If Next i End With End Sub
Select the workbook in which you want to store the Excel VBA code
Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
Choose Insert | Module
Where the cursor is flashing, choose Edit | Paste
To run the Excel VBA code:
Choose View | Macros
Select a macro in the list, and click the Run button
If I have helped, Don't forget to add to my reputation (click on the star below the post)
Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
Use code tags when posting your VBA code: [code] Your code here [/code]
Hi,
I want to revisit this macro with modified data. On the active sheet, "with_food", I tried to simply copy and paste the same macro and create a new macro and just change the columns to auto fill.
Here is the new code.
The macro is not working properly and I would like, if possible if someone could both change the code so it autofills column G:G and then from I:O and maybe briefly explain how the code works.![]()
Sub autofill_row2() Sub autofill_row2() Dim i As Long, lrow As Long With Worksheets("with_food") lrow = .Range("G" & .Rows.Count).End(xlUp).Row For i = 1 To lrow + 1 If .Range("G" & i) = "" Then .Rows(i).Insert .Range("G" & i - 1 & ":H" & i - 1).AutoFill Destination:=.Range("G" & i - 1 & ":H" & i), Type:=xlFillDefault .Range("I" & i - 1).AutoFill Destination:=.Range("I" & i - 1 & ":O" & i), Type:=xlFillDefault End If Next i End With End Sub
Thanks
![]()
Sub autofill_row() Dim i As Long, lrow As Long With Worksheets("Sheet1") lrow = .Range("D" & .Rows.Count).End(xlUp).Row For i = 3 To lrow + 1 If .Range("D" & i) = "" Then .Rows(i).Insert .Range("D" & i - 1 & ":E" & i - 1).AutoFill Destination:=.Range("D" & i - 1 & ":E" & i), Type:=xlFillDefault .Range("H" & i - 1).AutoFill Destination:=.Range("H" & i - 1 & ":H" & i), Type:=xlFillDefault End If Next i End With End Sub
Last edited by Cutter; 09-08-2012 at 08:11 AM. Reason: Added code tags
Thank you!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks