+ Reply to Thread
Results 1 to 15 of 15

Macro not running on all/correct sheet

  1. #1
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Macro not running on all/correct sheet

    Hi

    I have a workbook with two worksheets. The first worksheet, "Source", basically just has some editing done to it to end up with two columns, and the second worksheet, "Rates", does a vlookup from the first sheet.

    The problem I'm experiencing is that the code only works as its supposed to when "Source" is the active sheet. If I run with the "Rates" sheet as active, no editing is done to "Source" sheet. What do I need to do so that the user is able run the macro with the "Rates" sheet open, instead of having to launch it from the "Source" sheet? I plan on adding a button the to the ribbon.

    On a side note, when I only have the "Source" code in the book, the macro to edit runs in under a second. Once I add in the "Rates" code, it takes noticeably longer. Is it my code perhaps?

    Here's the code:

    Please Login or Register  to view this content.
    Thanks for the help!
    Last edited by Seraph84; 03-12-2013 at 08:28 AM.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Macro not running on all/correct sheet

    You need to ensure the sheet you want to process is active before running code against it. Alternatively, declare a couple of worksheet variables, set them to reference the sheets and use those references rather than relying on the activesheet being the sheet to process...

    Please Login or Register  to view this content.
    You need to qualify each reference to a range in the code.
    Last edited by cytop; 03-12-2013 at 06:57 AM.

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Macro not running on all/correct sheet

    by the way
    Please Login or Register  to view this content.
    needs to be
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro not running on all/correct sheet

    I have edited your code -
    Please Login or Register  to view this content.
    Also you do not need so many lrow variables. You can use just 1 and then have it re-calculate each time you need to use it.

    For e.g. this piece of code which i corrected
    Please Login or Register  to view this content.
    can be re-corrected / re-written as -
    Please Login or Register  to view this content.
    This way you do not have to remember which lrow variable points to which column.

    Also, you have to qualify each line when you use the "With" statement. For e.g. if you are using range, it should be .range. If you are using cells and rows, it should be .cells and .rows, respectively. Check my code and you will understand.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: Macro not running on all/correct sheet

    Ctop, JosephP, thanks I will try that. I thought the With Sheets (xxxx) would have worked, guess not.

    Arlu - I think you maybe mistunderstood my post (I also made an edit to it, sorry) - I still want to do a vlookup from the Rates sheet based on what is in the Source sheet. I see you removed reference to the Rates sheet? Can you perhaps explain what you did?

    Also, the With statements seem to work fine with only Range (instead of .Range etc, though I understand what you are saying.) Also thanks for the tip about the lrow variable.

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro not running on all/correct sheet

    Since i already referred to the Rates sheet at the top of the code, i removed the repeated references to them. The changes that i did were - just adding the . where required and removing some un-wanted With and End With statements.

    If you do not include the "." before the range, and by chance if your current sheet changes to something else, the actions will be performed on that sheet instead of the current sheet.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Macro not running on all/correct sheet

    Quote Originally Posted by Seraph84 View Post
    Also, the With statements seem to work fine with only Range (instead of .Range etc, though I understand what you are saying.)
    that's only if the right sheet happens to be active-you ought to always use the .Range notation

  8. #8
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: Macro not running on all/correct sheet

    Sorry, I see where the confusion is coming in. The editing should be done on the Source sheet. And the vlookup and some minor editing is all that is required on the Rates sheet. I will edit the code in my post.

    Apologies!

  9. #9
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: Macro not running on all/correct sheet

    Ok I've modified my code as suggested and added the below:

    Please Login or Register  to view this content.
    But now I now I get Error 424 (Object Required) with this piece of code (I assume it would be the same for my other variables):

    Please Login or Register  to view this content.
    What am I doing wrong here?

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro not running on all/correct sheet

    Try this
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: Macro not running on all/correct sheet

    Thanks Arlu, but i'm still getting the error?

  12. #12
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro not running on all/correct sheet

    It worked fine at my end.

    Why dont you try with a dummy file and see if it works? I tried with one and it did.

  13. #13
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: Macro not running on all/correct sheet

    Ok got it to work so everything on the Source sheet works as coded......but now my vlookup on the Rates sheet does not populate the range as specified. It only populates cells G1:2, instead of from G2 till the last row? And Column A is not deleted?

    Please Login or Register  to view this content.
    Last edited by Seraph84; 03-13-2013 at 09:49 AM.

  14. #14
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Macro not running on all/correct sheet

    remove the lrow12 part and use
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    09-25-2011
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    45

    Re: Macro not running on all/correct sheet

    Excellent! That seems to have worked, thanks so much everyone for the help!

+ 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