[英]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.
但是然后我的查询是:---目前我的输入数据表值从A1到P4,但是它可以更改:例如datavalue可以从A1到S7。 (the data always starts with A in excel sheet) But in the code given below, I have given the range manually.
(数据总是在excel表中以A开头),但是在下面给出的代码中,我手动给出了范围。 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: 我使用了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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.