VBA细胞选择" application.union" with cells()

时间:2015-06-19 09:24:11

标签: vba charts selection

我在选择子中两个不同行的部分时遇到问题。 我的sub必须创建一个图表,其中这些行为DataSource,第二行的编号是过程的参数。 CompteLigneDebutCompteColonnefin是我之前定义的函数,它们返回一个整数。 以下是导致问题的部分,导致错误的具体行是set x...

Sub test(ligne As Integer)
    Dim graphe As Chart
    Dim x As Range
    Set graphe = Charts.Add
    graphe.ChartType = xlColumnClustered
    Set x = Application.Union(Range(Cells(ligne, 2), Cells(ligne, CompteColonneFin)), Range(Cells(CompteLigneDebut, 2), Cells(CompteLigneDebut, CompteColonneFin - 1)))
    graphe.SetSourceData (x)

感谢您的帮助/建议

1 个答案:

答案 0 :(得分:0)

Union看起来不错,下面的代码对我来说很好。我认为你的一个功能就是问题。

Sub RunTheTest()

    test 3

End Sub
Sub test(ligne As Integer)
    Dim x As Range

    Set x = Application.Union(Range(Cells(ligne, 2), Cells(ligne, CompteColonneFin)), Range(Cells(CompteLigneDebut, 2), Cells(CompteLigneDebut, CompteColonneFin - 1)))

End Sub

Private Function CompteColonneFin() As Integer
    CompteColonneFin = 2
End Function

Private Function CompteLigneDebut() As Integer
    CompteLigneDebut = 2
End Function

尝试将Application.Union行拆分为较小的位。然后检查函数返回的值。

Sub test(ligne As Integer)
    Dim x As Range
    Dim colonneFin As Integer
    Dim ligneDebut As Integer

    colonneFin = CompteColonneFin()
    ligneDebut = CompteLigneDebut()

    Set x = Range(Cells(ligne, 2), Cells(ligne, colonneFin))
    Set x = Application.Union(x, Range(Cells(ligneDebut, 2), Cells(ligneDebut, colonneFin - 1)))

End Sub