Results 1 to 4 of 4

Thread: Excel VBA Column Loop

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178
    Hi all,

    Here is my problem:

    I am trying to loop through a number of columns and in each column autofill down. Here is the code I am trying to use

    Code:
    for i = 1 to Nocards
    
    TopCell = cells(rowcount, i + 1).address
    Bottomcell = cells(rowcount + 1, i + 1).address
    Insert_which = Topcell & ":" & Bottomcell
    Range(Insert_which).select
    selection.Autofill Destination:=Range(Insert_Which), Type:=Xlfilldefault
    
    next i
    This doesn't work !! I have tried to address the columns and everything...Now starting to bang head against a wall !!

    Anybody got any ideas on a better way or some way to modify the above code. Using the columns property returns a number and not the corresponding letter i.e.

    Code:
    TopCell = cells(rowcount, i + 1).column
    returns a 2, and not B

    Please help !! Any ideas greatly welcomed

    Steve

  2. #2
    Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    53
    It's not entirely clear what you are trying to do, but the following line of code will autofill down the first three columns of sheet1 for ten rows:

    dim s as worksheet

    Set s = Application.Sheets("Sheet1")
    s.Range("A1:C2").AutoFill Destination:=s.Range("A1:c10")

    Hopefully this will help!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Sorry !!

    I have just realised that you can autofill over more than one column (so I don't have to loop).

    Problem solved but thanks for the response

    Cheers
    Steve

  4. #4
    Fanatic Member Gaffer's Avatar
    Join Date
    Nov 2000
    Location
    London
    Posts
    828
    OK, here’s a solution, but is not based on AutoFill, but FillDown. I have also removed A1 type references to the cells and used row column references (I find them easier to use in code).

    Code:
     Dim nocards, rowcount
    Dim topX, topY, botX, botY
    
    For i = 1 To nocards
        topX = rowcount
        topY = i + 1
        botX = rowcount + 1
        botY = topY
            
        Range(Cells(topX, topY), Cells(botX, botY)).Select
        
        Selection.FillDown
    
    Next I
    I hope this saves your bruised head…

    Gaffer

Posting Permissions

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



Click Here to Expand Forum to Full Width