+ Reply to Thread
Results 1 to 40 of 40

Looping an Excel Inputbox with Single Range

  1. #1
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Question Looping an Excel Inputbox with Single Range

    Hello my potential saviour,

    I have been driven to despair with what I thought would be a simple VBA loop but after countless hours and many different searches I'm at a loss so have turned to you to ask for help.

    Although I have found similar people with my issue, none of them have the exact same problems, so I thought I'd start a new topic and lay out the steps of what I'm after in the hope someone can assist me.

    Point 1. I am trying to use an application.inputbox to ask the user to select a single range
    Point 2. If the user presses 'Cancel' on the inputbox screen then a msgbox should display "Macro Cancelled" and the macro is ended.
    Point 3. If the user presses 'Ok' when the inputbox is blank or the user selects a cell without a formula in then a msgbox should display "No formula in the selected cell. Please select a cell with a formula." and the macro is looped.

    Note: If the user sees the message from Point 3 and loops back, pressing 'Cancel' now should still react like Point 2. (In my current code it just creates an endless loop - See below for more details)
    Likewise if the user doesn't input anything into the inputbox and presses 'Ok' then the default excel message shouldn't pop up because I would like the msgbox from point 3 to show instead.

    I have attached a sample workbook where range A1 contains a formula, Range A2 contains a value and all other cells are empty.
    The code I currently have works if the user presses 'Cancel' before attempting to select a range, however if the user selects a range that doesn't contain a formula they get caught in an endless loop even if they press 'Cancel'. Also my code doesn't handle if a user leaves the inputbox blank and presses 'Ok'

    This part of the code is part of a larger macro which actually asks a user for a 2nd input and then compares the run times of the two formulas. However I don't think that is needed so I have simplified the problem for now and hope to be able to scale it up.

    Thank you in advance and I'll be around if you have any further questions or need any extra info.
    EricDonk
    Attached Files Attached Files
    Last edited by EricDonk; 07-11-2024 at 04:03 PM.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping an Excel Inputbox with Single Range

    Hi Eric,

    Does this work for you:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by Trebor76 View Post
    Hi Eric,
    Does this work for you:
    Regards,
    Robert
    Thanks for the prompt reply Robert, I guess if my hand was forced I could just about accept this way of making it work but if possible I'd still like to find a way to get the 'Cancel' button to work after entering the loop.

    If I'm honest I can't understand how VBA is handling this because to me it reads that that the loop should try attempting the code again, which includes checking if the user presses 'Cancel' during the loop.

    Unfortunately your method also doesn't correct the fact the user sees a default excel error message when they do nothing and just press 'Ok' on the empty inputbox, because ideally I'd like: "No formula in the selected cell. Please select a cell with a formula." to be displayed in a msgbox and then the loop.

    Cheers again for your reply though, I was excited to try it and do thank you for your time.
    EricDonk

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping an Excel Inputbox with Single Range

    Hi Eric,

    I don't use loops too often as I can't tell you the number of times I've had to force Excel to close because it gets stuck in a perpetual loop.

    That said is this closer to the mark as it just leaves the input box open if ok is pressed but no cell has been selected:

    Please Login or Register  to view this content.
    Robert
    Last edited by Trebor76; 07-11-2024 at 06:57 PM.

  5. #5
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by Trebor76 View Post
    Hi Eric,
    I don't use loops too often as I can't tell you the number of times I've had to force Excel to close because it gets stuck in a perpetual loop.
    Actually let me try something else.
    Robert
    Tell me about it I have been stuck in a fair few infinite loops this week alone just trying to solve this, I can't for the life of me work out why it skips the line of code at the start once it's entered the loop, it must be because the value isn't nothing, but if it isn't nothing then what is it?! As for getting out of the endless loops, I tend to hold 'Esc' until I'm out because I often can't even close excel.

    Good luck with what you went back to try, hopefully you have more success than me because as you've probably realised your second attempt still does the infinity loop and doesn't use the custom error message for a blank entry into the inputbox.

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping an Excel Inputbox with Single Range

    Good luck with what you went back to try, hopefully you have more success than me because as you've probably realised your second attempt still does the infinity loop and doesn't use the custom error message for a blank entry into the inputbox.
    Just updated the solution. Let me know how it goes.

  7. #7
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by Trebor76 View Post
    Just updated the solution. Let me know how it goes.
    I appreciate the effort Robert, but sadly this only partial solves the second issue of a user leaving the inputbox blank and pressing 'Ok' because it just hides the error message rather than displaying a msgbox of my choosing.
    Similar to you first post in that if push came to shove then I could combine this with your Yes No msgbox box solution to have both issues partially resolved but if possible I'd love to get to the bottom of these 2 problems.

    Once again thanks for trying, when I started this macro I had no idea that come the end of the week that I'd still be working on the part I thought would be the easiest, but that the 'hard' part was finished on the first day. VBA will be the death of me I'm sure.
    EricDonk

  8. #8
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping an Excel Inputbox with Single Range

    leaving the inputbox blank and pressing 'Ok' because it just hides the error message rather than displaying a msgbox of my choosing.
    I could be wrong but that's your only option as the code just won't get to the next command even with On Error Resume Next and Application.DisplayAlerts = False
    Last edited by Trebor76; 07-11-2024 at 08:03 PM.

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping an Excel Inputbox with Single Range

    An userform with a ref edit control may do the trick (refer attached).
    Attached Files Attached Files

  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
    47,995

    Re: Looping an Excel Inputbox with Single Range

    I don't often use Labels, and GoTo Label, but it seems the best way here.

    Please Login or Register  to view this content.
    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


  11. #11
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping an Excel Inputbox with Single Range

    Hi Trevor,

    Nifty code but the same issue arises like for my code if OK is pressed but the text box is empty even though you've used On Error Resume Next.

    Robert

  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
    47,995

    Re: Looping an Excel Inputbox with Single Range

    I know. Got rid of most of the issues though. No infinite loops. Just don't keep pressing Enter without selecting a range

  13. #13
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by Trebor76 View Post
    I could be wrong but that's your only option as the code just won't get to the next command even with On Error Resume Next and Application.DisplayAlerts = False
    Quote Originally Posted by Trebor76 View Post
    An userform with a ref edit control may do the trick (refer attached).
    Thanks for getting back to me Robert, I'm starting to get onboard with the error message being taken away when the inputbox is left blank, it makes sense to just keep the user on that inputbox unless they press 'Cancel' or select a cell.
    Regarding the userform with Ref edit control, this is something I'll have to look into as I've not used it, cheers for the spreadsheet as a staring point though.

    Quote Originally Posted by TMS View Post
    I don't often use Labels, and GoTo Label, but it seems the best way here.
    Quote Originally Posted by TMS View Post
    I know. Got rid of most of the issues though. No infinite loops. Just don't keep pressing Enter without selecting a range
    TMS thanks you for joining this thread, when I read your second message that you had beaten the infinite loop I was excited to try it, however unless I'm doing something wrong I still get it.
    If the user selects a cell without a formula it produces the msgbox saying "No formula in the selected cell. Please select a cell with a formula." but then any further attempts to cancel the inputbox will now loop the user back to the same message rather than cancelling.

    If I use a separate subroutine or function, would it reset the error status so that the 'Cancel' button can be detected again, I'm really starting to think this is the issue. Then again I'm the new guy and I see you are a retired Excel/VBA Consultant so maybe you have a better understanding of why once in the loop the 'Cancel' button on the inputbox no longer works?

    Thanks for your time and hopefully we'll get there.
    EricDonk

  14. #14
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Please Login or Register  to view this content.
    I believe this amended code now solves the infinite loop.
    After my comment that that the error flag didn't seem to be resetting, I decided to play around with your code TMS and had the idea to set the inputRng to Nothing at the start of every loop. This way if the user presses cancel then 'Nothing' stays as the inputRng and therefore allows the inputbox to be cancelled at any point.

    Now I guess I need to play around with the Excel error when the inputbox is left blank, I'm hoping that if I can track down the err.number for it then I might be able to write something like if err.number = 277 Then msgbox "Input cannot be blank"

    Finally I noticed TMS added some code about more than one cell being selected, this was a clever catch and not something I had thought about. Now I may switch my code to distinguish the difference between a blank cell vs a cell with a value just so the error message can be more specific.

    We will get there, thanks for your messages and help so far TMS and Robert.
    Last edited by EricDonk; 07-12-2024 at 05:30 AM.

  15. #15
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping an Excel Inputbox with Single Range

    TMS thanks you for joining this thread, when I read your second message that you had beaten the infinite loop I was excited to try it, however unless I'm doing something wrong I still get it.
    If the user selects a cell without a formula it produces the msgbox saying "No formula in the selected cell. Please select a cell with a formula." but then any further attempts to cancel the inputbox will now loop the user back to the same message rather than cancelling.
    Hi Eric,

    Try putting this line of code...

    Please Login or Register  to view this content.
    ...immediately below this line:

    Please Login or Register  to view this content.
    Make sure to save the workbook before you try the revised code and have no other workbook open should you have to close Excel if it gets in an endless loop again.

    Regards,

    Robert

  16. #16
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by Trebor76 View Post
    Hi Eric,
    Try putting this line of code...
    ...immediately below this line:
    Make sure to save the workbook before you try the revised code and have no other workbook open should you have to close Excel if it gets in an endless loop again.
    Regards, Robert
    Thanks Robert that did the trick and we have finally beaten the endless loop or in this case endless label. Thank you so much
    Honestly this forum is brilliant because of the awesome people, cheers to you and TMS for all the messages, code and help. I have now revised the macro to include your previous Application.DisplayAlerts = False and then True before and after the inputbox. I have also made a distinction between a blank cell and cell with a value in the msgbox that pops up:

    Please Login or Register  to view this content.
    I'm still looking into the possibility of getting the err.number of the error message that pops up when the inputbox is left blank and the user press 'Ok' (assuming alerts aren't switched to False) so that I can test if the line like the below might work?
    Please Login or Register  to view this content.
    EricDonk
    Last edited by EricDonk; 07-12-2024 at 05:30 AM.

  17. #17
    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
    47,995

    Re: Looping an Excel Inputbox with Single Range

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon (Next to Add Reputation) located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  18. #18
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping an Excel Inputbox with Single Range

    Thanks Robert that did the trick and we have finally beaten the endless loop or in this case endless label. Thank you so much.
    You're welcome and a big thank you to TMS 👍 Thank you also for the rep 😎

    I'm still looking into the possibility of getting the err.number of the error message that pops up when the inputbox is left blank and the user press 'Ok' (assuming alerts aren't switched to False) so that I can test if the line like the below might work?
    No - as I said earlier leaving the input open is your only option as far as I can tell unless you use the custom form option from my post# 9.

    All the best,

    Robert

  19. #19
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by TMS View Post
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.
    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon (Next to Add Reputation) located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.
    Cheers for the info TMS, rep added.

    Quote Originally Posted by Trebor76 View Post
    You're welcome and a big thank you to TMS 👍 Thank you also for the rep 😎
    No - as I said earlier leaving the input open is your only option as far as I can tell unless you use the custom form option from my post# 9.
    All the best,Robert
    Thanks also to Robert for all your help.

    Regarding marking this thread as 'Solved', I'd still like to leave it open for another day or so just in case someone has a different approach - It also gives me time to look into custom forms.
    If there is no new messages before the end of the weekend I'll come back and mark it 'Solved' due to the partial workaround by Robert.

    To anyone else reading I'm still looking for a way to customise the error message when a user leaves the inputbox blank and presses 'Ok', theories I've had include setting a default value inside the inputbox or finding the err.number and using an if fuction but my VBA coding isn't the best so I'm struggling for a way to test them. Robert believe this request is impossible which it very well may be, but if you can help in anyway do let me know, else I will close this post on Sunday.

    Cheers, EricDonk

  20. #20
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,924

    Re: Looping an Excel Inputbox with Single Range

    I think Robert is right as the formula alert is generated by the inputbox and isn't trapped by the vba.

    Maybe you could do without the inputbox altogether?

    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by ByteMarks View Post
    I think Robert is right as the formula alert is generated by the inputbox and isn't trapped by the vba.
    Maybe you could do without the inputbox altogether?
    Thank you for you answer ByteMarks, I am slowly understanding the issue with the error message now and like your different approach. (But out of curiosity why won't a default value in the inputbox resolve this?)
    Your code needed a slight tweak for the blank ("") value to be recognised separately to the has.formula = false. And I couldn't work out why it need error handling, given the selection approach, but assume I'm missing something.

    However unfortunately I can't use this method because it's only part of the macro and when scaled up it will be used to compare 2 different cells, potentially across different sheets/ workbooks so selections aren't a practical solution.
    Thanks a lot for your input and different way of thinking though.

  22. #22
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,924

    Re: Looping an Excel Inputbox with Single Range

    But out of curiosity why won't a default value in the inputbox resolve this?
    It would resolve it to a degree, but in my (context-blind) view would be more confusing than the blank since it might imply to users that the correct range has already been selected, which I guess is unlikely.

    I couldn't work out why it need error handling
    Not strictly necessary as it works with the selection, which you noted, but I'd tried a few things before.
    It saves having lots of exits subs, or gotos since everything is directed to the errhandler.

  23. #23
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by ByteMarks View Post
    It would resolve it to a degree, but in my (context-blind) view would be more confusing than the blank since it might imply to users that the correct range has already been selected, which I guess is unlikely.
    I played around with a default and tend to agree it's just more confusing, I even had a default as the users selection but oddly that didn't feel great either. I hoped a space would work but as it's not a range it also just shows a default error. So I'll stick with Robert's workaround for now and if no one else replies before the weekend is out, then I'll come back to mark this as 'Solved'.

    Quote Originally Posted by ByteMarks View Post
    Not strictly necessary as it works with the selection, which you noted, but I'd tried a few things before.
    It saves having lots of exits subs, or gotos since everything is directed to the errhandler.
    That makes sense thanks for explaining ByteMarks, I have a long way to go before I ever understand VBA but we've all have to start somewhere right. Cheers for your help, rep added.

  24. #24
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Looping an Excel Inputbox with Single Range

    Give this RefEdit Option a go...Covers all scenarios I think...
    Custom design the form to your specs...

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Sintek; 07-12-2024 at 12:03 PM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  25. #25
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by Sintek View Post
    Give this RefEdit Option a go...Covers all scenarios I think...
    Custom design the form to your specs...
    Oh my goodness, this is genius, I don't even know what to say other than thank you so much Sintek.
    This RefEdit option is so far above my level of VBA that I have no idea what's going on but it's outrageously good and addresses what I originally asked for. I know Robert was hinting at this option as a way to go but thank you Sintek for doing it.

    I understandably have a lot of question but 3 main ones come to mind:
    Where in the spreadsheet is the code mentioned in your post I couldn't find it in any of the VBA modules? (Sorry if that's a simple question)
    Secondly some of the code is red when I looked through the modules, everything worked as it should but I wondered what VBA didn't like?
    And finally I'll change the code to not pop up the userform until the user clicks the button but likewise how do I stop it popping back up after a cell with a formula is selected? (And does it store the range so I can use it later in my code)

    Sorry if that's too much to answer because for now I'm so happy with your provided spreadsheet and code Sintek.
    Cheers for time and help, it's much appreciated. Rep added. (I'll also mark as Solved once I hear back from you)

  26. #26
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Looping an Excel Inputbox with Single Range

    Where in the spreadsheet is the code mentioned in your post
    Code is in the Userform...

    Secondly some of the code is red
    This is 64bit API declarations...still show as error in 64bit systems but does not matter...cannot be compiled...an does not cause an error...

    And finally I'll change the code to not pop up the userform until the user clicks the button but likewise how do I stop it popping back up after a cell with a formula is selected?
    Been resolved in new attached file...

    And does it store the range so I can use it later in my code
    If declared in same same procedure then yes...
    Just Declare a Variable...

    Please Login or Register  to view this content.
    Not sure what you need to do after once you've captured the users selection...
    Depending on this you can declare as Global, Private ,Public etc to be used with workbook...



    THANKS.gif
    Attached Files Attached Files
    Last edited by Sintek; 07-12-2024 at 03:50 PM.

  27. #27
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Looping an Excel Inputbox with Single Range

    I know Robert was hinting at this option as a way to go but thank you Sintek for doing it.
    I also provided a workbook with an userform that had a RefEdit control.

    Give this RefEdit Option a go...Covers all scenarios I think
    Agreed the RefEdit is the best option (refer my reply #9). I really like the icon you've assigned the macro to 👍

    Hi Eric,

    Not sure if this is an option but can you create a named range for the cell(s) you want to check and loop through that range checking if any has a formula so the user won't have to select anything?

    Robert
    Last edited by Trebor76; 07-12-2024 at 09:05 PM.

  28. #28
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Looping an Excel Inputbox with Single Range

    @ Robert...
    I also provided a workbook with an userform that had a RefEdit control.
    I only just realised after opening your file that all worked as expected...I assumed that as OP made no mention of this that it did not fulfill all requirements...Hence my updated version and remark...
    Give this RefEdit Option a go
    And yes...Great minds think alike...

  29. #29
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by Trebor76 View Post
    I also provided a workbook with an userform that had a RefEdit control.
    Agreed the RefEdit is the best option (refer my reply #9). I really like the icon you've assigned the macro to 
    Quote Originally Posted by Sintek View Post
    @ Robert...
    I only just realised after opening your file that all worked as expected...I assumed that as OP made no mention of this that it did not fulfill all requirements...Hence my updated version and remark...
    And yes...Great minds think alike...
    First of all I'd like to say sorry to Robert, I completely misunderstood your reply in post #9 and although I downloaded your sheet I thought it was an example scenario of how to use RefEdit rather than something you had made for me. I'm so silly because I read the code (in the module only - as I don't know how to read the code in the form) but never pressed 'play' as I didn't realise it was made for for exact scenario. I then closed it thinking that is something I'll look into if people can't help via the inputbox as I knew for now it was over my head. Apologies again, I feel so guilty and am sorry to Sintek for the extra work I created. RefEdit does the job perfectly and I have marked this as solved now thanks to the both of you.

    Quote Originally Posted by Trebor76 View Post
    Hi Eric,
    Not sure if this is an option but can you create a named range for the cell(s) you want to check and loop through that range checking if any has a formula so the user won't have to select anything? Robert
    Thank you for the suggestion, but in this case that's how the code started, it checked every cell in every open workbook and tested any formulas for their efficiency. The data it created was cumbersome and lengthy so I set about to compare just 2 at a time because the likelihood is there will only ever be 2 or 3 ways a formula will be written and I want to evaluate the best one. The code I asked for help with is just a way to select cells, cancel if needs be and produce a custom error message if someone didn't select anything - All of which RefEdit solves. Thank you.

    Quote Originally Posted by Sintek View Post
    Code is in the Userform...
    This is 64bit API declarations...still show as error in 64bit systems but does not matter...cannot be compiled...an does not cause an error...
    Been resolved in new attached file...
    If declared in same same procedure then yes...
    Just Declare a Variable...
    Not sure what you need to do after once you've captured the users selection...
    Depending on this you can declare as Global, Private ,Public etc to be used with workbook...
    Thank you for the reply Sintek, it was all very helpful. Cheers for explaining the 64bit API code and amending your file to not pop up the user form when opened and also end when a formula cell is selected, as Robert pointed out your button is very cool too.
    In terms of what I'll be doing with the cell selected (and also another selected cell) is comparing the efficiency of the 2 formulas so I can best decided which is less intensive in a large scale spreadsheet, I have the code for that, so knowing I can set RefEdit1 as the range is helpful thank you.

    To summarise, thank you to Robert and Sintek for resolving my original question by using RefEdit, I have marked this thread as solved. Also apologies to you both for the extra effort I caused by misunderstanding Roberts earlier post. I will have a google of how to edit the code with a user form and tinker with the code you both made because they produce the the same error regardless if a range of cells, a blank cell or a cell with a value is selected but I'll try and make a separate msgbox for each of those scenarios.

    Cheers for all the help, it was really kind of you and I'm extremely grateful and once more, I am truly sorry for the misunderstanding.
    EricDonk
    Last edited by EricDonk; 07-13-2024 at 10:15 AM.

  30. #30
    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
    47,995

    Re: Looping an Excel Inputbox with Single Range

    as I don't know how to read the code in the form
    Right click on the form and choose View Code.

  31. #31
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59
    Quote Originally Posted by TMS View Post
    Right click on the form and choose View Code
    I can’t believe it’s as easy as that, but if you don’t know, you don’t know. Thank you TMS.

  32. #32
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Looping an Excel Inputbox with Single Range

    I will have a google of how to edit the code with a user form and tinker with the code you both made because they produce the the same error regardless if a range of cells, a blank cell or a cell with a value is selected but I'll try and make a separate msgbox for each of those scenarios
    Don't understand...Thought all was perfect...What are you referring to...

  33. #33
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59
    Quote Originally Posted by Sintek View Post
    Don't understand...Thought all was perfect...What are you referring to...
    Everything from my original request has been resolved which is why I said it’s solved. But during the conversation of this thread, TMS undated my code to notify the user with a different message box if multiple cells were selected instead of one. It read something like “Multiple cells selected. Please select just one cell” which isn’t something I thought of, but I liked it. So I further updated my code to have a different message boxes depending on if; 1 empty cell is selected, 1 cell with a value is selected or if multiple cells are selected. (And continue with my code if 1 cell with a formula is selected.

    All I was saying is that I’ll update your code (or Roberts) within the user form to also have the different messages boxes for the different scenarios. (Blank cell, cell with value, multiple cells).

  34. #34
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Looping an Excel Inputbox with Single Range

    Post 24 has that option...
    Just change the message...
    Please Login or Register  to view this content.

  35. #35
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by Sintek View Post
    Post 24 has that option...
    Just change the message...
    Thanks for getting back to me, that part of your code isn't working and up until yesterday I didn't even know how to view forms code hence I didn't realise you had tried to incorporate it.

    Now that I know how to edit the code in a user form, I'll be trying to fix your code, along with adding the additional criteria to distinguish the difference between selecting a cell with nothing in and a cell with just a value.

    Cheers for you help though and wanting to help me further.

  36. #36
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Looping an Excel Inputbox with Single Range

    It is all about the positioning of the check criteria...Edited Message to show selection options...
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Sintek; 07-15-2024 at 01:46 AM.

  37. #37
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Thank you so much Sintek, now that I have a bit more understanding of the code within user forms, I have also tweaked the code you provide to add a further message for if the cell selected is completely empty or contains a value.

    Please Login or Register  to view this content.
    Next I'm going to be working on a way to capture the full cell address (Workbook, sheet name and cell ref) of the user selected cell into my workbook so that I can run a further macro using that value as the 'selected cell'. but that's a topic (and thread) for another day.
    Cheers for everything you have provided. I am very impressed with how it all works so thank you once more because it's very much appreciated.

  38. #38
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Looping an Excel Inputbox with Single Range

    capture the full cell address (Workbook, sheet name and cell ref) of the user selected cell
    Not sure what you wanna do with it but here you go...
    Please Login or Register  to view this content.

  39. #39
    Registered User
    Join Date
    11-14-2022
    Location
    England
    MS-Off Ver
    MS365 (PC) Version 2308
    Posts
    59

    Re: Looping an Excel Inputbox with Single Range

    Quote Originally Posted by Sintek View Post
    Not sure what you wanna do with it but here you go...
    Wow, I can only hope to be as good at VBA as you one day, that's fantastic, thank you so much! I spent about 3 hours trying to get it working and you came along and did it in minutes, very well done.

    In answer to your question, I plan to turn the user form response into a full cell address that is pasted into my workbook. (In my case to Q4) That way I can re-run the body of my code without asking for the user to select the same cell again and again. The code you provided will be invaluable because to call the full address I'll use something like:
    Please Login or Register  to view this content.
    So thank you for you time and skills.
    Last edited by EricDonk; 07-15-2024 at 06:25 PM.

  40. #40
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Looping an Excel Inputbox with Single Range

    Glad to have contributed...Happy Coding....

+ 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. Replies: 4
    Last Post: 05-07-2020, 12:17 AM
  2. VBA inputbox error - looping problem
    By dmihaljevic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-05-2014, 09:07 AM
  3. Inputbox to retrieve Excel range from Powerpoint VBA
    By submariner18 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-26-2013, 01:50 AM
  4. [SOLVED] InputBox option and its features(looping,row switching,data collecting)
    By 4evra in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-29-2013, 02:05 PM
  5. [SOLVED] Looping sub not detecting user cancel from inputbox
    By jshaw82 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-07-2013, 06:14 PM
  6. [SOLVED] Trouble Looping Through an Excel Range using a 2D 'For Loop' with VBA
    By Sarvazad in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-14-2013, 04:33 PM
  7. looping through a range of cells in excel
    By rbhandair in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-23-2007, 09:19 AM

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