VBA“Mid”函数评估为错误的值

时间:2015-02-07 13:45:50

标签: excel vba excel-vba

我在SO中找到了一个VBA代码,并对其进行了调整。这是代码:

Sub UpdateChart()
'Adjusted version of Søren Holten Hansen sub found in SO

Const strChartName As String = "Chart 6"

Dim oChart As Chart
Set oChart = ThisWorkbook.Worksheets(2).ChartObjects(strChartName).Chart
Dim colSeries As SeriesCollection
Set colSeries = oChart.SeriesCollection

Dim i As Long, r As Long, n As Long 'counters
Dim p1 As Long, p2 As Long, p3 As Long '~positions of commas in Formula
Dim rngY As Range 'Y values in Formula (3rd SERIES formula argument)
Dim ax As Range 'Axis (X values) in Formula (2nd SERIES formula argument)
Dim rngLast As Range
Dim strYAddress As String

    'Cycles through each Series
    For n = 1 To colSeries.Count Step 1
        r = 0

        'Finds the current range of the series (3rd SERIES argument) and
        '   axis range (2nd argument) - ~positions of commas in Formula
        For i = 1 To Len(colSeries(n).Formula) Step 1
            If Mid(colSeries(n).Formula, i, 1) = "," Then
                r = r + 1
                If r = 1 Then p1 = i + 1
                If r = 2 Then p2 = i
                If r = 3 Then p3 = i
            End If
        Next 'i

        'Updates axis (2nd argument in Formula (SERIES formula))
        Set ax = Range(Mid(colSeries(n).Formula, p1, p2 - p1))
        Set rngLast = ax.End(xlDown)
        Set ax = Range(ax, rngLast)
        colSeries(n).XValues = ax

        'Updates Y values (3rd argument in Formula (SERIES formula))
        strYAddress = Mid(colSeries(n).Formula, p2 + 1, p3 - p2 - 1)
        Set rngY = ThisWorkbook.Worksheets(2).Range(strYAddress)     '<<<<<
        Set rngY = Range(rngY, Cells(rngLast.Row, rngY.Column))
        colSeries(n).Values = rngY

        Set rngLast = Nothing
        Set ax = Nothing
        Set rngY = Nothing
    Next 'n

Set rngLast = Nothing
Set ax = Nothing
Set rngY = Nothing
Set colSeries = Nothing
Set oChart = Nothing

End Sub

链接到原始Sub:by Søren Holten Hansen

我非常了解VBA,但我不知道如何解决这个问题,为什么会发生这种情况。我已经标记了导致错误的行(“&lt;&lt;&lt;&lt;&lt;&lt;&lt;”)。

起初我只有这一行:Set rngY = Range(Mid(colSeries(n).Formula, p2 + 1, p3 - p2 - 1))。然后我遇到了这个错误:“运行时错误'1004':对象'_Global'的方法'范围'失败了”。我添加了“限定”引用,并将Mid函数的结果放到另一个变量(strYAddress)。但我确信问题不在这里。

现在,当Sub停在标记的行上并调试sub时,我发现strYAddress的值",2"是错的!如果我再次返回并再次运行strYAddress = Mid(colSeries(n).Formula, p2 + 1, p3 - p2 - 1)行,则strYAddress的值变为"InvIst!$E$71:$E$88",这是正确的(!),我可以继续!

重点在哪里?为什么Mid从第一次尝试中没有评估到正确的值?

编辑:

我仍然不知道Mid函数有什么问题。似乎colSeries(n).XValues = ax阻止Mid评估为正确的值,因为我注意到如果此行被注释掉,Mid的结果是正确的...... < /强>

但是,我决定改用Split。一切都像魅力一样,代码看起来更好。

Sub UpdateChart()

Const strChartName As String = "Chart 6"

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(2)
Dim colSeries As SeriesCollection
Set colSeries = ws.ChartObjects(strChartName).Chart.SeriesCollection
Dim n As Long 'counter
Dim rngY As Range 'Y values in Formula (3rd SERIES formula argument)
Dim ax As Range 'Axis (X values) in Formula (2nd SERIES formula argument)
Dim strA() As String
Dim rngLast As Range

    'Cycles through each Series
    For n = 1 To colSeries.Count Step 1

        'Separates 2 most significant arguments - axis and Y values
        strA = Split(colSeries(n).Formula, ",")

        'Updates axis (2nd argument in Formula (SERIES formula))
        Set ax = ws.Range(strA(1))
        Set rngLast = ax.Offset(-1).End(xlDown)
        Set ax = Range(ax, rngLast)
        colSeries(n).XValues = ax

        'Updates Y values (3rd argument in Formula (SERIES formula))
        Set rngY = ws.Range(strA(2))
        Set rngY = Range(rngY, Cells(rngLast.Row, rngY.Column))
        colSeries(n).Values = rngY

    Next 'n

Set rngLast = Nothing
Set ax = Nothing
Set rngY = Nothing
Set colSeries = Nothing
Set ws = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

Range对象中,您无法引用工作表&#34; InvIst!&#34;和绝对单元格引用&#34; $&#34;被忽略了。范围必须是A1或(Cell 1,Cell 2)格式。

对于绝对引用,您可以使用Range.Address属性。

在您的情况下,strYAddress必须返回"E71:E88" ThisWorkbook.Worksheets(2)中引用工作表InvIst的位置。