Hide/show scrollbar with combobox

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
239
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I'm using the following code to hide/show charts and scrollbars.

Code:
Private Sub cmbDepartment_Change()Dim SelectedChart As String
Dim SelectedScrollbar As String
Dim blnVisible As Boolean



Me.cmbFilter.ListFillRange = Me.cmbDepartment
Range("B16") = cmbDepartment.ListIndex + 1


SelectedChart = "Chart" & Range("B16")
SelectedScrollbar = "Scrollbar" & Range("B16")  


    ActiveSheet.ChartObjects("Chart1").Visible = blnVisible
    ActiveSheet.Shapes("Scrollbar1").Visible = blnVisible
    ActiveSheet.ChartObjects("Chart2").Visible = blnVisible
    ActiveSheet.Shapes("Scrollbar2").Visible = blnVisible
    ActiveSheet.ChartObjects("Chart3").Visible = blnVisible
    ActiveSheet.Shapes("Scrollbar3").Visible = blnVisible
    ActiveSheet.ChartObjects("Chart4").Visible = blnVisible
    ActiveSheet.Shapes("Scrollbar4").Visible = blnVisible
    


    If Not blnVisible Then ActiveSheet.ChartObjects(SelectedChart).Visible = True
    If Not blnVisible Then ActiveSheet.Shapes(SelectedScrollbar).Visible = True
    
End Sub

Is there a way to reduce to code in size? Especially this part:

Code:
    ActiveSheet.ChartObjects("Chart1").Visible = blnVisible    
    ActiveSheet.Shapes("Scrollbar1").Visible = blnVisible
    ActiveSheet.ChartObjects("Chart2").Visible = blnVisible
    ActiveSheet.Shapes("Scrollbar2").Visible = blnVisible
    ActiveSheet.ChartObjects("Chart3").Visible = blnVisible
    ActiveSheet.Shapes("Scrollbar3").Visible = blnVisible
    ActiveSheet.ChartObjects("Chart4").Visible = blnVisible
    ActiveSheet.Shapes("Scrollbar4").Visible = blnVisible

You can imagine if I have 50 charts the length of the code will be very big.
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
You could try something like this.
Code:
For I = 1 To 50
    ActiveSheet.ChartObjects("Chart" & I).Visible = blnVisible
    ActiveSheet.Shapes("Scrollbar" & I).Visible = blnVisible
Next I
 
Upvote 0
It returns the error:

The item with the specified name wasn't found.
 
Upvote 0
I've assumed you have 50 charts named 'Chart1', 'Chart2' etc. and similarly 50 scrollbars named 'Scrollbar1', 'Scrollbar2' etc.

If that's not the case then a different approach will be required.
 
Upvote 0
Well, not yet :)

It was an example.

Currently i have 3 charts and 3 scrollbars but the sheet is in development.
This amount can grow to for example 50 charts and 50 scrollbars.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top