+ Reply to Thread
Results 1 to 4 of 4

AutoFill Funtion Error

  1. #1
    DM
    Guest

    AutoFill Funtion Error

    I'm not an experienced VBA programmer and need someone's help. The code
    below comes from a recorded macro, with some ideas from the VBA help files,
    and I've attempted to modify it to be more generic and not associated with a
    specific row or sheet.

    After modifying a cell's formula to reflect a new sheet name, I'm attempting
    to copy it across several columns, as follows (newRow is Dim'ed as an
    integer and is valid when I run the code):

    newFormula = "='" & sheetName & "'!K$2"
    Cells(newRow, 4).formula = newFormula
    ActiveSheet.Cells(newRow, 4).Select
    Selection.AutoFill Destination:=Range(Cells(newRow, 5), Cells(newRow,
    9)), Type:=xlFillDefault

    I get a run-time error 1004 - Autofill method of Range class failed. If I
    debug the code and hover the cursor over the various elements, I get
    "Cells(newRow, 5)=Error 2023".

    What am I doing incorrectly and where can I find the definitions of the
    reported error codes? I've searched help and searched the MS website
    without success.

    TIA

    DJM



  2. #2
    Tom Ogilvy
    Guest

    Re: AutoFill Funtion Error

    The destination range must include the source range. So

    Sub AC()
    sheetname = "ABCD"
    newRow = 6
    newFormula = "='" & sheetname & "'!K$2"
    Cells(newRow, 4).Formula = newFormula
    ActiveSheet.Cells(newRow, 4).Select
    Selection.AutoFill Destination:=Range(Selection, _
    Cells(newRow, 9)), Type:=xlFillDefault

    End Sub


    ? cvErr(xlErrRef)
    Error 2023

    so this is the worksheet reference error.


    --
    Regards,
    Tom Ogilvy


    "DM" <not_correct@nowhere.com> wrote in message
    news:uzk3jQA2FHA.3804@TK2MSFTNGP10.phx.gbl...
    > I'm not an experienced VBA programmer and need someone's help. The code
    > below comes from a recorded macro, with some ideas from the VBA help

    files,
    > and I've attempted to modify it to be more generic and not associated with

    a
    > specific row or sheet.
    >
    > After modifying a cell's formula to reflect a new sheet name, I'm

    attempting
    > to copy it across several columns, as follows (newRow is Dim'ed as an
    > integer and is valid when I run the code):
    >
    > newFormula = "='" & sheetName & "'!K$2"
    > Cells(newRow, 4).formula = newFormula
    > ActiveSheet.Cells(newRow, 4).Select
    > Selection.AutoFill Destination:=Range(Cells(newRow, 5), Cells(newRow,
    > 9)), Type:=xlFillDefault
    >
    > I get a run-time error 1004 - Autofill method of Range class failed. If I
    > debug the code and hover the cursor over the various elements, I get
    > "Cells(newRow, 5)=Error 2023".
    >
    > What am I doing incorrectly and where can I find the definitions of the
    > reported error codes? I've searched help and searched the MS website
    > without success.
    >
    > TIA
    >
    > DJM
    >
    >




  3. #3
    DM
    Guest

    Re: AutoFill Funtion Error

    Tom, many thanks. I guess if I had read the help just a little more
    carefully, I would have seen it :-)

    Now that it works, I've noticed another annoyance. The formating of the
    source cell is being copied so I will need to use the PasteSpecial method
    instead, specifying the xlPasteFormulas type. Does this work the same way
    wrt range? I realize I'll need to Copy before PasteSpecial.

    DJM

    "Tom Ogilvy" <twogilvy@msn.com> wrote in message
    news:OHvBGiA2FHA.980@TK2MSFTNGP14.phx.gbl...
    > The destination range must include the source range. So
    >
    > Sub AC()
    > sheetname = "ABCD"
    > newRow = 6
    > newFormula = "='" & sheetname & "'!K$2"
    > Cells(newRow, 4).Formula = newFormula
    > ActiveSheet.Cells(newRow, 4).Select
    > Selection.AutoFill Destination:=Range(Selection, _
    > Cells(newRow, 9)), Type:=xlFillDefault
    >
    > End Sub
    >
    >
    > ? cvErr(xlErrRef)
    > Error 2023
    >
    > so this is the worksheet reference error.
    >
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "DM" <not_correct@nowhere.com> wrote in message
    > news:uzk3jQA2FHA.3804@TK2MSFTNGP10.phx.gbl...
    >> I'm not an experienced VBA programmer and need someone's help. The code
    >> below comes from a recorded macro, with some ideas from the VBA help

    > files,
    >> and I've attempted to modify it to be more generic and not associated
    >> with

    > a
    >> specific row or sheet.
    >>
    >> After modifying a cell's formula to reflect a new sheet name, I'm

    > attempting
    >> to copy it across several columns, as follows (newRow is Dim'ed as an
    >> integer and is valid when I run the code):
    >>
    >> newFormula = "='" & sheetName & "'!K$2"
    >> Cells(newRow, 4).formula = newFormula
    >> ActiveSheet.Cells(newRow, 4).Select
    >> Selection.AutoFill Destination:=Range(Cells(newRow, 5), Cells(newRow,
    >> 9)), Type:=xlFillDefault
    >>
    >> I get a run-time error 1004 - Autofill method of Range class failed. If
    >> I
    >> debug the code and hover the cursor over the various elements, I get
    >> "Cells(newRow, 5)=Error 2023".
    >>
    >> What am I doing incorrectly and where can I find the definitions of the
    >> reported error codes? I've searched help and searched the MS website
    >> without success.
    >>
    >> TIA
    >>
    >> DJM
    >>
    >>

    >
    >




  4. #4
    Tom Ogilvy
    Guest

    Re: AutoFill Funtion Error

    No, you only need to include the destination, at the same time, since the
    ranges are contiguous, you can include the source range as well.

    You can actually skip both and just do


    newFormula = "='" & sheetName & "'!K$2"
    Range(Cells(newRow, 4), Cells(newRow, 9)).Formula = _
    newFormula

    --
    Regards,
    Tom Ogilvy


    "DM" <not_correct@nowhere.com> wrote in message
    news:O1mxSoC2FHA.1256@TK2MSFTNGP09.phx.gbl...
    > Tom, many thanks. I guess if I had read the help just a little more
    > carefully, I would have seen it :-)
    >
    > Now that it works, I've noticed another annoyance. The formating of the
    > source cell is being copied so I will need to use the PasteSpecial method
    > instead, specifying the xlPasteFormulas type. Does this work the same way
    > wrt range? I realize I'll need to Copy before PasteSpecial.
    >
    > DJM
    >
    > "Tom Ogilvy" <twogilvy@msn.com> wrote in message
    > news:OHvBGiA2FHA.980@TK2MSFTNGP14.phx.gbl...
    > > The destination range must include the source range. So
    > >
    > > Sub AC()
    > > sheetname = "ABCD"
    > > newRow = 6
    > > newFormula = "='" & sheetname & "'!K$2"
    > > Cells(newRow, 4).Formula = newFormula
    > > ActiveSheet.Cells(newRow, 4).Select
    > > Selection.AutoFill Destination:=Range(Selection, _
    > > Cells(newRow, 9)), Type:=xlFillDefault
    > >
    > > End Sub
    > >
    > >
    > > ? cvErr(xlErrRef)
    > > Error 2023
    > >
    > > so this is the worksheet reference error.
    > >
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > > "DM" <not_correct@nowhere.com> wrote in message
    > > news:uzk3jQA2FHA.3804@TK2MSFTNGP10.phx.gbl...
    > >> I'm not an experienced VBA programmer and need someone's help. The

    code
    > >> below comes from a recorded macro, with some ideas from the VBA help

    > > files,
    > >> and I've attempted to modify it to be more generic and not associated
    > >> with

    > > a
    > >> specific row or sheet.
    > >>
    > >> After modifying a cell's formula to reflect a new sheet name, I'm

    > > attempting
    > >> to copy it across several columns, as follows (newRow is Dim'ed as an
    > >> integer and is valid when I run the code):
    > >>
    > >> newFormula = "='" & sheetName & "'!K$2"
    > >> Cells(newRow, 4).formula = newFormula
    > >> ActiveSheet.Cells(newRow, 4).Select
    > >> Selection.AutoFill Destination:=Range(Cells(newRow, 5),

    Cells(newRow,
    > >> 9)), Type:=xlFillDefault
    > >>
    > >> I get a run-time error 1004 - Autofill method of Range class failed.

    If
    > >> I
    > >> debug the code and hover the cursor over the various elements, I get
    > >> "Cells(newRow, 5)=Error 2023".
    > >>
    > >> What am I doing incorrectly and where can I find the definitions of the
    > >> reported error codes? I've searched help and searched the MS website
    > >> without success.
    > >>
    > >> TIA
    > >>
    > >> DJM
    > >>
    > >>

    > >
    > >

    >
    >




+ 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