vba graph

Hi,

I recorded macro to graph lines. I got the following:

    'ActiveSheet.Shapes.AddChart.Select
    'ActiveChart.ChartType = xlLine
    'ActiveChart.ChartArea.Select
    'ActiveSheet.Shapes("Chart 5").IncrementLeft -5.25
    'ActiveSheet.Shapes("Chart 5").IncrementTop 99.75
    'ActiveChart.ChartArea.Select
    'ActiveChart.SeriesCollection.NewSeries
    'ActiveChart.SeriesCollection(1).Name = "=GetData!$C$10"
    'ActiveChart.SeriesCollection(1).Values = "=GetData!$C$11:$C$22"
    'ActiveChart.SeriesCollection(1).XValues = "=GetData!$B$11:$B$22"
    'ActiveChart.ChartArea.Select
    'ActiveChart.HasAxis(xlTimeScale) = True
    'ActiveChart.Axes(xlCategory).Select
    'ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale

But I want to dynamically select columns and rows and instead of  "=GetData!$C$10" I want to use
cells(10, emptyCol + 1).Value. Also for  "=GetData!$C$11:$C$22" I want to scroll down to the last non empty row. I have stored the column in a variable. How can I do that.

Thanks
camper12Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kanti PrasadCommented:
Hi

I am not sure if I understood you right but try to change the below code accordingly

 Dim ws As Worksheet
   On Error Resume Next
   Set RngB = ws.Range(ws.Cells(11, 2), ws.Range("B" & Cells.Rows.Count).End(xlUp))
   Set RngC = ws.Range(ws.Cells(11, 3), ws.Range("C" & Cells.Rows.Count).End(xlUp))
   
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.ChartArea.Select
    ActiveSheet.Shapes("Chart 5").IncrementLeft -5.25
    ActiveSheet.Shapes("Chart 5").IncrementTop 99.75
    ActiveChart.ChartArea.Select
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).Name = ws.Cells(10, 3)
    ActiveChart.SeriesCollection(1).Values = RngC
    ActiveChart.SeriesCollection(1).XValues = RngB
    ActiveChart.ChartArea.Select
    ActiveChart.HasAxis(xlTimeScale) = True
    ActiveChart.Axes(xlCategory).Select
    ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
camper12Author Commented:
Hi,

B and C are hardcoded in the example you have give. For my code I have the column number stored in a variable. For instance chart data is obtained from column B, I am using column number 2 which is stored in an integer.

Thanks
Kanti PrasadCommented:
Hi

Sorry are you looking for something as below

 Set RngB = ws.Range(ws.Cells(11, 2), ws.Range(ws.Cells(, 2) & Cells.Rows.Count).End(xlUp))
   Set RngC = ws.Range(ws.Cells(11, 3), ws.Range(ws.Cells(, 3) & Cells.Rows.Count).End(xlUp))
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Kanti PrasadCommented:
Dim i, j
    i = 2 ' put your values here
    j = 3  ' put your values here


   Set RngB = ws.Range(ws.Cells(11, 2), ws.Range(ws.Cells(, i) & Cells.Rows.Count).End(xlUp))
   Set RngC = ws.Range(ws.Cells(11, 3), ws.Range(ws.Cells(, j) & Cells.Rows.Count).End(xlUp))
camper12Author Commented:
Hi,

I am using the following code:

   Set RngX = ws.Range(ws.Cells(11, 2), ws.Range(ws.Cells(, 3) & Cells.Rows.Count).End(xlUp))
   'End If

    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.ChartArea.Select
    ActiveSheet.Shapes ("Chart 5")
   
   ' ActiveSheet.Shapes("Chart 5").IncrementLeft -5.25
    'ActiveSheet.Shapes("Chart 5").IncrementTop 99
    ActiveChart.ChartArea.Select
    ActiveChart.SeriesCollection.XValues = RngX
    ActiveChart.SeriesCollection(1).XValues = RngX
    ActiveChart.ChartArea.Select
    ActiveChart.HasAxis(xlTimeScale) = True
    ActiveChart.Axes(xlCategory).Select
    ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
    ActiveChart.SeriesCollection(1).XValues = RngX
    For i = 1 To UBound(InputRecords, 1)
    ActiveChart.SeriesCollection.NewSeries
    If i = 1 Then
    ActiveChart.SeriesCollection(i).Name = ws.Cells(10, 3)
    Set RngY = ws.Range(ws.Cells(11, 2), ws.Range(ws.Cells(, 3) & Cells.Rows.Count).End(xlUp))
    ActiveChart.SeriesCollection(i).Values = RngY
    Else
    ActiveChart.SeriesCollection(i).Name = ws.Cells(10, i * 2)
    Set RngY = ws.Range(ws.Cells(11, 2), ws.Range(ws.Cells(, i * 2) & Cells.Rows.Count).End(xlUp))
    ActiveChart.SeriesCollection(i).Values = RngY
    End If
  Next i

For some reason my graph is not appear!

Any inputs?

Thanks
Kanti PrasadCommented:
Sub Macro7()
Dim InputRecords
Dim RngX As Range
Dim RngY As Range

With Sheets("Sheet1")
   InputRecords = Range("B10:C10")
   
   Set RngX = ActiveSheet.Range(ActiveSheet.Cells(11, 1), ActiveSheet.Cells(11, 1).End(xlDown))
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.ChartArea.Select
    ActiveChart.Parent.Name = "Chart 5"
    ActiveSheet.Shapes("Chart 5").IncrementLeft -5.25
    ActiveSheet.Shapes("Chart 5").IncrementTop 99
    ActiveChart.ChartArea.Select
    ActiveChart.HasAxis(xlTimeScale) = True
    ActiveChart.Axes(xlCategory).Select
    ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
    ActiveChart.SeriesCollection(1).XValues = RngX
    For i = 1 To UBound(InputRecords, 1)
    ActiveChart.SeriesCollection.NewSeries
   If i = 1 Then
        Set RngY = ActiveSheet.Range(ActiveSheet.Cells(11, 2), ActiveSheet.Cells(11, 2).End(xlDown))
        RngY.Select
        With ActiveChart.SeriesCollection(i)
            .Name = ActiveSheet.Cells(10, 2)
            .Values = RngY
        End With
   Else
        
        Set RngY = ActiveSheet.Range(ActiveSheet.Cells(11, 2), ActiveSheet.Cells(11, i * 2).End(xlDown))
        With ActiveChart.SeriesCollection(i)
            .Name = ActiveSheet.Cells(10, i * 2)
            .Values = RngY
        End With
   End If
  Next i

End With

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.