+ Reply to Thread
Results 1 to 8 of 8

How to be sure which sheet the code refers to

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    How to be sure which sheet the code refers to

    I am having great difficulty with this code:-
    Please Login or Register  to view this content.
    I have been getting an error with Line 10 when line 5 was simply
    Please Login or Register  to view this content.
    so after a little research I took advice and dimmed and set Workbook and Worksheet.
    THen the code as presented did seem to work, at least it was not throwing error messages.
    However nothing turned up at Sheet4 C1.
    Looking further I found that the code Copied the relevant range on sheet4 (Which is empty) and therefore pasted nothing at C1.
    I cant understand how my code has told it to do that! Please help
    John

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to be sure which sheet the code refers to

    Please Login or Register  to view this content.
    Last edited by bakerman2; 10-08-2019 at 11:06 AM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: How to be sure which sheet the code refers to

    Your With block isn't doing anything because you haven't used .Range and .Cells to anchor those properties back to the object reference held by the With statement. It should have been:

    Please Login or Register  to view this content.
    or you could use ws in place of Sheets("Sheet1") in the With block.
    Rory

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: How to be sure which sheet the code refers to

    Please Login or Register  to view this content.
    ws is already declared and set as worksheet object, so you don't need to pass it as argument to Worksheets() collection.

    You should qualify both range and cells with period (i.e. ws.) to avoid error. When sheet other than ws is active, if range only is qualified with worksheet object, it can throw error by passing cell's parent sheet (which can be different from ws) to Range.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  5. #5
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: How to be sure which sheet the code refers to

    Thank you all for your interest and help. Essentially you all gave me the same answer, which was encouraging. Any way I have incorporated it into my code and it works.
    I have a related hole in my knowledge which has come up in your replies. This is when to use Range(Cells and when to use Range(.Cells I frequently got an error referring to this when struggling with this bit of code.
    John

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: How to be sure which sheet the code refers to

    If it's inside a With block holding a sheet (or even range) reference, and the range should refer to that reference, then both Range and Cells should have a dot in front of them. (they should always match, so either all with a dot, or all without, not mix and match!)

  7. #7
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: How to be sure which sheet the code refers to

    I Thanks for that. I get the feeling that there is a big black hole in my understanding of the fundamentals of vba/Excel.
    For instance after I had seen your post I carried on with the next part of the project withy the following code:-
    Please Login or Register  to view this content.
    Of the three versions only that with line 30 works. The other two give error 1004 Application or Object defined error
    Yet this seems to be exactly the setup that you were describing where 20 should have worked.
    John

  8. #8
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: How to be sure which sheet the code refers to

    It's nothing to do with the With block. Range accepts as arguments:

    1 or 2 strings (names/addresses of ranges); or
    2 range objects.

    You are trying to pass one range object, which is invalid (and unnecessary). You'd use this instead:

    Please Login or Register  to view this content.

+ 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. Makro refers to the previous sheet instead of exact sheet
    By Raku_moromete in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-25-2019, 05:32 AM
  2. [SOLVED] Formula Refers to Sheet not in Workbook!...HELP!
    By WaylettChris in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-27-2017, 09:47 PM
  3. [SOLVED] VBA - See if a cell has a formula that refers to another sheet
    By XSQUAD in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-06-2017, 11:01 AM
  4. Replies: 3
    Last Post: 01-06-2016, 10:04 AM
  5. [SOLVED] hyperlink refers to another sheet
    By kaan in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-16-2015, 03:15 PM
  6. [SOLVED] Formula that refers to a cell on a sheet that may or may not exist.
    By k1989l in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-01-2015, 02:50 PM
  7. Forumla which refers to a cell which refers to a sheet name
    By awiller2 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 12-03-2012, 09:57 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