Insert line in graph with macro

elbartje

New Member
Joined
Oct 28, 2015
Messages
39
I want to insert a line in a graph the x en y data needs to come from an userform (an input box is also ok).
I dont want to select a range of cells but directly enter a value(s) for the x and y data.
This data should not be put in cells but only in the graph itself

So I select a graph, run the macro I get a popup to enter x and y data for example X1=0 X2=10 and Y1=120 and Y2=120.
Now a horizontal line is plotted in the graph.
 
I think I need somthing like this:

Code:
Case xlLine

With .Axes(xlCategory)
        n = Value.MinimumScal
        m = Value.MaximumScale
    End With
        
For i = n To m
ReDim matrixx(i)
ReDim Preserve matrixx(UBound(yourArray) + 1)
matrixy(UBound(matrix)) = CDbl(Upperlimit.Text)



   With .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = r           '(r= titelname)
    .SeriesCollection(1).Values = matrixy
    
    End With

I send you my whole file becaus now I also get a case ... select error :(
(Case else and case xlXYScatter are normaly working perfect)
MY FILE
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Ok I fixed some problems!
I can plot a line in my x1Line chart but still not what I want.
My code is doing somthing strange but I dont know why:
Code:
Case xlLine


With .Axes(xlValue)
n = .MinimumScale
m = .MaximumScale

    End With
        
For i = n To m
ReDim matrixy(i)
ReDim Preserve matrixy(UBound(matrixy) + 1)
matrixy(UBound(matrixy)) = CDbl(Upperlimit.Text)
Next i


   With .SeriesCollection.NewSeries
    .Name = r
    .Values = matrixy
End With

The file
 
Upvote 0
When I read out the scale he uses the Y scale not the scale from x.
That explains a part of the problem.
I need some function that counts the data (from 1 line) in the graph, this wil represent my x scale.
 
Upvote 0
Hi again,

I think you need to try something like this:
Code:
' X1line
    Case xlLine
        With ActiveSheet.ChartObjects(1).Chart
            y = .SeriesCollection(1).XValues
        End With
    
        For i = LBound(y) To UBound(y)
            y(i) = CDbl(Upperlimit.Text)
        Next

        With .SeriesCollection.NewSeries
            .Name = r
            .Values = y
        End With
        
        For i = LBound(y) To UBound(y)
            y(i) = CDbl(Lowerlimit.Text)
        Next

        With .SeriesCollection.NewSeries
            .Name = r
            .Values = y
        End With
        
    'case else
    Case Else

This section:
Code:
        With ActiveSheet.ChartObjects(1).Chart
            y = .SeriesCollection(1).XValues
        End With
is used to create an array called y that has the same number of elements as there are categories on the x-axis. The values will be overwritten next.

The values in the array are changed to be the UpperLimit values in this loop:
Code:
        For i = LBound(y) To UBound(y)
            y(i) = CDbl(Upperlimit.Text)
        Next

Then a new series is created to add that line to the chart:
Code:
        With .SeriesCollection.NewSeries
            .Name = r
            .Values = y
        End With

The process is then repeated using the lower limit.
 
Upvote 0
Hi RickXL,
I wonder whether you can help me as well. I have a similar problem. I have created a clustered Bar chart where bars measure how many days it took so far to complete a certain task by a certain person. On my X axis I've got peoples' names and y axis horizontal bars showing the amount of days. As soon as it passes 14 days I want to draw a red limit line to show colleagues that they exceeded the limit. At the moment I draw it manually, but I would like to have it appeared automatically. On top of that red limit line I want to have it mentioned '14 days'. I could possibly use VBA, but would need some help with them.
Many thanks for your help.
Regards,
Vytas
 
Upvote 0
Hi,

First, it is usually better to create a new question rather than adding yours to the end of someone else's. That way, the people who can best solve the problem are the most likely to answer. Also, people who answered earlier questions may have left the forum and so it could be that no-one will see your question.

From what you have said it sounds as if you really need a stacked bar chart and might not need a macro at all. For instance, if you chart the first three columns here. Column D has the actual days in it and columns B and C are the values charted:


Excel 2013
ABCD
1NameNumber1Number2Days
2jan14115
3feb505
4mar707
5apr141327
6may141024
7jun909
8jul606
9aug141024
10sep14822
11oct141226
12nov13013
13dec14317
Sheet1
Cell Formulas
RangeFormula
B2=MIN(D2,14)
C2=MAX(0,D2-14)
D2=RANDBETWEEN(0,28)


Regards,
 
Upvote 0

Forum statistics

Threads
1,222,629
Messages
6,167,190
Members
452,104
Latest member
jadethejade

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