r/vba•
NSFWAvoid

In excel, is there a way to have a radio button change a displayed PDF?

Solved

Like if this radio (option) button is selected then change the displayed PDF to another PDF.

Seems like a possible thing so asking here :D

Edit 1: a better example

Like in Excel you can insert a PDF, yeah. I want to change the PDF based off what radio button that is selected.

So let's say I want radio button 2's PDF but it's currently on RB1. So I'd select RB2 and it will change the current PDF, RB1's, to RB2's PDF.

Posted Nov 4, 2019, 3:16 AM UTC · edited

526
allmappedout3•

If the pdf object you've inserted has the visibility property then you could have a Boolean switch on click on the radio button on click to change the visibility of both files

eg:

If radiobox then

Pdf1.visible = xlhidden

Pdf2.visible = xlvisible

Else

Pdf1.visible = xlvisible

Pdf2.visible = xlhidden

End if

Not 100% sure of the syntax as I'm typing this on my phone not a computer (it might be that you set visible to true/false).

You would

2
NSFWAvoidOP•

Okay, I'll try that. Thank you

1
NSFWAvoidOP•

So I thought this would be easy to understand where to put this code. Thought it was just in the developer tab and click on Visual Basic. I typed up this code with the names changed and its making a module, is this right? I havent messed around with Excel so I dont know.

Only reason I thought I could do this was because I took a class on Visual Basic and I understood that, no problem. Excel seems like a different breed of VBA lol

1
allmappedout3•

Hey so I finally got to a computer and my code was waaaay off.

As the poster below me noted, you have to create a radio button using the activeX controls, and assign a variable to its state.

In this case, my radio button is called "OptionButton1". You can find out what it is by right clicking on it and seeing what it's named in the top right corner.

Then, with your two PDF files inserted as objects, you can see what they're named (in this example, Object1 and 2 respectively).

All of this is taking place on the sheet called 'Test'.

I've confirmed that this example works based on what you described and a test (although I might suggest you use a tick box instead of a radio button since you may want it to be reversible, but that's your design choice and the same principles apply).

Hopefully the below makes sense!

Sub test()

Dim radiobox As Boolean

radiobox = Sheets("Test").OptionButton1.Value

If radiobox Then
Sheets("Test").Shapes("Object1").Visible = xlHidden
Sheets("Test").Shapes("Object2").Visible = xlVisible
Else
Sheets("Test").Shapes("Object1").Visible = xlVisible
Sheets("Test").Shapes("Object2").Visible = xlHidden
End If
End Sub
2
NSFWAvoidOP•

Solution Verified

2
Clippy_Office_AsstMOD•

You have awarded 1 point to allmappedout

^I ^am ^a ^bot, ^please ^contact ^the ^mods ^for ^any ^questions.

2
NSFWAvoidOP•

Hmm I just tried inputing it into my sheet and I cant figure out how to click on the button haha Like I can move it, select it but I cant actually activate it. Gosh, Excel is hard xD Never worked with excel before so, learning experience, I guess.

1
NSFWAvoidOP•

Haha just tried again with a few modifications and it worked. Thanks for the help :D

1
allmappedout3•

No problems!

If you could mark it at solved that'd be awesome!

1
HFTBProgrammer66•

You have to create the radio button object first. Do that via the Developer ribbon, Insert Control icon, then select the control(s) you want to plop onto your sheet.

1
Siggi_pop•

What does: displayed PDF to this PDF, mean?

1
NSFWAvoidOP•

Like in Excel you can insert a PDF, yeah. I want to change the PDF based off what radio button that is selected.

So let's say I want radio button 2's PDF but it's currently on RB1. So I'd select RB2 and it will change the current PDF, RB1's, to RB2's PDF.

1
Siggi_pop•

Yes there is a way. By hiding and unhiding PDF1 or PDF2 depending on which radio is selected.

1
NSFWAvoidOP•

Code for that? Tried to search before.

1
Siggi_pop•

Yeah i have code for that, but i have no idea if it would in work your case, because you have not clarified how you "inserted" PDF to a sheet, so lets start there.

1
[deleted]•

[deleted]

1
NSFWAvoidOP•

Yeah I thought about that I just don't know how to say show PDF 1. Nor do I know where to put it, thought it would be more simple then what Ive tried

1