+ Reply to Thread
Results 1 to 16 of 16

Assistance with getting Loop to change sheets

  1. #1
    Registered User
    Join Date
    11-07-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Assistance with getting Loop to change sheets

    Hi,

    So I'm attempting to write a macro that will look for the next empty cell below D7 and insert a vlookup into that cell which it will then replace with values. The macro needs to do this in each sheet in the book exept the last 2, but at the moment it isn't changing sheets. It's just inserting the formula multiple times in the blank cell below D7 on the active sheet. Any ideas welcome:



    Please Login or Register  to view this content.
    Thanks!
    Last edited by MattMurdoch; 11-08-2013 at 05:55 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,755

    Re: Assistance with getting Loop to change sheets

    Maybe throw a Sheets(I).Select in after the For. Not the best code but it should work. Maybe later when I wake up properly I can have another look


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    11-07-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Assistance with getting Loop to change sheets

    I knew I was missing something simple!

    (Can you tell I'm new at this?)

    Thanks!!

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,755

    Re: Assistance with getting Loop to change sheets

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  5. #5
    Registered User
    Join Date
    11-07-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Assistance with getting Loop to change sheets

    d'oh

    Looks like I spoke to soon. It worked fine in my little test workbook, but in the live one (with 50 odd sheets) it throws up a Run-time error '1004': Select method of Worksheet class failed error after updating the third sheet.

    Will keep trying. Any further ideas welcome.

  6. #6
    Registered User
    Join Date
    11-07-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Assistance with getting Loop to change sheets

    Which I now realise is caused by there being about a dozen hidden sheets in the workbook.

    Is it possible to loop and skip hidden sheets?

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,755

    Re: Assistance with getting Loop to change sheets

    Thanks for the rep

    You have a test workbook? That would be good to see

    You can't select or activate a hidden sheet ... you can't do it manually. As you have discovered.

    So you need to trap the error using On Error Resume Next, or On Error Goto label. Probably the latter. Put the label on the line before Next I. Like:

    Please Login or Register  to view this content.

    Regards, TMS

  8. #8
    Registered User
    Join Date
    11-07-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Assistance with getting Loop to change sheets

    Thanks for getting back to me TMS.

    I ended up unhiding the sheets (found a macro online to do that) and moving them to the end of the workbook. I then re-hid them (no idea what they're for - I inherited the sheet) and ended the loop 14 sheets earlier.

    Works an absoulte treat.

    Thanks again. Final Code:

    Please Login or Register  to view this content.
    Last edited by MattMurdoch; 11-08-2013 at 05:54 AM.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,755

    Re: Assistance with getting Loop to change sheets

    Please Login or Register  to view this content.

    Regards, TMS

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,755

    Re: Assistance with getting Loop to change sheets

    Ah, should have said ... please use Code Tags. I really shouldn't answer questions if you don't comply with the rules. Oops. So, please, do me a favour and edit your posts (see below). And see my last post. It should cope with the hidden sheets ... in fact, it will actually put the formula in and convert it to a value. However, if you don't know what the sheets are, you might not want that to happen.

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

  11. #11
    Registered User
    Join Date
    11-07-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Assistance with getting Loop to change sheets

    Sorry about that - I was wondering why my post didn't look right.

    Thanks again!

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,755

    Re: Assistance with getting Loop to change sheets

    You're welcome. . And thanks.


    Note my last post will be faster and less harsh on the eyes as it doesn't select anything. You just need to adjust the sheet count as you have in your final version.


    Regards, TMS

  13. #13
    Registered User
    Join Date
    11-07-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Assistance with getting Loop to change sheets

    Brilliant! That updates instantly - my version took about 3 seconds to loop through all the sheets.

    I don't quite understand 'with' statements yet, but it's next on my learning list.

    What a great forum!

    Cheers.

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,755

    Re: Assistance with getting Loop to change sheets

    You could make your version quicker by switching ScreenUpdating off and on but that really just masks the fact that selecting sheets and cells is slow ... and unnecessary.

    Regards, TMS

  15. #15
    Registered User
    Join Date
    11-07-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Assistance with getting Loop to change sheets

    I've used your version for the final workbook.

    I knew my coding was a really long way to do it, but at this stage I'm still at the manipulating-recorded-macros-and-copying-stuff-I-find-online level.

    I'll get the hang of it... eventually.

  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,755

    Re: Assistance with getting Loop to change sheets

    I'm still at the manipulating-recorded-macros-and-copying-stuff-I-find-online level.

    I guess we all start there ... at least, I know I did. My learning style is to find something that works, adopt it, use it, understand it, adapt it and, hopefully, improve on it.

+ 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. For Loop assistance
    By Paliza in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-02-2012, 05:01 AM
  2. [SOLVED] VBA Assistance to print many named ranges on many sheets in a certain order
    By BertLady56 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2012, 02:57 PM
  3. Assistance with WorkSheet Change Event
    By ker9 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-24-2012, 12:31 PM
  4. Assistance with Loop
    By mauddib13 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-20-2008, 06:25 PM
  5. Loop & Offset assistance required
    By Digory in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-20-2007, 04:30 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