Setting Title of Chart automatically - vba

Is there anyway to have the title of a chart be equal to string variable. I recorded me manually changing the title and it came up as first activating it, then showing my text edit, but how do I do this if I don't want to activate it. I know the name of the chart is chart 1, so was trying to find a way to have the title of chart 1 be set to my string variable (which I generate from another loop elsewhere). Thx
activesheet.chartobjects("chart 1").activate
activesheet.charttitle.text="my text edit"

ActiveSheet.ChartObjects("Chart 1").Chart.ChartTitle.Text = "my text edit"
You can read more about chartObjects here

I prefer this solution:
Dim var As String
Set objChrt1 = Sheets("sheet1").ChartObjects(1)
Set myChart1 = objChrt1.chart
myChart1.ChartTitle.Text = "my text edit"
var = myChart1.ChartTitle.Text
MsgBox var

Related

Edit Form Control Label Caption on VBA

Trying to edit the text of a form control label named lblsearchreminder and make sure that the font is Arial and size 20. I am pulling the edited text from an ActiveX textbox1 and trying to make that the caption of the label. If anyone has any insight I would greatly appreciate it.
Sub btnAltCustomSearch_Click()
Dim strTextBox As String
If Worksheets("User Interface").OLEObjects("TextBox1").Object.Value = "" Then
ErrorX.Show
Else
strTextBox = Worksheets("User Interface").OLEObjects("TextBox1").Object.Value
Worksheets("Muscle Wasting Database").Shapes("lblsearchreminder") = vbCrLf & "Disease: All" & vbCrLf & vbCrLf & "Keyword: " & strTextBox
lblsearchreminder.Object.Characters.Text = "Arial"
lblsearchreminder.Object.Font.Size = 20
End If
End Sub
Yes the textbox1 is an ActiveX textbox control, but I will ideally use a Form Control label. So I am trying to pull the typing from the ActiveX textbox and use it in the Form Control label – Thor Nagel 3 hours ago
Unfortunately you can't manipulate the font-size/name, color or style of a Form Control Label. If you notice the formatting items have been "grayed out" in the Font group on the Excel Ribbon.
To set a Text is easy
Dim lblsearchreminder As Shape
Set lblsearchreminder = Sheet1.Shapes("Label 1")
lblsearchreminder.TextFrame.Characters.Text = "Hello"
But you cannot do (Even though Intellisense allows it)
lblsearchreminder.TextFrame.Characters.Font.Name = "Arial"
or
lblsearchreminder.TextFrame2.TextRange.Characters.Font.Name = "Arial"
I would recommend using a TextBox shape or an ActiveX Label instead.
Similarly you cannot change the font using
lblsearchreminder.TextFrame.Characters.Font.Size= 20
I don't think you're using a Form Control. TextBox1 would be the default name for an ActiveX control, which would be coherent with using .OLEObject.Object to retrieve it.
Declare a MSForms.TextBox variable for it.
Dim box As MSForms.TextBox
Now assign it to the .OLEObject.Object:
Set box = Worksheets("User Interface").OLEObjects("TextBox1").Object
If the sheet "User Interface" exists in ThisWorkbook at compile-time, give it a code name (F4; set the (Name) property to e.g. UserInterfaceSheet) - then you can use that identifier directly, without needing to pull the worksheet from the Worksheets collection:
Set box = UserInterfaceSheet.OLEObjects("TextBox1").Object
Now you have an early-bound object reference to play with, you'll have IntelliSense to guide you. MSForms.TextBox does not have a .Characters property. It does have a .Font property though, so you can start exploring that:
So the Font property is an object of type NewFont; using the Object Browser (F2) you can browse its members:
Thus:
box.Font.Name = "Arial"
box.Font.Size = 20
Should do it.
Watch out for misleading names and prefixes: lblsearchreminder reads like you're looking at a MSForms.Label control, not a TextBox. txtSearchReminder would be more appropriate, or if you prefer control-agnostic names, SearchReminderBox works as well.
Also Error is a function from the VBA.Conversion module, that you are shadowing here.

Turning the visibility of chart series on/off using excel Macros/vba

