+ Reply to Thread
Results 1 to 4 of 4

Thread: Autofill several columns using a marco

  1. #1
    Registered User
    Join Date
    06-03-2011
    Location
    Hatfield, England
    MS-Off Ver
    Excel 2007
    Posts
    15

    Smile Autofill several columns using a marco

    Hi everyone.

    I have been pulling my hair out over this one. Just cant figure it out.

    I am trying to autofill a formula down a column, as far as there is data in the adjacent row [ Or the length of column A] in a macro.

    The idea is that i paste my values into column A, and run a macro that autofills the formulas in B2,C2,D2.....O2 down as far as there are values in column A.

    At the moment i am having to paste my values into column A, and double click all the auto fill handles on the cells with the formulas.

    I have tried this

    Dim i As Long
    i = Cells(Rows.Count, "A").End(xlUp).Row
    Selection.AutoFill Destination:=Range("B2:B" & i)
    Selection.Autofill Destination:=Range("C2:C" & i)
    etc to
    Selection.Autofill Destination:=Range("O2:O" & i)

    This works for the B column, but fails from there.

    Any ideas ? I really need help !
    Last edited by adx1000; 09-13-2011 at 06:54 AM.

  2. #2
    Registered User
    Join Date
    05-17-2009
    Location
    Thornton, IA
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Autofill several columns using a marco

    adx1000,
    Could you supply a dummy worksheet? It will be easier to answer.
    hth

  3. #3
    Forum Guru mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,984

    Re: Autofill several columns using a marco

    The following comand will fail if the selected cell is not in column C

    Selection.Autofill Destination:=Range("C2:C" & i)
    try
    Range("c1").AutoFill Destination:=Range("C2:C" & i)
    or to autofill multiple adjoining columns

    Range("c1:e1").AutoFill Destination:=Range("C2:e" & i)
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  4. #4
    Registered User
    Join Date
    06-03-2011
    Location
    Hatfield, England
    MS-Off Ver
    Excel 2007
    Posts
    15

    Smile Re: Autofill several columns using a marco

    Quote Originally Posted by mudraker View Post
    The following comand will fail if the selected cell is not in column C



    try
    Range("c1").AutoFill Destination:=Range("C2:C" & i)
    or to autofill multiple adjoining columns

    Range("c1:e1").AutoFill Destination:=Range("C2:e" & i)



    Many thanks Mudraker. I had to use a combo of the two above to get the macro to run. Otherwise the selection was too large.

    I ended up with

    Sub autofillcolumns()
    
    Dim i As Long
    
    
    Application.ScreenUpdating = False
            i = Cells(Rows.Count, "A").End(xlUp).Row
            
          Range("B2:E2").AutoFill Destination:=Range("B2:E" & i)
          Range("F2").AutoFill Destination:=Range("F2:F" & i)
          Range("G2").AutoFill Destination:=Range("G2:G" & i)
          Range("H2").AutoFill Destination:=Range("H2:H" & i)
          Range("I2").AutoFill Destination:=Range("I2:I" & i)
          Range("J2").AutoFill Destination:=Range("J2:J" & i)
          Range("K2").AutoFill Destination:=Range("K2:K" & i)
          Range("L2").AutoFill Destination:=Range("L2:L" & i)
          Range("M2").AutoFill Destination:=Range("M2:M" & i)
          Range("N2").AutoFill Destination:=Range("N2:N" & i)
          Range("O2").AutoFill Destination:=Range("O2:O" & i)
    
    End Sub
    Which works like a charm. Again, many thanks.
    So did my original macro fail due to cell selection ?

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