VBA change font properties of chart axes without using With-EndWith Statement - excel

I am running an Excel macro in a C# program.
I have a chart and I'd like to change its properties.
Here's the code I've tried:
ActiveSheet.ChartObjects("myChart").Activate
ActiveChart.Axe(xlCategory).Select
With Selection.Format.TextFrame2.TextRange.Font 'Run-Time error: method of object failed
.BaselineOffset = 0
.Bold = msoTrue
.Size = 12
.Italic = msoFalse
End With
However using the With-EndWith statemnt is giving me a run-time error.
Therefore, I'd like to know if there is any code that is equivalent to the code above. I am using Excel 2013.

if your goal is to simply change TickLabels font, may try something like this
ActiveSheet.ChartObjects("myChart").Activate
Dim Axx As Axis
Set Axx = ActiveChart.Axes(xlCategory)
With Axx.TickLabels.Font
.Bold = True
.Size = 12
.Name = "Bookman Old Style"
.Italic = False
.Color = RGB(255, 0, 0)
End With

Related

Excel VBA copy Text Box (Text and Format) to another Text Box (no ActiveX / User Forms)

I am trying to copy the contents (text and format) from a text box on one sheet to another text box on another sheet within the same workbook. I have been able to successfully copy over almost everything, but the justification (center/left/right) is not working for each individual line. I am doing this in a very clunky way: copy the text, then loop through each character to get the format set. There does not seem to be an easy way in excel vba to copy both the text and ALL of the format over. Essentially I am trying to do a "select all (Cntrl-A)", "copy (Cnrl-C)" on the origin textbox, then do a "paste special (keep source formatting)" on the destination text box. IT works wonderfully using the mouse, but I do not want to do that. I just want to run a macro to do the same thing. Also, I noted that when the macro runs, the destination text box applies justification global to the text and I am no longer able to individually select a single line and set its justification (i.e. either all lines are centered or all lines are left justified vs. being able to adjust each line individually). Again, this weird behavior only happens after the macro is run. If I use the mouse cut-and-paste method, the text is able to be justified line-by-line again. Here is my clunky code:
Sub Update_CARD_LEG_BACK()
' Set varibles to reduce typing and make changing origin and destination text boxes easier.
Set Orig = Sheets("MAIN_INPUT2").Shapes("CARD_LEG_BACK")
Set Orig_Sheet = Sheets("MAIN_INPUT2")
Set Dest = Sheets("CARD_LEGACY").Shapes("BACK")
Set Dest_Sheet = Sheets("CARD_LEGACY")
'Copy text from origin text box to destination text box. Copies only the text NO formating.
Dest.TextFrame.Characters.Text = Orig.TextFrame.Characters.Text
For i = 1 To Len(Orig.TextFrame.Characters.Text)
Dest.TextFrame.Characters(i, 1).Font.Underline = Orig.TextFrame.Characters(i, 1).Font.Underline
With Dest.TextFrame2.TextRange.Characters(i, 1)
.Text = Orig.TextFrame2.TextRange.Characters(i, 1).Text
With .Font
.Name = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Name
.Size = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Size
.Bold = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Bold
.Strikethrough = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Strikethrough
.Superscript = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Superscript
.Subscript = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Subscript
.Fill.ForeColor.RGB = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Fill.ForeColor.RGB
.Fill.BackColor.RGB = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Fill.BackColor.RGB
.Fill.Visible = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Fill.Visible
.Fill.Transparency = Orig.TextFrame2.TextRange.Characters(i, 1).Font.Fill.Transparency
End With
With .ParagraphFormat
.BaselineAlignment = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.BaselineAlignment
.SpaceWithin = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.SpaceWithin
.SpaceBefore = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.SpaceBefore
.SpaceAfter = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.SpaceAfter
.IndentLevel = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.IndentLevel
.FirstLineIndent = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.FirstLineIndent
.Alignment = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.Alignment
.HangingPunctuation = Orig.TextFrame2.TextRange.Characters(i, 1).ParagraphFormat.HangingPunctuation
End With
End With
Next i
'Copy fill color of origin text box to destination text box. Also copies transparancy (required for 'no fill' option to copy correctly).
Dest.Fill.ForeColor.RGB = Orig.Fill.ForeColor.RGB
Dest.Fill.Transparency = Orig.Fill.Transparency
End Sub
You could replace the second with a copy of the first:
Sub Tester()
ReplaceWithCopy Sheet1.Shapes("SourceTB"), Sheet2.Shapes("DestTB")
End Sub
Sub ReplaceWithCopy(shpSrc As Shape, shpDest As Shape)
Dim nm As String
shpSrc.Copy
shpDest.Parent.Paste
With shpDest.Parent.Shapes(shpDest.Parent.Shapes.Count)
.Left = shpDest.Left
.Top = shpDest.Top
.Width = shpDest.Width
.Height = shpDest.Height
nm = shpDest.Name
shpDest.Delete 'remove the shape being replaced
.Name = nm 'rename copy to just-deleted shape
End With
End Sub

VBA Excel Changing the features of textbox with formula

