MS Excel + VBA: setting data label orientation programmatically - excel

I'm trying to set the orientation of data labels on a bar chart to vertical. Normally I'd find out how to do this by recording a macro, but for some reason it doesn't seem to generate any code. Is it possible to do?
I'm using Excel 2010.

Here is what recording the macro generated:
With Selection.TickLabels
.ReadingOrder = xlContext
.Orientation = xlDownward
End With
Hope this helps! :)

Related

How to change the subtype of Chart from VBA code

I create charts from some data:
ActiveSheet.Shapes.AddChart2(297, xlColumnStacked).Select
But on Excel there are different subtypes of the xlColumnStacked chart...
I need to show the second one, not the default one.
It has to be programmatically, not manually.
I tried to record the macro and see what does the change, but it doesn't record anything... I tried loading from Templates also but they don't work exactly like the one I need.
Thanks for your support.
I thing you have to change .ChartTyp
for example:
ActiveChart.ChartType = 52
Here is list of proper Value
https://bettersolutions.com/excel/charts/vba-chart-types.htm
If you want to switch x axis and y axis data try one of this:
ActiveChart.PlotBy = xlColumns
ActiveChart.PlotBy = xlRows
I hope I helped :)
They both looked like stacked columns. VBA doesn't have chart subtypes, and controls the chart type using this syntax:
ActiveChart.ChartType = xlColumnClustered
ActiveChart.ChartType = xlColumnStacked
ActiveChart.ChartType = xlColumnClusteredStacked100
VBA's Object Browser shows you all of the possibilities.

How can I make PDF with Landscape orientation from an Excel file in Vba Access?

I used to make pdf file from Access forms using this code:
DoCmd.OutputTo acOutputForm,"FR_PFMEAPrint2", acFormatPDF,"PFMEAPrint2.PDF", True
and I printed Excelforms with Portrait orientation in Access using this code:
Excel_App.ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
and for landscape I used this code in Access:
Forms("FR_Print").Printer.Orientation = acPRORLandscape
DoCmd.PrintOut acPages, 1, 1, , 1
but I need to output Excel file to PDF landscape format via Vba Access Thanks
You have to set the property of Excel Sheet object like shown in the sample code snippet below:
Sheets(1).PageSetup.Orientation = xlLandscape
Pertinent to your case it will look like:
Excel_App.ActiveWindow.SelectedSheets(1).PageSetup.Orientation = xlLandscape
or you can iterate through the multiple Sheets and set their property in the loop. Also, FYI: you can use underlying xlLandscape value of 2.
Hope this will help.

Macro to format y- axis in Excel

I've written some code to create a second y-axis in a chart. I recorded a macro when I formated the second y-axis and the code looks great. However, it doesn't work when I try to run it from the VBA code..
I've tried the Format Painter (the first y-axis), which would be optimal if it worked, and also by formating the second y-axis "by hand"
This is what I have (and it's doesn't work..)
Formating the second y-axis to Calibri ("by hand"):
ActiveChart.Axes(xlValue, xlSecondary).Select
Selection.Format.TextFrame2.TextRange.Font.Name = "+mj-lt" 'This line doesn't work...
Selection.Format.TextFrame2.TextRange.Font.Size = 14
Error code: Method TextFrame2 of Object 'ChartFormat' failed
Using Format Painter:
ActiveChart.Axes(xlValue).Select
Selection.Format.PickUp 'This line doesn't work...
ActiveChart.Axes(xlValue, xlSecondary).Select
Error code: Object doesn't support this property or method.
Does anyone know why none of the methods work or if there's a work around?
I found a work around by using:
With ActiveChart.Axes(xlValue, xlSecondary).TickLabels.Font
.Name = "Calibri"
.Size = 16
End with
I haven't been able to solve the issue with Format Painter though...

Accessing PivotChart SeriesCollection with a Sub

I am quite new to VBA. I have written a macro which creates about 10 pivot charts and some normal charts after filtering and cutting some data from a database spreadsheet. I now want to write a sub which goes through applying the same formatting to each one. The sub is as follows:
Sub FormatChart(Cht As ChartObject, title As String)
Cht.Activate
MsgBox ActiveChart.SeriesCollection.Count
With ActiveChart
.HasTitle = True
.ChartTitle.Text = title
.Axes(xlValue).HasMajorGridlines = False
End With
ActiveChart.SeriesCollection(1).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 182, 0)
End With
End Sub
I originally didn't include all the activates and selects, but couldn't get the macro to work without them and don't see it as the end of the world - the datasets are never going to be massive so speed isn't so much of a concern, and I disable screenupdating so that users can't click on cells/other objects and disrupt the macro as it runs.
Here's my problem. If I take out the second With loop, everything proceeds perfectly and the gridlines are removed and the title is added. However, whenever I try to edit the colours of columns with the above I get Run time error '1004': Invalid parameter. I've also tried keeping the content of the second with loop inside the first but then moved it out to try using selection to see if it made a difference.
I've fiddled around quite a bit and recorded various macros changing the colour of the chart in question, but I think the problem might be to do with referencing SeriesCollection as when I try to debug with
MsgBox ActiveChart.SeriesCollection.Count
I get "0".
Please let me know if I'm missing the point - as I said I am new to VBA and am trying to learn as much as possible :)
EDIT: The solution was that I was passing each chart to the above sub after I had created the chart, but before I had set a data source for the chart. Doh!
This obviously meant that there were no seriesCollections for that chart, hence the error I was getting.
I marked Joehannah as answering the question (even though it isn't technically the solution) because it made me check my code and notice that the above could be causing the problem - if I shouldn't do that someone please tell me and I'll try to fix it!
You are better off using Set cht = (create chart object) for each chart and then Immediately calling the format method passing in cht.
I have been told to post my own answer since I figured out the issue.
I was passing each chart to this function just after creating it, as follows:
Set ptr1 = Sheets("withinBRSPivots").PivotTables("UniqueClicks").TableRange1
Set cht1 = Sheets("BRS Overview").ChartObjects.Add(Left:=950, Top:=500, _
Width:=400, Height:=300)
cht1.Name = "UCChart"
Charter.FormatChart cht1, "Average Number of Unqique Clicks"
I then set the source data for the chart after doing the above. This meant that when the chart was passed to my chartformat sub, it had no source data. This is what resulted in being able to edit the title and gridlines, but not the seriesCollection. The fix was to just move the FormatChart sub line to after I had set the source data.
Many thanks to everyone who posted answers!

How to Add Icon to a Excel Menu/Toolbar Button

I need to add a image to a custom toolbar/menu item which is create through VBA.
For a toolbar item, I tried following code
Set NewBtn = TBar.Controls.Add(Type:=msoControlButton)
With NewBtn
.Picture = LoadPicture("mypic.bmp")
.OnAction = "'MyFunction""" & para1 & """'" //VBA Function
'.Caption = "MyFunction"
.TooltipText = "MyFunction"
.Style = msoButtonCaption
End With
In the above code LoadPicture() does not seem to be working. My toolbar is initializing at the workbook load up event. I noticed that the image is loading to the toolbar button, but in a fraction of second it disappears and only item text is displayed. My image is 16x16 pixel bmp one.
Any help appreciate to get around this problem
Thank you
Use MsoButtonStyle.msoButtonIcon or one of the MsoButtonStyle members that contain the word Icon.
In VBA I store the icons on a worksheet (oTemplate) and transfer them to the buttons using:
with NewBtn
oTemplate.Shapes("picCalcOpt").CopyPicture
.PasteFace

Resources