+ Reply to Thread
Results 1 to 5 of 5

Passing a Range to a Subroutine using CELLS isn't working

  1. #1
    Forum Contributor
    Join Date
    01-28-2010
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2013
    Posts
    200

    Passing a Range to a Subroutine using CELLS isn't working

    I have the following code to run a sub:

    Please Login or Register  to view this content.
    which I want to replace with
    Please Login or Register  to view this content.
    to make it dynamic.

    When I try to run it, I get the following error

    Run-Time Error '1004': Method 'Range' of object '_Worksheet' failed.

    Not sure what I am doing wrong as it works with:

    Please Login or Register  to view this content.
    Thank you.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Passing a Range to a Subroutine using CELLS isn't working

    In the first 2 lines of code you don't have a worksheet reference for Cells, kind of surprised the first one worked - wsCalc must have been the active sheet when you ran it.

    Try this.
    Please Login or Register  to view this content.
    Or this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Passing a Range to a Subroutine using CELLS isn't working

    What's the value of lCol at time of error?
    Last edited by leelnich; 06-23-2017 at 11:21 AM.

  4. #4
    Forum Contributor
    Join Date
    01-28-2010
    Location
    Pittsburgh, PA
    MS-Off Ver
    Excel 2013
    Posts
    200

    Re: Passing a Range to a Subroutine using CELLS isn't working

    Quote Originally Posted by Norie View Post
    In the first 2 lines of code you don't have a worksheet reference for Cells, kind of surprised the first one worked - wsCalc must have been the active sheet when you ran it.

    Try this.
    Please Login or Register  to view this content.
    Or this.
    Please Login or Register  to view this content.
    Adding that extra reference to wsCalc made the difference. Hardest thing for a VBA newbie is understanding when you can (and should) reference up the chain of the object model and when you don't need to.

    Thanks for your help.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Passing a Range to a Subroutine using CELLS isn't working

    You should fully reference things all the time.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Passing a range from one subroutine to another
    By j_Southern in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-25-2013, 06:34 AM
  2. passing variables to subroutine from userform
    By PD3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-07-2013, 02:11 AM
  3. Question about passing array to subroutine......
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-21-2012, 08:19 PM
  4. Passing Object Array to a Subroutine
    By Farmer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-21-2011, 03:09 PM
  5. Passing subroutine as a parameter
    By jartzh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-26-2009, 03:39 PM
  6. Passing a worksheet into a subroutine
    By dmartindale in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2007, 10:41 AM
  7. Passing parameters to a subroutine in a different workbook
    By munzer1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-09-2007, 03:34 AM
  8. Passing Module to subroutine
    By SixSigmaGuy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-25-2006, 06:10 PM

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