+ Reply to Thread
Results 1 to 10 of 10

Code running on wrong sheet.

  1. #1
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Code running on wrong sheet.

    Please Login or Register  to view this content.
    I am looping through each sheet in a workbook and selecting based on the last 5 characters in a sheet name.
    All works as required, however, if I comment out the ".activate" line of my code then it runs on whatever the active sheet is.

    My question: is 'with sheet' supposed to activate the sheet? If not, then what is the point of having 'with sheet' in the code if the code runs on whatever the activesheet is?

    Edit: just to clarify, the activesheet is the sheet that is active at the beginning of the code. i.e. the code is looping through the sheets but the active sheet is not looping.
    Last edited by anrichards22; 07-13-2015 at 02:20 PM.

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Code running on wrong sheet.

    "with sheet" is so that if you have a bunch of code for that sheet, you don't have to type sheets(i)..... all the time... inside the with you can say stuff like .activate or .range and it knows that before the period is Sheets(i).
    You need to activate it probably because your called code does not reference specific sheets only ranges. It is not good practice to to that when working with multiple sheets, which is why often people dim the sheet at the start and then include it in their code.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code running on wrong sheet.

    You do not need to use "Active". Active is the default value.

    With Sheets(i) is the active sheet, but your cursor has to be on the active sheet before you run your code as you have not specified the sheet name. VBA assumes the active sheet happen to be where ever the cursor is.

  4. #4
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    853

    Re: Code running on wrong sheet.

    Do you need to count the sheets? You can just loop through each one like below?

    I had learned also if you want to call a variable name in a Macro Call, you need use Application.Run as well

    Perhaps try this?
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Code running on wrong sheet.

    Thanks for all your assistance, I think I understand it a little better now.



    Quote Originally Posted by ptmuldoon View Post
    Do you need to count the sheets?
    I don't need to count sheets, I am in the process of rewriting some code and counting the sheets was required in my old code.
    Thanks.

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

    Re: Code running on wrong sheet.

    Try changing cch45_parse to take a sheet as an argument and make sure you reference the passed sheet properly in the sub's code.
    If posting code please use code tags, see here.

  7. #7
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Code running on wrong sheet.

    Quote Originally Posted by Norie View Post
    Try changing cch45_parse to take a sheet as an argument and make sure you reference the passed sheet properly in the sub's code.

    You mean like?
    Please Login or Register  to view this content.
    The first line of my code for cch45_parse sets the worksheet variable
    Please Login or Register  to view this content.
    , the rest of the the code is
    Please Login or Register  to view this content.
    but that wasn't giving the desired result.

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Code running on wrong sheet.

    based on ptmuldoon's excellent suggestion:

    Please Login or Register  to view this content.

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

    Re: Code running on wrong sheet.

    Can you post the code for cch45_parse?

  10. #10
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Code running on wrong sheet.

    Quote Originally Posted by Norie View Post
    Can you post the code for cch45_parse?
    The code for cch45_parse is simply just find and replace 150 different text strings.

+ 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. [SOLVED] displays wrong sheet after running a macro
    By OAKLEY in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-30-2014, 03:07 PM
  2. [SOLVED] VBA code running in wrong order and not executing properly
    By ollylondon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-21-2012, 10:11 AM
  3. Code working but on wrong sheet
    By pauldaddyadams in forum Excel General
    Replies: 6
    Last Post: 11-11-2011, 07:29 AM
  4. Running Sheet Code From Within A Userform Sub
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-22-2010, 01:52 AM
  5. Copy/paste-button - wrong reference code in sheet 2
    By Krapter in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-08-2009, 05:17 AM
  6. Code running in wrong sheet
    By cgeistman in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-01-2008, 03:39 AM
  7. [SOLVED] Processing code on wrong sheet
    By Jeff Klein in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-03-2005, 03:06 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