根据变量选择Array元素

时间:2017-05-16 15:25:15

标签: arrays vba multidimensional-array

我有以下多维数组类型:

Type Wedge
    C407                As Long
    C417                As Long
    C507                As Long
    C516                As Long
    C607                As Long
    C617                As Long
    C707                As Long
    C716                As Long
    C807                As Long
    C817                As Long
    C907                As Long
End Type

它继续进行另外20个左右的元素,然后我将数组定义为

Dim myWedge() As Wedge
ReDim myWedge(99, 4)

这个工作正常,并且结构合理,以便我可以将时间戳分解为整天的15分钟段。到目前为止一切顺利

我遇到的问题是我搜索的文本字符串如:

--- YTD05 C707 DC5 64:Right wedge not in OK

我使用下面的行拉出C707,再次没问题,

myWedgeFault = (Mid(myFaultDesc, 11, 4))   ' gives me C707

然后想要使用类似的东西将数组元素增加1:

myWedge(myftime, ArrayCol).C707 = myWedge(myftime, ArrayCol).C707 + 1

但是我想通过使用myWedgeFault这样的变量设置C707部分,这样我就不必硬编码

中的所有不同选项了。

类似的东西:

myWedge(myftime, ArrayCol).myWedgefault = myWedge(myftime, ArrayCol).myWedgefault + 1

这可能吗

由于

1 个答案:

答案 0 :(得分:1)

如果你想保留当前的数据结构,你可以在VBA中做很少的事情,除了写一些这样的辅助函数:

Function GetWedge(w As Wedge, key As String) As Long
  Select Case key
    Case "C407": GetWedge = w.C407
    Case "C417": GetWedge = w.C417
    Case "C507": GetWedge = w.C507
    Case "C516": GetWedge = w.C516
    Case "C607": GetWedge = w.C607
    Case "C617": GetWedge = w.C617
    Case "C707": GetWedge = w.C707
    Case "C716": GetWedge = w.C716
    Case "C807": GetWedge = w.C807
    Case "C817": GetWedge = w.C817
    Case "C907": GetWedge = w.C907
    Case Else: Err.Raise vbObjectError + 1, "GetWedge", "Unknown key: & key"
  End Select
End Function

Sub SetWedge(w As Wedge, key As String, value As Long)
  Select Case key
    Case "C407": w.C407 = value
    Case "C417": w.C417 = value
    Case "C507": w.C507 = value
    Case "C516": w.C516 = value
    Case "C607": w.C607 = value
    Case "C617": w.C617 = value
    Case "C707": w.C707 = value
    Case "C716": w.C716 = value
    Case "C807": w.C807 = value
    Case "C817": w.C817 = value
    Case "C907": w.C907 = value
    Case Else: Err.Raise vbObjectError + 1, "SetWedge", "Unknown key: & key"
  End Select
End Sub

Sub IncrementWedge(w As Wedge, key As String)
  SetWedge w, key, GetWedge(w, key) + 1
End Sub

并像这样使用它们:

IncrementWedge myWedge(myftime, ArrayCol), "C707"