last day (14 days later) » 

12:27 PM
-1
A: Option Button - Check if exists on sheet

RalphI'd suggest this approach. Hope it helps: Public Sub AllShapesOnAllSheets() For Each ws In ActiveWorkbook.Worksheets For Each btn In Worksheets(ws.Name).OptionButtons Select Case btn.Name Case "OptionButton1" If btn.Value = xlOn Then opt = "ob1" Case "OptionButton...

 
Aiken: The Buttons-Class can be found within the VBA Object Explorer: just go to the object explorer (pressing F2) in VBA, right-click into the window and select "Show Hidden Members". There you will find the Buttons- and the Button-Class.
 
so can I start inserting this into my script, or I have no chance that this will work?
 
Well, obviously you still have to check for the .Value and you'll have to complete the code where I took the short-cut with [...]. But other than that I am pretty confident it will work.
 
I have updated the script
 
My bad. There was a typo in my solution. Of course, it should be coherent throughout the script. Either use shp everywhere or btn. Also, my original script used the Buttons-Class when you needed the OptionButtons-Class. I just updated the solution. It works in my Excel.
 
12:27 PM
The script does not work. "The item with the specified name wasn't found" error on line Set shp3 = ActiveSheet.Shapes("OptionButton3") when the button is missing, also when I added the On Error Resume Next, and checked what is going on with msgbox, when "OptionButton1" was selected, it has set the value of opt to "ob3" 2 times. I think because of there are 2 buttons on that sheet. 1 and 2
 
In fact you may remove all these set shpx... they are redundant and no longer needed. Yet, it is strange that (apparently) your code found an OptionButton with the name "OptionButton1" (must be due to the select case...statement) but when your are trying to do something with it, VBE says it cannot be found. Seems almost impossible!
 
ok, the actual question was if we can avoid this somehow, or is it impossible? This way we just made stuff more complicated than the original solution.
 
More complicated? Not sure what you mean by that. It seems to me (personally) that my proposition is much shorter: one case statement eliminated, all dim statements gone. Looking back at the original question you wanted to eliminate the "on error resume next" which I propose to eliminate by cycling through all existing (!) OptionButtons within your Excel file. So, is the code working or do you experience any problems?
 
As I mentioned before, the code is not working. Could not eliminate the "on error resume next" and even by using it, the opt string gets different value than it should.
 
I tried the code "as is" on three different computers with Excel 2010 and Excel 2003 / XP and it worked. Apart from that I cannot see any logical errors.
Could it be that you forgot to mention something? Is there more code to it? If that's an option, please feel free to email me the file to have a look at.
To check the incorrect value you can temporarily add
debug.print "Option " & opt & " on sheet " & ws.Name
after the line "end select"
 
12:37 PM
sorry, I have 10 more minutes to discuss this for today as my shift is over, and I only have this script at my workplace where I work on it on my free time. So I have many sheets where I have different buttons with the same name. ex. button a, b, c, d, e.
Lets say on sheet 1 I use button a and b; c is only used on sheet 5. Another script calls this one to decide what to do after the selected option. When I inserted your script, while button a was selected, your script told the main script that button c is.
we will try to debug tomorrow if it's ok for you
for today I can try to explain as many details as you need.
 
Sure. If that's an option, please send me the file.
 
so ask me if you have questions
it is quite complex
 
Don't mind.
Maybe button c on a different sheet is also selected. You can catch that with the above proposition do append the debug.print.
There you'll see on which sheet the option was found.
 
yes, you are right, button c is selected on another sheet
we need to check it only on the actual sheet we are working on
 
Ok, I updated that part.
So, now there is still the "on error resume next" problem.
Can you tell me on which line this error occurs?
 
12:50 PM
now it is working even without it
I still need to test this for some bugs
 
Glad it finally works.
 
tomorrow I will accept your solution if everything is ok
 
Let me know if you run into any additional problems.
 
for now, thank you for your effort!
have a nice day :)
 
You too.
 

last day (14 days later) »