Pictographs using VBA

bgeniusz

New Member
Joined
Jan 14, 2012
Messages
2
I am trying to change the columns of a Waterfall chart from bars to an arrow. I know how to accomplish this in Excel but I need to make this happen for the user with VBA. The macro recorder does not work when modifying charts in Excel so I cannot find the code to create a pictograph there or anywhere else online. Any help would be greatly appreciated.

I have also tried to create the arrow and then copy it to the series columns (a procedure that works in Excel but not using VBA).

Is there a way to create a pictograph (column chart using a shape not a rectangle) using VBA?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Here's some VBA to do what I think you want.

  • Copy the two subs below into a Module
  • Create a bar chart (note the name)
  • Create shapes that are to be used (the can be not visible) in the worksheet with the chart (note the name(s))
  • Modify the DoSeries chart_name, series_number, shape_name
  • Run the test
  • Test sub
  • Let me know what needs to be changed
Code:
Sub test()
    Dim sResult As String
    sResult = [COLOR=green][B]DoSeries[/B][/COLOR]("Chart 11", 1, "BlueArrow")
        If sResult <> "" Then MsgBox sResult
    sResult = DoSeries("Chart 11", 2, "RedArrow")
        If sResult <> "" Then MsgBox sResult
End Sub


Code:
Option Explicit
Function DoSeries(sChart, iSeries, sShape) As String
    DoSeries = ""
    On Local Error GoTo errors
    Application.ScreenUpdating = False
    Dim bVis As Boolean: bVis = ActiveSheet.Shapes(sShape).Visible
    ActiveSheet.Shapes(sShape).Visible = True
    Dim ac As Object: Set ac = ActiveCell
    ActiveSheet.Shapes(sShape).Select
    Selection.Copy
    ActiveSheet.ChartObjects(sChart).Activate
    ActiveChart.SeriesCollection(iSeries).Select
    Selection.Paste
    ac.Select
errors:
    If Err <> 0 Then
        DoSeries = Err.Description & String(2, vbCrLf) & _
                   "Chart     " & sChart & vbCrLf & "Series    " & _
                   iSeries & vbCrLf & "Shape    " & sShape
        ac.Select
    End If
    ActiveSheet.Shapes(sShape).Visible = bVis
    Application.ScreenUpdating = True
End Function
 
Upvote 0
Yeah! This code worked for solving my problem. Thank you very much. From what was posted, the code errored out with two lines (both in the "doseries" function:

ActiveChart.ChartObjects(sChart).Activate
ac.Select

I simply remarked them out and the code worked to accomplish the task but the message boxes still showed the error of:
"Object variable or With Block variable not set" and the correct names of the chart, series and arrow.

I then took the msgbox out of the code and ran it again. The code worked and replaced all of the columns with my arrows beautifully. Thank you.

Here is what the final code looks like - since I did not need the msgbox, I simply called the "DoSeries" directly.

Code:
Private Function DoSeries(sChart, iSeries, sShape) As String
    DoSeries = ""
    On Local Error GoTo errors
    Application.ScreenUpdating = False
    Dim bVis As Boolean: bVis = ActiveChart.Shapes(sShape).Visible
    ActiveChart.Shapes(sShape).Visible = True
    Dim ac As Object: Set ac = ActiveCell
    ActiveChart.Shapes(sShape).Select
    Selection.Copy
    'ActiveChart.ChartObjects(sChart).Activate
    ActiveChart.SeriesCollection(iSeries).Select
    Selection.Paste
    ac.Select
errors:
    If Err <> 0 Then
        DoSeries = Err.Description & String(2, vbCrLf) & _
                   "Chart     " & sChart & vbCrLf & "Series    " & _
                   iSeries & vbCrLf & "Shape    " & sShape
        'ac.Select
    End If
    ActiveChart.Shapes(sShape).Visible = bVis
    Application.ScreenUpdating = True
End Function
 
Upvote 0

Forum statistics

Threads
1,214,795
Messages
6,121,624
Members
449,041
Latest member
Postman24

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