I am making a line graph (chart) in Excel with several data series being plotted onto the same chart.
I need to create a macro/VBA solution that can turn the visibilty of these series on/off via the pressing of a button (or tick box etc)
Similar to this picture (manually done through the excel menu system)
I have tried to look through all the member vars/methods on
https://msdn.microsoft.com/EN-US/library/office/ff837379.aspx
but haven't had much luck.
I have tried playing around with bits like
Charts("Chart1").SeriesCollection(1)
and
Worksheets("Graphical Data").ChartObjects(1)
but I can neither get the chart object ( I get a subscript out of range error) nor able to find any method that would allow me to turn on/off the visibility of individual series.
Any Ideas?
Whenever I don't know how to do something like this, I turn on the macro recorder.
I had a chart with four series, and I used the filter function in Excel 2013 to hide and show the second series, while the macro recorder was running.
Here's the relevant code:
ActiveChart.FullSeriesCollection(2).IsFiltered = True
' series 2 is now hidden
ActiveChart.FullSeriesCollection(2).IsFiltered = False
' series 2 is now visible
The series type (line or column) does not matter, this works for any of them.
I believe the property you are looking for is the SeriesCollection.Format.Line.Visible property. I quickly created an Excel workbook and added a simple data set (just 1-10) and added a line graph "Chart 2" to the sheet Sheet1.
This code turned the visibility of the line off:
Option Explicit
Private Sub Test()
Dim cht As Chart
Dim ser As Series
'Retrieve our chart and seriescollection objects'
Set cht = Worksheets("Sheet1").ChartObjects("Chart 2").Chart
Set ser = cht.SeriesCollection(1)
'Set the first series line to be hidden'
With ser.Format.Line
.Visible = msoFalse
End With
End Sub
And likewise, setting the ser.Format.Line.Visible property to msoTrue made the line visible again.
As for retrieving the chart itself I had to first activate it, then set my cht variable to the ActiveChart. To view the name of your chart, select it and look in the name box (near where you would enter the cell value / formula).
Update
When using the method above, the series name remains in the legend box. I couldn't find a visibility property for the SeriesCollection in the legend, however one workaround is to simply re-name the series as an empty string (this will make the series disappear from the legend) and then rename the series when you want to show it.
This code below will toggle the visibility of the line and series name in the legend.
Option Explicit
Private Sub Test()
Dim cht As Chart
Dim ser As Series
'Retrieve our chart and seriescollection objects'
Set cht = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
Set ser = cht.SeriesCollection(1)
'Set the first series line to be hidden'
With ser.Format.Line
If .Visible = msoTrue Then
.Visible = msoFalse
ser.Name = vbNullString
Else
.Visible = msoTrue
ser.Name = "Series 1"
End If
End With
End Sub
And, whenever you use .Format.Line.Visible = msoTrue just remember to set ser.Name back to whatever the name for your series is.
There is a simple way to on & off the visibility of the series: using filter on your source data.
May it help you easily as follows.
You can insert a new Window. Setone of them to source data sheet and the other window to Chart sheet. Then arrange the two windows to see both at the same time. Now if you filter the series you like on the source data sheet simultaneously you will see the series you desired on the other sheet.

Delete all command buttons in document and add them back again

I am trying to save some values from all command buttons in Word, then delete them, and then add them back again.
This is for printing purposes.
My question is how can I loop through all the command buttons to save their top, left, name and width values, how can I then delete them and then how can I create new buttons with their previous name, width and position.
I know how to create arrays and whatnot, all I need to figure out is the syntax for looping through the command buttons and for adding new command buttons with the values from the old ones.
Not tested the code but should work with little modification where ever required
Delete Command Button
For Each o In ActiveDocument.InlineShapes
If o.OLEFormat.Object.Name = "CommandButtonName" Then
'Save all properties of the command button using .Top, .Left, .Width etc
o.Delete
End If
Next
For Adding Command Button (Check http://support.microsoft.com/kb/246299):
'Add a command button to a new document
Dim doc As Word.Document
Dim shp As Word.InlineShape
Set doc = Documents.Add
Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
shp.OLEFormat.Object.Caption = "Click Here"
shp.OLEFormat.Object.Left = 100
shp.OLEFormat.Object.Top = 100
shp.OLEFormat.Object.Width = 255
shp.OLEFormat.Object.Visible = True
I've figured resizing the command buttons to 0x0 provided a more elegant option that didn't require arrays and was much quicker.

basic chart + title using vba for ppt

I am new to vba.
I want to generate a ppt with chart and title in it. I am using following code, but having problems. Please suggest.
Set pptobj = CreateObject("PowerPoint.Application")
pptobj.Visible = TRUE
Set presentn= pptobj.Presentations.Add
Dim dirtemp
dirtemp= CreateObject("WScript.Shell").ExpandEnvironmentStrings("%Temp%")
Dim tempImg
tempImg = dirtemp+"\test.gif"
Dim cnt
ind = 1
'my chart is in chartobj
if Not IsNull ( chartobj) then
presentn.Slides.Add ind, 8
chartobj.ExportPicture tempImg, "gif"
presentn.Slides(ind).Shapes(1).TextFrame.TextRange.Text = "some title"
presentn.Slides(ind).Shapes(2).AddPicture tempImg, false, true, 50, 50
ind = ind + 1
end if
I am using ppLayoutChart (value 8).
However if I use layout = 12 (ppLayoutBlank), I am able to generate the chart successfuly, however there is no way I can add title then :(
On a blank layout slide, there will be no shapes, so .Shapes(1) will not return anything (it's throwing an error when you refer to it, yes?)
If you need to add a title to a blank slide, you'll have to add a text shape, set its text and format it to taste.
It might be simpler to add a dummy ppLayoutChart slide, set its title text, copy/paste that onto your real slide, then delete the dummy slide. That will ensure that the "pseudo-title" is formatted as it should be.
Or simpler yet, don't add a blank, add a title only layout slide.

VSTO VB.NET Excel Chart, Subtitle for Chart - How to access chart text box from code?

I have a chart in Excel that I'd like to use a subtitle on. There is no SubTitle Property on the chart object, so I looked in the Excel User interface and found that you can only create a sub-title by using a text box. The text box looks like its associated with the chart, not the worksheet, so how would I access that text box from code?
Thanks...
There is another possibility, and that is to include the subtitle in the chart title, for example:
Dim co As ChartObject
Set co = Sheet1.ChartObjects(1)
co.Chart.HasTitle = True
co.Chart.ChartTitle.Text = "First Quarter Sales" & vbCrLf & "Subtitle"
co.Chart.ChartTitle.Characters(22, 8).Font.FontStyle = "Bold Italic"