I have managed with input the textbox to the formula, as per the following query, which I raised a while ago...
VBA Excel how to write Excel formula in the textbox
and everything is fine, but I have got problems with input the proper font features into this textbox.
Basically I have two separate sets of code, which I would love to combine into the one
Sub Duct1()
Set myDocument = ActiveSheet
With myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 300, 140, 180, 30)
.name = "Duct1"
With .TextFrame
.HorizontalAlignment = xlLeft
With .Characters
.Text = "1W-20mm/90' upturn"
.Font.ColorIndex = 3
.Font.Size = 16
.Font.Bold = True
End With
End With
.Rotation = 25
.Fill.Visible = False
.Line.Visible = False
End With
End Sub
Sub Duct1Desc()
ActiveSheet.Shapes("Duct1").OLEFormat.Object.Formula = "=AB1"
End Sub
For the second code I tried also:
Sub Duct1Desc()
ActiveSheet.Shapes("Duct1").OLEFormat.Object.Formula = "=AB1"
With ActiveSheet.Shapes("Duct1")
.Font.ColorIndex = 3
.Font.Size = 16
.Font.Bold = True
End With
End Sub
But in this issue I have got an error, that VBA doesn't support this property or method.
Can anyone help me to bind these 2 codes together?
Thanks
This works for me:
Dim s As Shape
Set s = ActiveSheet.Shapes("myBox")
s.DrawingObject.Formula = "=B2"
OK I thought the problem was the linking, not the formatting: this works for me.
Sub Duct1Desc()
Dim s
Set s = ActiveSheet.Shapes("Duct1")
s.OLEFormat.Object.Formula = "=A1"
With s.DrawingObject
.Font.ColorIndex = 3
.Font.Size = 20
.Font.Bold = True
End With
End Sub

Why are FullSeriesCollection format changes affect two data series at the same time?

I have an XY scatter chart with seven data series on it (using Office 365). I am trying to set the format for the data series using VBA, and it all works fine with the exception that the first series is inexplicably linked to the seventh series. The code looks something like this:
'Series 0
With Sheet1.ChartObjects("MyChart").Chart.FullSeriesCollection("Type_0")
With .Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
End With
.Format.Glow.Radius = 0
.Format.Line.Visible = msoFalse
.MarkerStyle = 1
End With
'Series 1 to 6
For i = 1 To 6
With Sheet1.ChartObjects("MyChart").Chart.FullSeriesCollection("Type" & i)
.MarkerStyle = -4105 'On
.Format.Line.Visible = msoFalse
With .Format.Glow
.Radius = 0
.Color.ObjectThemeColor = msoThemeColorAccent6
.Color.TintAndShade = 0
.Color.Brightness = 0
.Transparency = 0
.Radius = 5
.Color.RGB = RGB(105, 211, 33)
End With
With .Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
End With
End With
Next i
It doesn't matter which order I put those blocks in, or even if I try to adjust the series formatting manually in the Format Shape window, anytime I try to change Series7, the exact same change applies to Series0 and vice versa.
What kind of awful setting have I enabled that makes that happen, and how do I turn it off?

Excel button VBA not working in 64 bit Excel 2016

I have been using 32 bit MS Excel and this Macro worked fine. Now I have moved to 64 bit Excel 2016 and macro is not working. What is the problem with it? Is there a solution to get it work with both 32 bit and 64 bit versions?
Sub Rectangle_Clicked()
Button = Application.Caller
Rectangle_Toggle (Button)
End Sub
Sub Rectangle_Toggle(Button As Variant)
With Application.ActiveSheet.Shapes(Button)
'Transparency of the shape 1 = transparent, 0 = solid
.Fill.Transparency = 1
'Hides the cell's value
Range(.TopLeftCell.Address).Font.Color = Range(.TopLeftCell.Address).Interior.Color
If .TextFrame.Characters.Text = Chr$(252) Then
.TextFrame.Characters.Text = ""
.Fill.ForeColor.RGB = RGB(255, 255, 255)
Range(.TopLeftCell.Address).Value = False
Else
.TextFrame.Characters.Text = Chr$(252)
.Fill.ForeColor.RGB = RGB(46, 208, 80) '(50, 195, 50) <-- change your color here
Range(.TopLeftCell.Address).Value = True
End If
End With
End Sub
I am getting an error "Can't find project in library". And VB highlights word button in Button = Application.Caller

Formatting the width of y-axis labels

I am working on a VBA Excel script that uses a pivot table to create a bunch of charts and copy them into a Word document, creating a report.
The charts come in pairs and, after pasting them, there's a slight "distance" between the y-axes.
I wish to have them make a single line if they were extended.
I'm using the TickLables.Offset property. It's set to the same value in both charts, yet that doesn't do the trick.
Any suggestions are welcome.
Edit:
A screenshot of the issue:
Code:
ActiveChart.Axes(xlValue).Select
Selection.TickLabels.AutoScaleFont = True
With Selection.TickLabels.Font
.Name = "Arial"
.FontStyle = "Normal"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
With Selection.TickLabels
.Offset = 500
End With
Since Offset is an inter percentage, as in .Axes(xlValue).TickLabels.Offset it is hard to pin down. To align things, in my case within a single chart, I get perfect results with:
.Legend.Left = .PlotArea.Left + .PlotArea.Width - .PlotArea.InsideWidth
.Legend.Width = .PlotArea.InsideWidth
In other words when you have TickLables only on the left (or right), the width of the Ticklables is the difference between the inside and outside Widths.

Resources