Script works in forms element but doesn't in normal option button

C

CalamityJane

Hello,

I'm programming a Macro to hide/unhide some entire rows and colums, but the
following proble occurs:

When I add the following script to the option button from controls, it
displays an error in line 3 (in the example marked with *):

Private Sub OptionButton1_Change()
If OptionButton1.Value = True Then
Sheets("Voorbeeldportefeuille").Select
*Columns("H:K").Select*
Selection.EntireColumn.Hidden = False
Else
Sheets("Invoer").Select
End If
End Sub

When I use the option button from the Forms toolbar, I get the following
script after recording the macro, and it works perfectly:

Sub Keuzerondje25_BijKlikken() (-> = Sub
Optionbutton25_Click())
Sheets("Voorbeeldportefeuille").Select
Columns("H:K").Select
Selection.EntireColumn.Hidden = False
End Sub

Great that it works, but I want to use the option button from the controls
toolbar, as I need to use the change function, in stead of the click
function. Can anyone tell me what I'm doing wrong?

Thanks.
 
P

Peter T

Sheets("Voorbeeldportefeuille").Select
*Columns("H:K").Select*

You need to qualify Columns("H:K") with the sheet reference. Normally
without the reference the activesheet is implied, but as your code is in a
worksheet module the code tries to select those columns in its own sheet.
But it can't because it's no longer active. Instead try

Sheets("Voorbeeldportefeuille").Columns("H:K").Select

Actually you really don't need to select, simply

Sheets("Voorbeeldportefeuille").Columns("H:K").EntireColumn.Hidden = False

(you don't even need to activate the sheet to do the above)

Regards,
Peter T


If the worksheet module with your code does not "Voorbeeldportefeuille" you
will need t
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top