vba图表的动态数据范围

时间:2016-09-07 11:01:42

标签: excel vba excel-vba

以下提到的代码工作正常。但后来我的询问是:--- 目前我的输入数据表值从A1到P4但它可以改变:例如datavalue可以从A1到S7。 (数据始终以excel表中的A开头) 但是在下面给出的代码中,我手动给出了范围。我需要在范围部分进行哪些更改,以便自动找出列范围?

            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

3 个答案:

答案 0 :(得分:1)

我使用了您的 ws_InputSheet 但是As WorkSheet,我想您会发现下面的代码更简单:

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

答案 1 :(得分:0)

试试这个

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

答案 2 :(得分:0)

尝试以下代码。这将从活动选择

创建图表
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