+ Reply to Thread
Results 1 to 6 of 6

AutoFill code creates error

Hybrid View

  1. #1
    Registered User
    Join Date
    04-13-2004
    Posts
    74

    AutoFill code creates error

    I am trying to copy a cell in row 'A' down to a variable number of rows. The code generates the error message: "AutoFill method of range class failed". y and z are declared Integer's. The applicable portion of the code is below. Basically, find 'z', do a couple of copy pastes, then find 'y', then do the autofill. It's the last line that causes the problem.

    All help is appreciated. Thanks, Cliff

     
    z = Workbooks("master.xlsm").Worksheets("sheet1").Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row + 4
    Workbooks("master.xlsm").Worksheets("sheet1").Range("A" & z) = b & "  " & Worksheets(c).Name
                'Puts target files name in Column A and row found above = z.
    Workbooks("master.xlsm").Worksheets("sheet1").Range("B" & z).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False   'Puts used range that was copied to the right of file name.
     y = Workbooks("master.xlsm").Worksheets("sheet1").Cells.Find(What:="*", _
        SearchDirection:=xlPrevious, _
        SearchOrder:=xlByRows).Row
    Workbooks("master.xlsm").Worksheets("sheet1").Range("A" & z).AutoFill Destination:=Range("A" & z & ":A" & y), Type:=xlFillDefault

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: AutoFill code creates error

    Try substituting this line:
    Workbooks("master.xlsm").Worksheets("sheet1").Cells(z, 1) Destination:=Range(Cells(z, 1), Cells(y, 1))
    Last edited by AlvaroSiza; 05-09-2012 at 04:41 PM.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Registered User
    Join Date
    04-13-2004
    Posts
    74

    Re: AutoFill code creates error

    Alvaro:

    Thanks for responding. The code you suggested created a compile error saying 'expected end of statement'. It there something wrong with the syntax?

  4. #4
    Registered User
    Join Date
    04-13-2004
    Posts
    74

    Re: AutoFill code creates error

    bump for solution

  5. #5
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: AutoFill code creates error

    I think Alvaro was missing a .Copy:

    Workbooks("master.xlsm").Worksheets("sheet1").Cells(z, 1).Copy Destination:=Range(Cells(z, 1), Cells(y, 1))

    In your original code, what is the value of y when you get the error? It looks like it should work...

  6. #6
    Registered User
    Join Date
    04-13-2004
    Posts
    74

    Re: AutoFill code creates error

    Thanks wallyeye. That was it. Missing .Copy

    y was like 233, but the problem was as you described above.


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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