使用连接范围变量和工作表名称设置源数据

问题描述 投票:0回答:1

此子过程旨在更新簇状柱形图的源数据 (SetSourceData)。图表和表格都是从我们的项目仪表板工作表中提取的。

此代码旨在在工作簿中其他两个工作表的下拉菜单中触发与更改相关的事件过程时运行。

需要注意的是,虽然这个子过程适用于硬编码的单元格引用,但我想让它更加健壮,因此我引用了命名范围,这可以在最后一个 If 语句的 Else 部分中看到。这是子过程中唯一不起作用的部分。

我想知道如何让我的 VBA 代码工作,以便它引用用于创建聚集柱形图的表中的源区域。

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
excel vba charts concatenation
1个回答
0
投票

从您的代码来看,问题似乎可能出在最后一个

Offset
语句的
Else
部分中的
If
方法。
Offset
方法需要数字参数,但您提供
fyMonths
,它是一个字符串数组。

此外,在设置图表源数据的代码的最后部分中,您在

chartRangeMonths.Address
函数中使用了
Range
两次。这也可能会导致问题。

Sub changeSourceData()

    '... (rest of your code)

    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'!" & chartRangeValues.Address)
    End If

End Sub

在此修订版中,我将

chartRangeMonths.Address
函数中的第二个
chartRangeValues.Address
替换为
Range
。这应该正确设置图表的源数据。

请尝试修改后的代码,并告诉我它是否适合您。

© www.soinside.com 2019 - 2024. All rights reserved.