此子过程用于更新聚集柱形图的源数据(SetSourceData)。它从Project Jmeter 板工作表中提取的图表和表。
当工作簿中的其他两个子目录中的任何一个的下拉唐斯中触发与更改相关的事件过程时,将运行此代码。
需要注意的是,虽然这个子过程可以使用硬编码的单元格引用,但我想让它更健壮,因此我引用了命名范围,这可以在最后一个If语句的Else部分中看到。这是子过程中唯一不起作用的部分。
我想知道如何让我的MySQL代码工作,以便它引用用于创建集群柱状图的表中的源区域。
Sub changeSourceData()
Dim currentMonth As String
Dim projectDashboardWorksheet As Object
Dim matchRange As String
Dim fyMonths As Variant
Dim monthIndex As Integer
Dim expandRange As Integer
Dim chartRangeMonths As Range
Dim chartRangeValues As Range
currentMonth = Evaluate(ThisWorkbook.Names("CoverWorksheet_Current_Month_DropDown").Value)
Set projectDashboardWorksheet = Sheet13.ChartObjects("Chart 3")
matchRange = Range("CoverWorksheet_Current_Month_DropDown").Value
fyMonths = Array("Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar")
monthIndex = Application.Match(matchRange, fyMonths, 0)
If Range("CoverWorksheet_Current_Month_DropDown") = "Apr" Then
expandRange = 1
ElseIf Range("CoverWorksheet_Current_Month_DropDown") = "May" Then
expandRange = 2
ElseIf Range("CoverWorksheet_Current_Month_DropDown") = "Jun" Then
expandRange = 3
ElseIf Range("CoverWorksheet_Current_Month_DropDown") = "Jul" _
Or Range("CoverWorksheet_Current_Month_DropDown") = "Aug" _
Or Range("CoverWorksheet_Current_Month_DropDown") = "Sep" _
Or Range("CoverWorksheet_Current_Month_DropDown") = "Oct" _
Or Range("CoverWorksheet_Current_Month_DropDown") = "Nov" _
Or Range("CoverWorksheet_Current_Month_DropDown") = "Dec" _
Or Range("CoverWorksheet_Current_Month_DropDown") = "Jan" _
Or Range("CoverWorksheet_Current_Month_DropDown") = "Feb" _
Or Range("CoverWorksheet_Current_Month_DropDown") = "Mar" _
Then
expandRange = Evaluate(ThisWorkbook.Names("Profile_Of_Income_Data_Source_Range").Value)
End If
If currentMonth = "Apr" _
Or currentMonth = "May" _
Or currentMonth = "Jun" Then
Set chartRangeMonths = Range("Profile_Of_Income_Months_archived_HEADER").Resize(Range("Profile_Of_Income_Months_archived_HEADER").Rows.Count + expandRange - 1).Offset(1, 0)
Set chartRangeValues = Range("Profile_Of_Income_MonthsRANGE").Resize(Range("Profile_Of_Income_MonthsRANGE").Rows.Count + expandRange - 1).Offset(1, 0)
ElseIf currentMonth = "Jul" _
Or currentMonth = "Aug" _
Or currentMonth = "Sep" _
Or currentMonth = "Oct" _
Or currentMonth = "Nov" _
Or currentMonth = "Dec" _
Or currentMonth = "Jan" _
Or currentMonth = "Feb" _
Or currentMonth = "Mar" Then
If Profile_Of_Income_Data_Source_Range = 1 Then
Set chartRangeMonths = Range("Profile_Of_Income_Months_archived_HEADER").Resize(Range("Profile_Of_Income_Months_archived_HEADER").Rows.Count + expandRange - 1).Offset(fyMonths, 0)
Set chartRangeValues = Range("Profile_Of_Income_MonthsRANGE").Resize(Range("Profile_Of_Income_MonthsRANGE").Rows.Count + expandRange - 1).Offset(fyMonths, 0)
ElseIf Profile_Of_Income_Data_Source_Range = 2 Then
Set chartRangeMonths = Range("Profile_Of_Income_Months_archived_HEADER").Resize(Range("Profile_Of_Income_Months_archived_HEADER").Rows.Count + expandRange - 1).Offset(fyMonths - 1, 0)
Set chartRangeValues = Range("Profile_Of_Income_MonthsRANGE").Resize(Range("Profile_Of_Income_MonthsRANGE").Rows.Count + expandRange - 1).Offset(fyMonths - 1, 0)
ElseIf Profile_Of_Income_Data_Source_Range = 3 Then
Set chartRangeMonths = Range("Profile_Of_Income_Months_archived_HEADER").Resize(Range("Profile_Of_Income_Months_archived_HEADER").Rows.Count + expandRange - 1).Offset(fyMonths - 2, 0)
Set chartRangeValues = Range("Profile_Of_Income_MonthsRANGE").Resize(Range("Profile_Of_Income_MonthsRANGE").Rows.Count + expandRange - 1).Offset(fyMonths - 2, 0)
ElseIf Profile_Of_Income_Data_Source_Range = 4 Then
Set chartRangeMonths = Range("Profile_Of_Income_Months_archived_HEADER").Resize(Range("Profile_Of_Income_Months_archived_HEADER").Rows.Count + expandRange - 1).Offset(fyMonths - 3, 0)
Set chartRangeValues = Range("Profile_Of_Income_MonthsRANGE").Resize(Range("Profile_Of_Income_MonthsRANGE").Rows.Count + expandRange - 1).Offset(fyMonths - 3, 0)
End If
End If
If currentMonth = "Apr" Then
projectDashboardWorksheet.Chart.SetSourceData _
Source:=Range("Profile_Of_Income_Months_archived_HEADER,Profile_Of_Income_MonthsRANGE,'Project Dashboard'!$F$57,'Project Dashboard'!$H$57:$S$57")
ElseIf currentMonth = "May" Then
projectDashboardWorksheet.Chart.SetSourceData _
Source:=Range("Profile_Of_Income_Months_archived_HEADER,Profile_Of_Income_MonthsRANGE,'Project Dashboard'!$F$57:$F$58,'Project Dashboard'!$H$57:$S$58")
ElseIf currentMonth = "Jun" Then
projectDashboardWorksheet.Chart.SetSourceData _
Source:=Range("Profile_Of_Income_Months_archived_HEADER,Profile_Of_Income_MonthsRANGE,'Project Dashboard'!$F$57:$F$59,'Project Dashboard'!$H$57:$S$59")
Else
projectDashboardWorksheet.Chart.SetSourceData _
Source:=Range("Profile_Of_Income_Months_archived_HEADER,Profile_Of_Income_MonthsRANGE,'Project Dashboard'! & chartRangeMonths.Address & ,'Project Dashboard'! & chartRangeMonths.Address & ")
End If
End Sub
字符串
2条答案
按热度按时间ruoxqz4g1#
按期间号扩展范围,即1表示4月,12表示3月
字符串
kx7yvsdv2#
从您的代码来看,问题似乎出在上一个
If
语句的Else
部分中的Offset
方法上。Offset
方法需要数值参数,但您提供了fyMonths
,这是一个字符串数组。此外,在代码的最后一部分,在设置图表的源数据时,在
Range
函数中使用了两次chartRangeMonths.Address
。这也可能会导致问题。字符串
在这个修订版本中,我已经将
Range
函数中的第二个chartRangeMonths.Address
替换为chartRangeValues.Address
。这应该可以正确设置图表的源数据。请尝试此修订后的代码,让我知道它是否适合你。