Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在vba中固定时间序列的日期轴_Vba_Excel_Graph_Charts - Fatal编程技术网

在vba中固定时间序列的日期轴

在vba中固定时间序列的日期轴,vba,excel,graph,charts,Vba,Excel,Graph,Charts,我有一个从excel中获取数据并生成图形的代码。它需要一些输入(因为完成了多个图形),创建一个新的图表表并插入数据 问题1我的代码从指定的工作表中获取UsedRange,并使用它填充图形。数据从A1开始,一直到结束。但是,其中一个图的第一行中有不应进入图中的数据。例如: 这适用于其中一个法线图: Item 1 Item 2 day 1 100 100 day 2 110 180 day 3

我有一个从excel中获取数据并生成图形的代码。它需要一些输入(因为完成了多个图形),创建一个新的图表表并插入数据

问题1我的代码从指定的工作表中获取UsedRange,并使用它填充图形。数据从A1开始,一直到结束。但是,其中一个图的第一行中有不应进入图中的数据。例如:

这适用于其中一个法线图:

           Item 1         Item 2
 day 1      100            100
 day 2      110            180
 day 3       90            110
 day 4       70            130
等等。这是一个例外:

           Item 1         Item 2
 day 1        1              1
 day 2      110            180
 day 3       90            110
 day 4       70            130
问题1在第二种情况下,我想从图中删除第一行数据(不是标识符行)。既然我使用的是UsedRage,那该怎么做呢

问题2对于所有图表,数据系列从过去的某个点开始,直到今天。问题是,当日期序列被绘制成图表时,它从第一天开始计数(逐步通过周期,例如6个月),直到到达最近的日期

在这种情况下,它不包括今天的日期,这是我试图实现的

举例说明:

Function Grapher(ChartSheetName As String, SourceWorksheet As String, ChartTitle As String, secAxisTitle As String)

Dim lColumn As Long, lRow As Long
Dim LastColumn As Long, LastRow As Long
Dim RetChart As Chart
Dim w As Workbook
Dim RetRange As Range
Dim chrt As Chart
Dim p As Integer
Dim x As Long, y As Long
Dim numMonth As Long
Dim d1 As Date, d2 As Date

Set w = ThisWorkbook

'find limit
LastColumn = w.Sheets(SourceWorksheet).Cells(1,   w.Sheets(SourceWorksheet).Columns.Count).End(xlToLeft).column
LastRow = w.Sheets(SourceWorksheet).Cells(w.Sheets(SourceWorksheet).Rows.Count, "A").End(xlUp).Row

If SourceWorksheet <> "DD" Then 'this is the exception case    
    Set RetRange = w.Sheets(SourceWorksheet).UsedRange 'HOW CAN i CHANGE THE RANGE TO ACCOUNT FOR THE PROBLEM 1?     
Else    
    Set RetRange = w.Sheets(SourceWorksheet).UsedRange                    
End If

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For Each chrt In w.Charts
    If chrt.Name = ChartSheetName Then
        Set RetChart = chrt
        RetChart.Activate
        p = 1
    End If
Next chrt
If p <> 1 Then
    Set RetChart = Charts.Add
End If
'count the number of months in the time series, do the ratio
d1 = w.Sheets(SourceWorksheet).Range("A2").Value
d2 = w.Sheets(SourceWorksheet).Range("A" & LastRow).Value    
numMonth = TestDates(d1, d2)                
x = Round((numMonth / 15), 1)        
'ratio to account for period size
If x < 3 Then
    y = 1
ElseIf x >= 3 And x < 7 Then
    y = 4
ElseIf x > 7 Then
    y = 6
End If            
'create chart
        With RetChart
            .Select
            .ChartType = xlLine
            .HasTitle = True
            .ChartTitle.Text = ChartTitle
            .SetSourceData Source:=RetRange
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = secAxisTitle
            .Name = ChartSheetName
            .SetElement (msoElementLegendBottom)
            .Axes(xlCategory).TickLabelPosition = xlLow
            .Axes(xlCategory).MajorUnit = y
            .Axes(xlCategory).MajorUnitScale = xlMonths
        End With
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function TestDates(pDate1 As Date, pDate2 As Date) As Long
TestDates = DateDiff("m", pDate1, pDate2)
End Function
在本例中,我的数据将持续到12月7日,但最后一个出现的日期是10月31日

问题2这个问题可以解决吗

Obs:我已经尝试使用记录器生成代码,但即使在嵌入的excel选项中,我也找不到任何方法

到目前为止的代码:

Function Grapher(ChartSheetName As String, SourceWorksheet As String, ChartTitle As String, secAxisTitle As String)

Dim lColumn As Long, lRow As Long
Dim LastColumn As Long, LastRow As Long
Dim RetChart As Chart
Dim w As Workbook
Dim RetRange As Range
Dim chrt As Chart
Dim p As Integer
Dim x As Long, y As Long
Dim numMonth As Long
Dim d1 As Date, d2 As Date

Set w = ThisWorkbook

'find limit
LastColumn = w.Sheets(SourceWorksheet).Cells(1,   w.Sheets(SourceWorksheet).Columns.Count).End(xlToLeft).column
LastRow = w.Sheets(SourceWorksheet).Cells(w.Sheets(SourceWorksheet).Rows.Count, "A").End(xlUp).Row

If SourceWorksheet <> "DD" Then 'this is the exception case    
    Set RetRange = w.Sheets(SourceWorksheet).UsedRange 'HOW CAN i CHANGE THE RANGE TO ACCOUNT FOR THE PROBLEM 1?     
