+ Reply to Thread
Results 1 to 8 of 8

Looping through cell ranges in multiple worksheets and prevent saving if cell value empty

  1. #1
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Looping through cell ranges in multiple worksheets and prevent saving if cell value empty

    Hi. I have a workbook with 8 sheets. I have 5 sheets that are called WO1, WO2, WO3, WO4, WO5. I want to loop through these 5 sheets and look at a certain range of cells to see if there is a value in any of those cells. IF there is and there is not a value in the next cell over, i want it to prevent saving and pop up with a message box asking the user to fill in all required fields in the worksheet that is missing information. Here is an example of my current code:

    Please Login or Register  to view this content.
    I tried defining strSection and strCustSparePart as a range and i got an "Object variable or With block variable not set" error on either line with the Choose function.

    When i leave it as is i get an error on the CountA lines saying "Application-defined or object defined error". This is most likely because i am trying to call a string variable as a range?

    Any help is appreciated!
    Last edited by Spritz; 04-19-2013 at 10:56 AM.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Looping through cell ranges in multiple worksheets and prevent saving if cell value em

    The reason you got the error code when trying to define it as a range is because with objects and ranges you need to use the word "Set" like this

    Please Login or Register  to view this content.
    With that being said, if it were me doing this I would be approaching it completely different. Can you be more specific on which ranges you want to confirm there is a value and if there is a value, what range specifically cannot be blank. Please be very specific.

  3. #3
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Re: Looping through cell ranges in multiple worksheets and prevent saving if cell value em

    Hi stnkynts,

    Thanks for responding.

    The exact ranges can be found above. I want it to look through each sheet for any values in the ranges of ("C26:C33", "J26:J33", "P26:P33") and IF THERE IS a value in say C26, there MUST be a value in G26. If there isn't i want it to show an error/msgbox when the user tries to save indicating they need to input information in G26.

    So it goes as follows:

    Looks for values in C26:C33 and looks for a corresponding value in G26:G33
    Same with the others stated above in the loops. J/M, P/U

    Does this make sense?

    I ended up figuring most of it out on my own and everything is working as intended. I would like to fix the msg box so that it will display what value is missing a yes/no from which worksheet. This is my current code:

    Please Login or Register  to view this content.
    any ideas?
    Last edited by Spritz; 04-19-2013 at 02:11 PM.

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Looping through cell ranges in multiple worksheets and prevent saving if cell value em

    Try this: I didn't have time to test it nor write into a before save but i assumed based upon what you submitted you could figure that part out.

    Please Login or Register  to view this content.
    Edit: unfortunately it errored and I don't have time to figure out why. I will look at it when i get back this weekend.
    Last edited by stnkynts; 04-19-2013 at 05:28 PM.

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Looping through cell ranges in multiple worksheets and prevent saving if cell value em

    Ok fixed it. I accidentally set the array as a string instead of variant. Give this a try:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Re: Looping through cell ranges in multiple worksheets and prevent saving if cell value em

    Hi stnkynts,

    Thanks for your help so far!

    So far this works great but i noticed that if you are missing information in multiple cells that it will only display a message box for the first one found. Is there a way i can get it to show individual messages for each one or one big one with all the missing ones?

    Also for the message box, is there a way i can get it to say something like "Please choose Yes/No for Customer Spare Part for the part number " _____ and have it actually copy the value that is in cell C26? So people know what Part number it is for?

  7. #7
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Looping through cell ranges in multiple worksheets and prevent saving if cell value em

    So far this works great but i noticed that if you are missing information in multiple cells that it will only display a message box for the first one found. Is there a way i can get it to show individual messages for each one or one big one with all the missing ones?
    Yes. Just remove the 3 "Exit Sub". That line is what stops the code from going further to show all the empty cells.

    Also for the message box, is there a way i can get it to say something like "Please choose Yes/No for Customer Spare Part for the part number " _____ and have it actually copy the value that is in cell C26? So people know what Part number it is for?
    Yes. See below amended code.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Re: Looping through cell ranges in multiple worksheets and prevent saving if cell value em

    Thanks for all your help stnkynts!

    Works perfect.

    Here is the full code:

    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)

Tags for this Thread

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