简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM