+ Reply to Thread
Results 1 to 14 of 14

VBA loop through all open workbooks and stop at first

  1. #1
    Registered User
    Join Date
    09-20-2017
    Location
    Hemel Hempstead
    MS-Off Ver
    Office 2007
    Posts
    15

    VBA loop through all open workbooks and stop at first

    Hi
    I have a macro that is finding a specific product code and deleting the entire row, I then need the macro to loop through all open workbooks and do the same thing if the product code exists but I am struggling to get it to loop through?

    It does the first find and delete but then does not carry on through the workbooks.

    If possible I also want it to keep looping until it gets back to the first workbook where the macro was started?

    Here is what I have so far:

    Please Login or Register  to view this content.

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA loop through all open workbooks and stop at first

    Hi JM0308,
    maybe so
    Please Login or Register  to view this content.
    Last edited by nilem; 09-20-2017 at 03:51 AM.

  3. #3
    Registered User
    Join Date
    09-20-2017
    Location
    Hemel Hempstead
    MS-Off Ver
    Office 2007
    Posts
    15

    Re: VBA loop through all open workbooks and stop at first

    Thanks for your quick reply! I am getting an error on this. Not sure if it is to do with the sheet name? all sheets are named differently?

    First of all it doesn't find the product code and then it debugs on the
    Please Login or Register  to view this content.
    does this also stop when it gets to the sheet it started with?
    Last edited by JM0308; 09-20-2017 at 03:57 AM.

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA loop through all open workbooks and stop at first

    Yes, I have corrected my post
    Please Login or Register  to view this content.
    added
    The product code is searched only on the first sheet of each book
    Please Login or Register  to view this content.
    Last edited by nilem; 09-20-2017 at 04:12 AM.

  5. #5
    Registered User
    Join Date
    09-20-2017
    Location
    Hemel Hempstead
    MS-Off Ver
    Office 2007
    Posts
    15

    Re: VBA loop through all open workbooks and stop at first

    Hi nilem,
    thank you this is now working and deleting all the rows in my open worksheets. The only issue is now even if the product code does appear on all of my open spreadsheets I get the box come up saying "that name could not be found".
    Could this be to do with the personal.xls?

    There is another thing that would be amazing if it could be done! Is there a way to make it do this: once the product code has been found and selected, If the cell above the product is blank delete this row too?

    Thank you for your help

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA loop through all open workbooks and stop at first

    Quote Originally Posted by JM0308 View Post
    ... the product code does appear on all of my open spreadsheets...
    Product code can be located on several (on any)sheets of the book?

  7. #7
    Registered User
    Join Date
    09-20-2017
    Location
    Hemel Hempstead
    MS-Off Ver
    Office 2007
    Posts
    15

    Re: VBA loop through all open workbooks and stop at first

    I only need the macro to look for the product code on the active worksheet of all the open workbooks, the workbooks do not have more than 1 worksheet. The product code could also not appear in the workbooks and of course at this point it would be ok to display the message but when I am testing I know the product code is on every sheet.

    I will open around 50 files, the product code I search for could be on all 50 it could be on 40 of them, I need it to keep looping through all the workbooks even if it doesn't find the code on one of them then stop when it gets back to the first workbook where I called the macro on.

    Hope this makes sense

  8. #8
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA loop through all open workbooks and stop at first

    try it
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    09-20-2017
    Location
    Hemel Hempstead
    MS-Off Ver
    Office 2007
    Posts
    15

    Re: VBA loop through all open workbooks and stop at first

    Hi Nilem,

    amazing!! thank you. this has thrown something new into my logic that I had not thought of though.

    Where now it is doing: if the cell above is blank then delete that too.
    Can you make it so it does: if the cell to the right is blank then delete the row above the selected cell (product Code)?

    sorry to change at the last minute but I didn't see this being an issue until testing

  10. #10
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA loop through all open workbooks and stop at first

    change this line
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    09-20-2017
    Location
    Hemel Hempstead
    MS-Off Ver
    Office 2007
    Posts
    15

    Re: VBA loop through all open workbooks and stop at first

    Genius!

    I amended the code slightly as I needed it to be AND instead of OR but you were not to know this! It works brilliantly now

    I have just had a thought, I do not want the message box to appear every time it fails, in this macro is there a way that we can count how many times it has successfully completed the delete and how many times it has not managed to find the product code and display this in a message at the end of the macro?

    "Product Codes removed:
    X Successful
    Y Unsuccessful"

  12. #12
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA loop through all open workbooks and stop at first

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    09-20-2017
    Location
    Hemel Hempstead
    MS-Off Ver
    Office 2007
    Posts
    15

    Re: VBA loop through all open workbooks and stop at first

    Hi Nilem,

    You are brilliant thank you. It works perfectly. Thank you very much

  14. #14
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA loop through all open workbooks and stop at first

    You are welcome

+ 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] loop through open workbooks and replace values
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-06-2017, 10:11 PM
  2. [SOLVED] Open 2 related workbooks in a loop.
    By swallis in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-27-2015, 07:14 PM
  3. Loop Through Open Xcel Workbooks
    By pablowilks in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-14-2013, 03:19 PM
  4. Open workbooks with variables in filename using loop
    By as1234 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-31-2011, 01:17 PM
  5. [SOLVED] When I open Excel, workbooks open automatically. How can I stop t
    By Rhealbird in forum Excel General
    Replies: 2
    Last Post: 02-23-2006, 06:15 AM
  6. [SOLVED] Loop through, open, and modify all workbooks in file
    By Jason in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-28-2005, 05:05 PM
  7. Loop Through All Open Workbooks
    By scott in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-31-2005, 11: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