VBA函数不存储变量

时间:2017-06-21 19:18:22

标签: excel vba excel-vba

我已经包含了我的功能代码。我大部分时间都是从网上找到的东西中删除的,因为我非常喜欢编码。我试图采用图表的趋势线并将其用于数学计算。当我单步执行此代码时,它运行良好。但是,当我从另一个sub调用该函数时,它会给我一个错误。错误9:下标超出范围。当我调试时,它向我显示a = spl(0)行。真正的问题是变量“s”仍然是空的。为什么呢?

我尝试将图表的创建添加到函数中以避免“Active Sheet”出错。我也尝试将此代码粘贴到我的子代码中,而不是调用单独的函数。依然没有。当我调试并突出显示t.DataLabel.Text时,它会显示正确的值,但由于某种原因,s不保存该值。在“局部”窗口中,t具有值,但s为空(“”)。

Function TrendLineLog() As Double
Dim ch As Chart
    Dim t As Trendline
    Dim s As String
    Dim Value As Double

    ' Get the trend line object
    Set ch = ActiveSheet.ChartObjects(1).Chart
    Set t = ch.SeriesCollection(1).Trendlines(1)

    ' make sure equation is displayed
    t.DisplayRSquared = False
    t.DisplayEquation = True

    ' set number format to ensure accuracy
    t.DataLabel.NumberFormat = "0.000000E+00"

    ' get the equation
    s = t.DataLabel.Text '<--------- HERE


    ' massage the equation string into form that will evaluate

    s = Replace(s, "y = ", "")
    s = Replace(s, "ln", " *LOG")
    s = Replace(s, " +", "")
    s = Replace(s, " - ", " -")
    spl = Split(s, " ")
    a = spl(0) '<----------- HERE
    b = spl(1)
    c = spl(2)
    y = 0.5

..... Math stuff

End Function

以下是调用函数的代码部分:

For rowx = 41 To 46 Step 1
Cells(rowx, 4).Select
va = Cells(rowx, 4).Value
vb = Cells(rowx, 5).Value
vc = Cells(rowx, 6).Value
vd = Cells(rowx, 7).Value
locb = ActiveCell.Address
Cells(rowx, 7).Select
loce = ActiveCell.Address
rang = locb & ":" & loce
If va < 0.8 Then    
If va < vb And vb < vc And vc < vd Then
    Range(rang).Select
    ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
    ActiveChart.SetSourceData Source:=Range(rang)
    ActiveChart.FullSeriesCollection(1).Trendlines.Add
    ActiveChart.FullSeriesCollection(1).Trendlines(1).Select
    Selection.DisplayEquation = True
    Selection.Type = xlLinear
    ans = TrendLineLin()
    Sheets("Results").Activate
    Cells(rowx - 39, 3).Value = ans
    Sheets(nam).Activate


ElseIf va < vb And vb < vc And vc > vd Then
    Range(rang).Select
    ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
    ActiveChart.SetSourceData Source:=Range(rang)
    ActiveChart.FullSeriesCollection(1).Trendlines.Add
    ActiveChart.FullSeriesCollection(1).Trendlines(1).Select
    Selection.DisplayEquation = True
    Selection.Type = xlLogarithmic
    ans = TrendLineLog()
    Sheets("Results").Activate
    Cells(rowx - 39, 3).Value = ans
    Sheets(nam).Activate
ElseIf va < vb And vb < vc And vc > vd Then
    Range(rang).Select
    ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
    ActiveChart.SetSourceData Source:=Range(rang)
    ActiveChart.FullSeriesCollection(1).Trendlines.Add
    ActiveChart.FullSeriesCollection(1).Trendlines(1).Select
    Selection.DisplayEquation = True
    Selection.Type = xlLogarithmic
    Windows(nam1).Activate
    ans = TrendLineLog(rang)
    Windows(nam1).Activate
    Sheets("Results").Activate
    Cells(rowx - 39, 3).Value = ans
    Sheets(nam).Activate

我有指数,对数和线性趋势线的函数。我的测试用例是一个日志,这是发布的功能。代码几乎相同。

0 个答案:

没有答案