1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Code Error when filling Dynamic Range

Discussion in 'Business Applications' started by FLOVETT, Nov 10, 2011.

Thread Status:
Not open for further replies.
  1. FLOVETT

    FLOVETT Thread Starter

    Joined:
    Sep 17, 2007
    Messages:
    79
    First Name:
    Felton
    I am doing a VLOOKUP and only want to fill to a certain range - The number of blank rows appearing in column "d" as the number of rows will always vary. I have tried the following code and the vlookup places the first value in cell e2, but it stops at the Selection.Autofill line. I am getting a Run-time error Autofill method of Range class failed. Here is my code. I am using the t to count the number of rows.
    Code:
     Sub test_autofill()
    Dim t As Long
    t = Cells(Rows.Count, "d").End(xlUp).Row
    Range("e2").Select
        ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],catcode.xls!CVA_PERCENT,2,0)"
        Selection.AutoFill Destination:=Range("e2" & t), Type:=xlFillDefault
        Range("e2" & t).Select
            MsgBox "please remove OT services for all customers except EQP & GSA"
    
    End Sub                
     
  2. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    Trying using the line below instead.

    Code:
    Selection.AutoFill Destination:=Range("E2:E" & t)
    Rollin
     
  3. FLOVETT

    FLOVETT Thread Starter

    Joined:
    Sep 17, 2007
    Messages:
    79
    First Name:
    Felton
    Thanks,

    I had a little trouble at first, but it works perfectly. I will have to ammend a lot of my code using this structure. I was filling in lots of extra rows and then deleting them.. Thanks
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1026311

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice