Excel object chart visible and invisible through VBA - excel

I want hide the chart on the some specific condition like if corresponding cell is blank the chart should be invisible. but once that cell value is not blank then again chart should be visible.
I have the below program, but it doesnt work for me. please help.
Sub chart_visibility()
ActiveWorkbook.Sheets("RP0004").Activate
If Range("H32").Value = "" Then
ActiveSheet.Charts("Chart 5").Visible = False
Else
ActiveSheet.Charts("Chart 5").Visible = True
End If
End Sub

E.g:
Sub chart_visibility()
With ActiveWorkbook.Sheets("RP0004")
.ChartObjects("Chart 5").Visible = (Len(.Range("H32").Value)>0)
End With
End Sub
You want the ChartObjects collection and not the Charts collection

Related

Loop through one or multiple selected Chart Objects

I want to write a code that works whether if I select one or multiple charts in Excel.
Actually I use this code, but it fail when only one chart is seleted :
Dim obj As Object
'Check if any charts have been selected
If Not ActiveChart Is Nothing Then
MsgBox ("Select multiple charts")
Else
For Each obj In Selection
'If more than one chart selected
If TypeName(obj) = "ChartObject" Then
Call AnotherMacro(obj.Chart)
End If
Next
End If
I'm looking for a solution working into a unique Sub.
Thanks for your help
Please, try the next way:
Sub iterateSelectedCharts()
Dim obj As Object
If TypeName(Selection) = "ChartArea" Then 'for the case of only one selected chart:
'Call AnotherMacro(ActiveChart)
ElseIf TypeName(Selection) = "DrawingObjects" Then
For Each obj In Selection
If TypeName(obj) = "ChartObject" Then
'Call AnotherMacro(obj.Chart)
End If
Next obj
Else
MsgBox ("Please, select charts") 'for the case when selection contains
'other type of object(s): cells, rectangles, circles etc.
End If
End Sub

Excel VBA - Convert existing image in cell to comment picture

I'm trying to use VBA in Excel to convert a bunch of pictures in a column (one per cell) to a pop up comment image instead so that the sheet is more easily readable.
I can find the image I need by iterating through the shapes, and I can set this as an object; but I can't seem to use that onject to populate the comment field. It seems to be looking for a true file path instead.
I don't particularly want to have to save each image and then reload it, seems kind of pointless.
For Each Pic In ActiveSheet.Shapes
If Pic.TopLeftCell.Address = ActiveCell.Address Then
If Pic.Type = msoPicture Then
Pic.Select
Application.ActiveCell.AddComment.Shape.Fill.UserPicture **(ActiveSheet.Shapes(Pic.name))** 'if I use a path here its okay
'SelectPictureAtActiveCell = name
Exit For
End If
End If
Next
any thoughts?
CJ
I think you want to show one image if you select a specific cell then
See
Making shapes invisible/visible in excel through VBA
with
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Macro1
End Sub
You can make images hide and show using
ActiveSheet.Shapes("ImageName").Visible = False or True
for example when you click on cell A1 first image is hidden else all images are visible
Sub Macro1()
Dim shp As Shape
If ActiveCell.Address = "$A$1" Then
For Each shp In ActiveSheet.Shapes
ActiveSheet.Shapes(1).Visible = False
' or you can use image name as
'ActiveSheet.Shapes("ImageName").Visible = False
'shp.Visible = False
Next
Else
For Each shp In ActiveSheet.Shapes
shp.Visible = True
Next
End If
End Sub

Excel images to appear based on cell value