Else    
    Set RetRange = w.Sheets(SourceWorksheet).UsedRange                    
End If

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For Each chrt In w.Charts
    If chrt.Name = ChartSheetName Then
        Set RetChart = chrt
        RetChart.Activate
        p = 1
    End If
Next chrt
If p <> 1 Then
    Set RetChart = Charts.Add
End If
'count the number of months in the time series, do the ratio
d1 = w.Sheets(SourceWorksheet).Range("A2").Value
d2 = w.Sheets(SourceWorksheet).Range("A" & LastRow).Value    
numMonth = TestDates(d1, d2)                
x = Round((numMonth / 15), 1)        
'ratio to account for period size
If x < 3 Then
    y = 1
ElseIf x >= 3 And x < 7 Then
    y = 4
ElseIf x > 7 Then
    y = 6
End If            
'create chart
        With RetChart
            .Select
            .ChartType = xlLine
            .HasTitle = True
            .ChartTitle.Text = ChartTitle
            .SetSourceData Source:=RetRange
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = secAxisTitle
            .Name = ChartSheetName
            .SetElement (msoElementLegendBottom)
            .Axes(xlCategory).TickLabelPosition = xlLow
            .Axes(xlCategory).MajorUnit = y
            .Axes(xlCategory).MajorUnitScale = xlMonths
        End With
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function TestDates(pDate1 As Date, pDate2 As Date) As Long
TestDates = DateDiff("m", pDate1, pDate2)
End Function
函数图示器(ChartSheetName为字符串,SourceWorkplace为字符串,ChartTitle为字符串,secAxisTitle为字符串)
昏暗的L柱一样长,L柱一样长
将LastColumn变长,将LastRow变长
作为图表的图表
将w作为工作簿
变暗重新排列为范围
暗chrt为图表
作为整数的Dim p
尺寸x和长度一样,y和长度一样
暗的毫米和长的一样
尺寸d1为日期,d2为日期
Set w=此工作簿
"求极限",
LastColumn=w.Sheets(SourceWorksheet)。单元格(1,w.Sheets(SourceWorksheet)。Columns.Count)。结束(xlToLeft)。列
LastRow=w.Sheets(SourceWorksheet)。单元格(w.Sheets(SourceWorksheet)。Rows.Count,“A”)。结束(xlUp)。行
如果源工作表为“DD”,则“这是例外情况
设置RetRange=w.Sheets(sourcesheet)。UsedRange“如何更改范围以解决问题1?”?
其他的
设置重新排列=w.Sheets(SourceWorksheet)。使用重新排列
如果结束
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
对于w.图表中的每个chrt
如果chrt.Name=ChartSheetName,则
Set RetChart=chrt
激活图表
p=1
如果结束
下一个chrt
如果p1那么
设置图表=图表。添加
如果结束
'计算时间序列中的月数,并计算比率
d1=w.Sheets(源工作表)。范围(“A2”)。值
d2=w.Sheets(SourceWorksheet).Range(“A”&LastRow).值
numMonth=测试日期(d1、d2)
x=圆形((毫米/15),1)
'占期间大小的比率
如果x<3,则
y=1
如果x>=3且x<7
y=4
如果x>7那么
y=6
如果结束
'创建图表
用图表
.选择
.ChartType=xlLine
.hasttle=正确
.ChartTitle.Text=图表标题
.SetSourceData源:=重新安排
.Axes(xlCategory,xlPrimary).hasttle=True
.Axes(xlCategory,xlPrimary).AxisTitle.Characters.Text=“日期”
.Axes(xlValue,xlPrimary).hastintle=True
.Axes(xlValue,xlPrimary).AxisTitle.Characters.Text=secAxisTitle
.Name=图表名称
.SetElement(MSoElementLegendBooth)
.Axes(xlCategory).TickLabelPosition=xlLow
.轴(xlCategory).MajorUnit=y
.Axes(xlCategory).MajorUnitScale=xl个月
以
端函数
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
函数测试日期(pDate1为日期,pDate2为日期)尽可能长
TestDates=DateDiff(“m”,pDate1,pDate2)
端函数
任何帮助都将不胜感激。

问题1:

如果起始单元格应为A2,则可以尝试以下操作:

Set RetRange=w.Sheets(SourceWorksheet).Range(“A2:C”和LastRow)

问题2:

我在你们的图表上看到,X轴上显示的数据间隔是4个月。正如我所知,没有办法强制excel创建不规则的间隔,并强制显示最后一个值的附加标签

您可以尝试将标签添加到最后一个数据点:

编辑:将屏幕添加到我的评论中

问题1:

如果起始单元格应为A2,则可以尝试以下操作:

Set RetRange=w.Sheets(SourceWorksheet).Range(“A2:C”和LastRow)

问题2:

我在你们的图表上看到,X轴上显示的数据间隔是4个月。正如我所知,没有办法强制excel创建不规则的间隔,并强制显示最后一个值的附加标签

您可以尝试将标签添加到最后一个数据点:

编辑:将屏幕添加到我的评论中


在对代码进行了大量修改并得到其他帖子和人员的帮助后,我终于可以解决问题1

问题1可以通过将范围设置为“总计”,然后在创建图形时重置数据来解决。其代码为(添加在“with”中):


问题2我一直在清理互联网,但显然没有办法向后退一步,然后镜像图表,所以这仍然没有解决。

在对代码进行了大量修改并得到其他帖子和人员的帮助后,我终于可以解决问题1

问题1可以通过将范围设置为“总计”,然后在创建图形时重置数据来解决。其代码为(添加在“with”中):

问题2我一直在上网,但显然没有办法解决