excel SetSourceData与串联的范围变量和工作表名称

gdrx4gfi  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(95)

此子过程用于更新聚集柱形图的源数据(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

字符串

ruoxqz4g

ruoxqz4g1#

按期间号扩展范围,即1表示4月,12表示3月

Option Explicit

Sub changeSourceData()

    Dim cht As Chart
    Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range
    Dim fyMonths, mth As String, fpno As Long
    
    ' convert month name to period 1 to 12
    fyMonths = Array("Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar")
    mth = Range("CoverWorksheet_Current_Month_DropDown").Value
    fpno = Application.Match(mth, fyMonths, 0)
    If vbNo = MsgBox("Period is " & fpno & " (" & mth & "), continue ?", _
              vbYesNo, "Confirm") Then
        Exit Sub
    End If
    
    ' expand ranges by number of periods
    Set rng1 = Range("Profile_Of_Income_Months_archived_HEADER")
    Set rng2 = Range("Profile_Of_Income_MonthsRANGE")
    Set rng3 = rng1.Resize(fpno).Offset(1)
    Set rng4 = rng2.Resize(fpno).Offset(1)
    
    Set cht = Sheets("Project Dashboard").ChartObjects(1).Chart
    cht.SetSourceData Source:=Application.Union(rng1, rng2, rng3, rng4)

    MsgBox "Done", vbInformation

End Sub

字符串

kx7yvsdv

kx7yvsdv2#

从您的代码来看,问题似乎出在上一个If语句的Else部分中的Offset方法上。Offset方法需要数值参数,但您提供了fyMonths,这是一个字符串数组。
此外,在代码的最后一部分,在设置图表的源数据时,在Range函数中使用了两次chartRangeMonths.Address。这也可能会导致问题。

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

字符串
在这个修订版本中,我已经将Range函数中的第二个chartRangeMonths.Address替换为chartRangeValues.Address。这应该可以正确设置图表的源数据。
请尝试此修订后的代码,让我知道它是否适合你。

相关问题