+ Reply to Thread
Results 1 to 4 of 4

autofill new cells

Hybrid View

  1. #1
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: autofill new cells

    Try this 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
    Copy the Excel VBA code
    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]

  2. #2
    Registered User
    Join Date
    05-17-2012
    Location
    United States
    MS-Off Ver
    365
    Posts
    6

    Re: autofill new cells

    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.

    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
    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.

    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
    Attached Files Attached Files
    Last edited by Cutter; 09-08-2012 at 08:11 AM. Reason: Added code tags

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1