VB debug help!

Thoron6

New Member
Joined
Oct 27, 2015
Messages
27
Hi all,

I've got the below code running in my dashboard tab:
Code:
Sub chart_visibility()
     ActiveWorkbook.Sheets("Dashboard").Activate
       If Range("A1").Value = "1" Then
        ActiveSheet.ChartObjects("Chart 3").Visible = False
        ActiveSheet.ChartObjects("Chart 1").Visible = False
        ActiveSheet.Shapes("TextBox 12").Visible = False
        ActiveSheet.Shapes("TextBox 13").Visible = False
        ActiveSheet.Shapes("TextBox 14").Visible = False
        ActiveSheet.Shapes("TextBox 15").Visible = False
        ActiveSheet.Shapes("TextBox 16").Visible = False
        ActiveSheet.Shapes("TextBox 17").Visible = False
        ActiveSheet.Shapes("TextBox 8").Visible = False
        ActiveSheet.Shapes("TextBox 5").Visible = True
        Rows("18:31").EntireRow.Hidden = True
        
        
       Else
        ActiveSheet.ChartObjects("Chart 3").Visible = True
        ActiveSheet.ChartObjects("Chart 1").Visible = True
        ActiveSheet.Shapes("TextBox 12").Visible = True
        ActiveSheet.Shapes("TextBox 13").Visible = True
        ActiveSheet.Shapes("TextBox 14").Visible = True
        ActiveSheet.Shapes("TextBox 15").Visible = True
        ActiveSheet.Shapes("TextBox 16").Visible = True
        ActiveSheet.Shapes("TextBox 17").Visible = True
        ActiveSheet.Shapes("TextBox 8").Visible = True
        ActiveSheet.Shapes("TextBox 5").Visible = False
        Rows("18:31").EntireRow.Hidden = False
      End If
    
End Sub


Private Sub Worksheet_Calculate()
Static OldVal As Variant
If Range("A1").Value <> OldVal Then
    OldVal = Range("A1").Value
    Call Sheet4.chart_visibility                <------ This row is highlighted on debug.             
End If
End Sub

The only other bit of VB is in the worksheet to hide the ribbon:
Code:
Private Sub Workbook_Open()
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",true)"
End Sub

Any help would be greatly appreciated....
 
Last edited by a moderator:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Sorry, so I have all of the above code in the sheet section and it all references a formula cell (A1). Once changes occur on that sheet and the formula changes, I need the code above to kick in. I have no code written in modules (Which I think the issue may be) but when I copy both the codes above into modules, nothing works. I'm just trying to get the above code to work and think it's probably pretty close and needs a little tweak? Thank you
 
Upvote 0
In which sheet is A1 in this line? is it in Sheet4? or Dashboard? (I think it is in Dashboard)
Code:
If Range("A1").Value = "1" Then vis = False Else vis = True

In which sheet are the Rows that you want hidden\not hidden? (I think they are in Dashboard)
Code:
Rows("18:31").EntireRow.Hidden = True
 
Last edited:
Upvote 0
They are all in sheet 4 which I have renamed "Dashboard" ... Also, a new update. All of the above works fine until I protect the sheet.
 
Upvote 0
So if the sheet changes you need the code to run?


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    chart_visibility
End Sub
 
Upvote 0
Thank you, I figured it out. When I was protecting the sheet it was stopping the macro from running some of the actions. Thanks for the replies. This can be deleted now. cheers
 
Upvote 0
1. Instead of repeating ActiveSheet on every line use With ActiveSheet construction like this
Code:
With [COLOR=#ff0000]ActiveSheet[/COLOR]
        .ChartObjects("Chart 3").Visible = True
        .ChartObjects("Chart 1").Visible = True
    etc
End With
2. Me can be used instead of ActiveSheet because chart_visibility is in the sheet module of the sheet that the code applies to

3. Instead of repeating Visible = True (or False) and listing all the objects listed twice, consider using a Boolean variable like this ...
Code:
Sub chart_visibility()
    Dim[COLOR=#008080] vis As Boolean[/COLOR]
    Me.Activate  
    If Range("A1").Value = "1" Then [COLOR=#008080]vis = False[/COLOR] Else [COLOR=#008080]vis = True[/COLOR]
    With [COLOR=#ff0000]Me[/COLOR]
        .ChartObjects("Chart 3").Visible = [COLOR=#008080]vis[/COLOR]
        .ChartObjects("Chart 1").Visible = [COLOR=#008080]vis[/COLOR]
        .Shapes("TextBox 12").Visible = [COLOR=#008080]vis[/COLOR]
        .Shapes("TextBox 13").Visible =[COLOR=#008080] vis[/COLOR]
        .Shapes("TextBox 14").Visible = [COLOR=#008080]vis[/COLOR]
        .Shapes("TextBox 15").Visible = [COLOR=#008080]vis[/COLOR]
        .Shapes("TextBox 16").Visible = [COLOR=#008080]vis[/COLOR]
        .Shapes("TextBox 17").Visible = [COLOR=#008080]vis[/COLOR]
        .Shapes("TextBox 8").Visible = [COLOR=#008080]vis[/COLOR]
        
        .Shapes("TextBox 5").Visible = [COLOR=#008080]Not vis[/COLOR]
        Rows("18:31").EntireRow.Hidden = [COLOR=#008080]Not vis[/COLOR]
    End With
End Sub

And for info only..
Both pairs of lines are equally valid and mean the same thing inside a sheet module..
Code:
If Range("A1").Value = "1" Then vis = False Else vis = True
If Me.Range("A1").Value = "1" Then vis = False Else vis = True

Rows("18:31").EntireRow.Hidden = Not vis
Me.Rows("18:31").EntireRow.Hidden = Not vis
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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