动态更新因变量

时间:2014-05-27 17:32:34

标签: javascript excel vba excel-vba

刚刚在我的cs知识中发现了一个漏洞......这段代码是用VBA编写的,但我真的很想知道如何在javascript中做到这一点!

基本上我处于一整套变量依赖于一个变量的情况 - 如下所示:

Sub test()
    Dim start As Integer
    Dim var1 As Integer
    Dim var2 As Integer
    Dim var3 As Integer

    var1 = start + 1
    var2 = start + 2
    var3 = start + 3
End Sub

我的问题是我希望var1,var2和var3的值根据start的值动态更新 ,例如

Sub test()
    Dim start As Integer
    Dim var1 As Integer
    Dim var2 As Integer
    Dim var3 As Integer

    start = 0
    var1 = start + 1
    var2 = start + 2
    var3 = start + 3

    ' would like to have var1 = 1, var2 = 2, var3 = 3
    MsgBox "start = " & start & vbNewLine & _
        "var1 = " & var1 & vbNewLine & _
        "var2 = " & var2 & vbNewLine & _
        "var3 = " & var3

    start = 5

    ' would now like to have var1 = 6, var2 = 7, var3 = 8
    MsgBox "start = " & start & vbNewLine & _
        "var1 = " & var1 & vbNewLine & _
        "var2 = " & var2 & vbNewLine & _
        "var3 = " & var3
End Sub

但显然这不起作用,我得到相同的值

var1 = 1, var2 = 2, var3 = 3

两次。有没有办法让这个工作在VBA? 是否有这样的名称,所以我可以更好地谷歌吗?像“动态因变量”之类的东西?非常感谢!

2 个答案:

答案 0 :(得分:2)

我猜你正在寻找像Excel单元格公式和级联更新的方式。在VBA中没有类似编码的内置机制。有些编程语言使用称为Functional Programming的范例,它通过将函数链接在一起并以惰性方式进行评估,以类似的方式运行,但VBA更像是一种(弱)面向对象的命令式风格。

解决此类问题的一种方法通常是创建一个类来封装所有计算并使用成员变量作为首先设置的计算的基本元素,如start,以及衍生数字,例如var1var2var3

这是一个例子。创建一个名为clsRectangle的类并复制以下内容:

Option Compare Database
Option Explicit

Public length As Integer
Public width As Integer

Public Property Get diagonal() As Double
    diagonal = VBA.Sqr((length ^ 2) + (width ^ 2))
End Property

Public Property Get area() As Integer
    area = length * width
End Property

Public Property Get perimeter() As Integer
    perimeter = 2 * (length + width)
End Property

接下来创建一个名为mdlMain的模块并添加以下内容:

Public Sub Main()
    Dim rect As clsRectangle
    Set rect = New clsRectangle

    With rect
        .length = 3
        .width = 5

        MsgBox "perimeter: " & .perimeter & vbCrLf & _
               "diagonal: " & .diagonal & vbCrLf & _
               "area: " & .area & vbCrLf

        'After changing the underlying numbers (like *start* in your example)
        'area, perimeter and diagonal all return new values
        .length = 2
        .width = 7

        MsgBox "perimeter: " & .perimeter & vbCrLf & _
               "diagonal: " & .diagonal & vbCrLf & _
               "area: " & .area & vbCrLf
    End With
End Sub

在编译时构建一个类来处理公式计算是一种很好的做法,因为它以一种非常易于维护的方式公开你的数学和逻辑,并从语言的语法和类型检查中获益。但是,它确实缺乏运行时系统的一些灵活性。如果你想尝试类似的东西,我可以给你一些指示,但这将是一个非常重要的提升。我过去做过类似的事情,不得不实施[拓扑排序],根据先决条件图找出执行计算更新的顺序。


我无法直接与Javascript对话,因为我没有太多使用它的经验。但是,Javascript是一种比VBA更具动态性的语言,完全有可能已经存在的库可以满足您的需求。

答案 1 :(得分:0)

据我所知,这在VBA中是不可能的。您可以在模块级别(程序之外)声明变量,并通过调用函数更新它们,如下所述。它并不像你想象的那么简单,但它比每次“开始”更改时手动更新变量要好。

Dim start As Integer
Dim var1 As Integer
Dim var2 As Integer
Dim var3 As Integer

Sub test()

    start = 0
    var1 = start + 1
    var2 = start + 2
    var3 = start + 3

    ' would like to have var1 = 1, var2 = 2, var3 = 3
    MsgBox "start = " & start & vbNewLine & _
        "var1 = " & var1 & vbNewLine & _
        "var2 = " & var2 & vbNewLine & _
        "var3 = " & var3

    start = ChangeStart(5)

    ' would now like to have var1 = 6, var2 = 7, var3 = 8
    MsgBox "start = " & start & vbNewLine & _
        "var1 = " & var1 & vbNewLine & _
        "var2 = " & var2 & vbNewLine & _
        "var3 = " & var3
End Sub

Public Function ChangeStart(StartValue As Long)

start = StartValue
var1 = start + 1
var2 = start + 2
var3 = start + 3

End Function