I have 2 sheets on excel, one called Raw and one called Graphs. What I want to do is have some cells in Raw and it they =TRUE then I want a shape to appear on the Graphs page.
I am pretty new to VBA so i havent tried much :(
Private Sub Worksheet_Calculate()
With Worksheets("Graph")
If Me.Range("FK45").Value = True Then
.Shapes("Test1").Visible = True
Exit Sub
ElseIf Me.Range("FK45").Value = False Then
.Shapes("Test1").Visible = False
Exit Sub
End If
End With
End Sub
I can get this to work so if FK45 is TRUE the image shows but if FK45 is FALSE it doesn't, But what I want to be able to do is add more to this e.g.
Private Sub Worksheet_Calculate()
With Worksheets("Graph")
If Me.Range("FK45").Value = True Then
.Shapes("Test1").Visible = True
Exit Sub
ElseIf Me.Range("FK45").Value = False Then
.Shapes("Test1").Visible = False
Exit Sub
End If
End With
With Worksheets("Graph")
If Me.Range("FK46").Value = True Then
.Shapes("Test2").Visible = True
Exit Sub
ElseIf Me.Range("FK46").Value = False Then
.Shapes("Test2").Visible = False
Exit Sub
End If
End With
End Sub
I want them all to be independent from each other and be able to add more if necessary
If FK45 is TRUE Image1 shows
If FK45 is FALSE Image1 doesn't show
and/or
If FK46 is TRUE Image2 shows
If FK46 is FALSE Image2 doesn't show
and/or
If FK47 is TRUE Image3 shows
If FK47 is FALSE Image3 doesn't show
and so on...
This is how I would do it
In VB Editor find your Worksheet and Shape objects and rename their system name to something intutive in their respective property windows.
This way you can get rid of With Worksheets("Graph") construction, instead you can call them by their system name like With Graph. This will also come in handy if you wish to rename your worksheets or shapes.
Note that you Exit Sub after each cell check, your procedure stops after first cell and doesn't proceed any further.
Instead of Worksheet.Calculate event I advise using Worksheet.Change. This way you can iterate through your cells one by one.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngCell As Range
Dim shpImage As Shape
For Each rngCell In Target.Cells
' check if change was made in "FK"
If Not Intersect(rngCell, Me.Columns("FK")) Is Nothing Then
Select Case rngCell.Row
Case 45: Set shpImage = Graph.MyImage_1
Case 46: Set shpImage = Graph.MyImage_2
End Select
' if only boolean values present, no need for IF construction
shpImage.Visible = rngCell.Value : Set shpImage = Nothing
End If
Next
End Sub
If you had separate column for image name it would be much easier, you could check like this (for example, shape names are located in "FL" column)
Graph.Shapes(rngCell.Offset(0, 1).Value).Visible = rngCell.Value

Making shapes invisible/visible in excel through VBA

I'm having this problem for the last few hours and I would really appreciate some help with it.
Basically, I want to be able to hide/unhide shapes depending on selections a user makes on a userform. I've broken the problem down into a very simple example. If I insert a shape called "oval 1" in a sheet and run the code:
Sub hideshape()
With ActiveSheet
.Shapes("Oval 1").Select
With Selection
.Visible = False
End With
End With
End Sub
the shape disappears but when I run this code
Sub unhideshape()
With ActiveSheet
.Shapes("Oval 1").Select
With Selection
.Visible = True
End With
End With
End Sub
I get an error "Requested Shapes are locked for Selection"
The workbook is not protected and I have tried un-ticking locked and locked text on the shape properties.
Any ideas what's causing this.
You cannot Select a hidden object. However, you dont need to use Select at all, and it is usually not recommended. Try simply:
Sub HideShape()
ActiveSheet.Shapes("Oval 1").Visible = False
End Sub
Sub UnhideShape()
ActiveSheet.Shapes("Oval 1").Visible = True
End Sub
I hide shapes based on their name since some shapes I don't want to hide. I use this format:
Sheet1.Shapes.Range(Array("COtxtBox1")).Visible = msoTrue
name of your shape or shapes goes into the array
if it only 1 shape you could just use:
Sheet1.Shapes.range("COtxtBox1").Visible = True
I found that the "mso" part is not necessary for the True or False statement
Sub HideEachShape()
Dim sObject As Shape
For Each sObject In ActiveSheet.Shapes
sObject.Visible = False
Next
End Sub
from: extendoffice.com
I solved problem with this code(Oval = Type 9, from MsoAutoShapeType Enumeration (Office)):
Sub hide()
s = ActiveSheet.Shapes.Count
For i = 1 To s
If ActiveSheet.Shapes(i).Type = 9 Then ActiveSheet.Shapes(i).Visible = False
Next i
End Sub
Sub unhide()
s = ActiveSheet.Shapes.Count
For i = 1 To s
If ActiveSheet.Shapes(i).Type = 9 Then ActiveSheet.Shapes(i).Visible = True
Next i
End Sub
If "Type = 9" is wrong, you can find out type of your shape with code in Immediate window (ctrl+G in VBA):
?ActiveSheet.Shapes("Oval 1").Type
Public HIDE As Boolean
Sub T_BUTTON ()
ActiveSheet.Shapes("T 1").Visible = HIDE
If ActiveSheet.Shapes("T 1").Visible = False Then
HIDE = True
Else
HIDE = False
End If
END SUB

refresh cell values before conditional formatting

I have a sheet that displays data in shapes to look nice. These shapes simply reference a cell in a data staging sheet. The data in the staging area is using a getpivotdata formula to lookup data.
On my display sheet with the shapes that display values to the user, there is also a combo box that changes the pivot table. When the pivot table is changed it updates the data in the staging area which in turn changes the data in the shapes on the display sheet. I then call a macro that sets the shapes to green if >0 or red if <0.
The problem is that while the pivot data is changing and being recalculated my shapes keep having their colours changed in accordance to the old values as the new fields haven't been calculated yet.
I've tried DoEvents but have little experience as to what it does and had no luck. I don't want to delay by a fixed time because that's just horrible. I've also tried calling StagingSheet.calculate before calling my updateColours macro without success.
Any ideas?
You could try Application.ScreenUpdating = false. E.g.:
Sub AllWorkbookPivots()
Dim pt As PivotTable
Dim ws As Worksheet
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
Next ws
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
That might work.
So to make sure the values are calculated before the colour change is called I used the following macro to make the application wait until the calculations were done.
Sub WaitForCalculate()
Application.Cursor = xlWait
Application.EnableEvents = False
Do Until Application.CalculationState = xlDone
DoEvents
Loop
Application.EnableEvents = True
Application.Cursor = xlDefault
End Sub
Not very pretty but gets the job done.

Resources