hightides1973
hightides1973

Reputation: 527

dynamic data range for vba chart

The below mentioned code is working properly. But then my query is:--- currently I am having Input datasheet value from A1 to P4 but it can change: for example datavalue can be from A1 to S7. (the data always starts with A in excel sheet) But in the code given below, I have given the range manually. what changes i need to do in range part so that it will automatically find out the column range?

            Sub to_Draw_chart()
               Dim ws_InputSheet As String 
               ws_InputSheet = "Sheet3"
               Charts.Add
               ActiveChart.ChartType = xlColumnStacked
               ActiveChart.SetSourceData Source:=Sheets(ws_InputSheet).Range(ws_InputSheet & "!$A$1:$P$4"), PlotBy:=xlColumns
               With ActiveChart
                   .HasTitle = True
                   .ChartTitle.Characters.Text = "Time_Plotter"
                   .Axes(xlValue).MaximumScale = 1000
                   .Axes(xlValue).MajorUnit = 250
                   .Axes(xlCategory).CategoryType = xlCategoryScale
                   .Axes(xlCategory).CategoryType = xlAutomatic
               End Sub

Upvotes: 0

Views: 1082

Answers (3)

Shai Rado
Shai Rado

Reputation: 33672

I used your ws_InputSheet but As WorkSheet, I think you'll find the code below simplier to follow:

Option Explicit

Sub to_Draw_chart()

    Dim ws_InputSheet As Worksheet, ws_OutputSheet As Worksheet
    Dim dataRng As Range

    Set ws_InputSheet = ThisWorkbook.Sheets("Sheet3")

    With ws_InputSheet
        Set dataRng = .Range("A1").CurrentRegion
    End With

    Charts.Add
    ActiveChart.ChartType = xlColumnStacked

    ActiveChart.SetSourceData Source:=dataRng, PlotBy:=xlColumns
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "Time_Plotter"
        .Axes(xlValue).MaximumScale = 1000
        .Axes(xlValue).MajorUnit = 250
        .Axes(xlCategory).CategoryType = xlCategoryScale
        .Axes(xlCategory).CategoryType = xlAutomatic
    End With

End Sub

Upvotes: 1

Arun Thomas
Arun Thomas

Reputation: 845

Try the below code. This will create the chart from the active selection

Sub to_Draw_chart()
    Dim ws_InputSheet As String
    Dim SelRange As Range
    Sheets("Sheet3").Activate
    Set SelRange = Selection
    ws_InputSheet = "Sheet3"
    Charts.Add
    ActiveChart.ChartType = xlColumnStacked
    ActiveChart.SetSourceData Source:=SelRange, PlotBy:=xlColumns
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "Time_Plotter"
        .Axes(xlValue).MaximumScale = 1000
        .Axes(xlValue).MajorUnit = 250
        .Axes(xlCategory).CategoryType = xlCategoryScale
        .Axes(xlCategory).CategoryType = xlAutomatic
    End With
End Sub

Upvotes: 0

user3598756
user3598756

Reputation: 29421

try this

Option Explicit

Sub to_Draw_chart()
    Dim ws_InputSheet As String, ws_OutputSheet As String

    ws_InputSheet = "Sheet3"

    Dim dataRng As Range
    With Worksheets(ws_InputSheet)
        Set dataRng = .Range("A1").CurrentRegion
    End With

    Charts.Add
    ActiveChart.ChartType = xlColumnStacked
'    ActiveChart.SetSourceData Source:=Sheets(ws_OutputSheet).Range(ws_InputSheet & "!$A$1:$P$4"), PlotBy:=xlColumns
    ActiveChart.SetSourceData Source:=Sheets(ws_OutputSheet).Range(dataRng.Address(True, True, True, True)), PlotBy:=xlColumns
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "Time_Plotter"
        .Axes(xlValue).MaximumScale = 1000
        .Axes(xlValue).MajorUnit = 250
        .Axes(xlCategory).CategoryType = xlCategoryScale
        .Axes(xlCategory).CategoryType = xlAutomatic
    End With
End Sub

Upvotes: 0

Related Questions