Consulting

Results 1 to 13 of 13

Thread: Changing name and range of horizonal and vertical axis in a stacked bar chart

  1. #1
    VBAX Regular
    Joined
    Nov 2015
    Posts
    17
    Location

    Changing name and range of horizonal and vertical axis in a stacked bar chart

    Hello everyone.

    I want to generate a gantt chart via excel and VBA. So far, What I did is:
    Sub chart()
    
    
    Dim TheChart As chart
    Dim MySheets As Sheets
    
    
    ActiveSheet.Shapes.AddChart(xlBarStacked).Select
    'Series#1
    With ActiveChart.SeriesCollection.NewSeries
    .XValues = ActiveSheet.Range("B22:B25")
    .Name = ActiveSheet.Range("B22")
    .Values = ActiveSheet.Range("B23:B25")
    End With
    
    'Series#2
    With ActiveChart.SeriesCollection.NewSeries
    .XValues = ActiveSheet.Range("D22:D25")
    .Name = ActiveSheet.Range("D22")
    .Values = ActiveSheet.Range("D23:D25")
    End With
    
    With ActiveChart
    .HasTitle = True
    .ChartTitle.Text = "Scheduling"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Text = Range("A22")
    .Axes(xlCategory).ReversePlotOrder = True
    
    .Axes(xlValue).MajorGridlines.Delete
    .SeriesCollection(1).Interior.ColorIndex = xlNone
    .Legend.Delete
    End With
    
    End Sub
    And my graph looks like this: Capture-1.jpg

    What I still can't do are:
    1. To have the task name (Task1, Task2, Task3) in the vertical axis and Date in the horizontal axis.
    2. To change the maximum and minimum range of the horizontal axis. I realized that XValues is not for the vertical axis here, however, I still can't make it work.
    Have anyone encountered this before? And how should I do these?

    Thanks a lot for your help!

    Elizabeth

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What does the data look like?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Nov 2015
    Posts
    17
    Location
    Hello xld,
    the data looks like this
    Capture2.jpg

    and I already changed the VBA code. However, the chart resulted from the code is not consistent. I dont know where went wrong.
    Sub chart()
    
    
    'Dim TheChart As chart
    'Dim MySheets As Sheets
    Dim begindate As Date
    
    
    ActiveSheet.Shapes.AddChart(xlBarStacked).Select
    begindate = Range("B2")
    
    
    With ActiveChart.SeriesCollection.NewSeries
    .XValues = ActiveSheet.Range("A2:A4")
    .Name = ActiveSheet.Range("D1")
    .Values = ActiveSheet.Range("D2:D4")
    End With
    
    
    With ActiveChart.SeriesCollection.NewSeries
    .XValues = ActiveSheet.Range("A2:A4")
    .Name = ActiveSheet.Range("F1")
    .Values = ActiveSheet.Range("F2:F4")
    End With
    
    
    With ActiveChart
    .HasTitle = True
    .ChartTitle.Text = "Scheduling"
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Text = Range("A1")
    .Axes(xlCategory).ReversePlotOrder = True
    
    
    .Axes(xlValue).MajorGridlines.Delete
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Text = "Date"
    .Axes(xlValue, xlPrimary).TickLabels.NumberFormat = "d-mm-yy"
    .Axes(xlValue, xlPrimary).MinimumScale = begindate
    
    
    .SeriesCollection(1).Interior.ColorIndex = xlNone
    .Legend.Delete
    End With
    
    
    End Sub
    Thanks!

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Looks good to me, what do you mean by '... not consistent'?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Nov 2015
    Posts
    17
    Location
    So, the first idea is to have it updated automatically every time people change the "Date" in B2.
    When I change the B2 Cell, the graph will look like this:

    Capture3.jpg

    However, if I try several time to generate the graph, I can get a result like this:
    Capture4.jpg

    Thanks!

  6. #6
    VBAX Regular
    Joined
    Nov 2015
    Posts
    17
    Location
    If anyone knows any other way that is less complicated than this, I am open to suggestions!

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You are confusing me now. How does changing B2 generate a completely style of chart? And what do you mean by '... if I try several time to generate the graph'?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Regular
    Joined
    Nov 2015
    Posts
    17
    Location
    Hello xld,

    That is actually my question as well.

    step 1: I change B2 cell to other date (for example 22/03/15) and run the vba. The chart generated will be the first picture in the previous post (colorful bar).
    step 2: I delete that colorful chart and run again the vba (B2 cell is still 22/03/15). What I meant by ' ... if I try several time to generate the graph' is that I try to do step 2 several time.
    After sometimes, the chart generated will be as the second picture in the previous post (the red color bar).

    Actually I don't know what went wrong. I am sorry for the confusion.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I don't get anything like that. Can you post the workbook, I am intrigued?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I finally managed to reproduce what you are seeing. This seems to fix it

    Sub chart()
        Dim this As Worksheet
        Dim chtObj As ChartObject
        Dim begindate As Date
         
        Set this = ActiveSheet
         
        Set chtObj = this.ChartObjects.Add(200, 100, 375, 225)
        With chtObj.chart
             
            .ChartType = xlBarStacked
            begindate = this.Range("B2").Value
             
            With .SeriesCollection.NewSeries
                .XValues = this.Range("A2:A4")
                .Name = this.Range("D1").Value
                .Values = this.Range("D2:D4")
            End With
             
            With .SeriesCollection.NewSeries
                .XValues = this.Range("A2:A4")
                .Name = this.Range("F1").Value
                .Values = this.Range("F2:F4")
            End With
             
            .HasTitle = True
            .ChartTitle.Text = "Scheduling"
             
            With .Axes(xlCategory, xlPrimary)
                 
                .ReversePlotOrder = True
            End With
             
            .Axes(xlValue).MajorGridlines.Delete
            With .Axes(xlValue, xlPrimary)
                 
                .TickLabels.NumberFormat = "d-mm-yy"
                .MinimumScale = begindate
            End With
             
            .SeriesCollection(1).Interior.ColorIndex = xlNone
            .Legend.Delete
        End With
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    VBAX Regular
    Joined
    Nov 2015
    Posts
    17
    Location
    Hello xld,
    Sorry didnt read the message yesterday.

    it is solved! Thanks a lot!
    Is it because of this code?
    ActiveSheet.Shapes.AddChart(xlBarStacked).Select
    Do you mind to tell me what's the difference between this code and yours?
    Set chtObj = this.ChartObjects.Add(200, 100, 375, 225)
    I just realized that if my cursor is located in empty cell and not close to other filled cell, the resulted graph is correct. Otherwise, it creates the wrong one.

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I am not really sure Elizabeth. Looking at your code, I came to the conclusion that all the selecting, and not fully qualifying ranges, might be the problem, so I rewrote it as I would have written it, adding a chart object and then referencing that object.

    Your observation regarding the activecell does support my thoughts on the code selecting cells I think, and maybe why I didn't see the problem for a while.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    Good function

    Thank you

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •