如何调用一个包含一维值和多维值的数组

huangapple go评论64阅读模式
英文:

How to call an array that contains both one dimensional values and multi dimensional values

问题

我想要更改 statikk_start(2),statikk_start(3) 和 statikk_start(4) 的值。当 statikk_start 的值从单一值(整数)更改为一系列值时,我不知道如何在 Rs() 计算中调用它们。我希望 Rs 循环遍历 statikk_start 的每个值。我尝试使用 Select Case,但它给我报了 3、4 和 5 的范围错误。

    Dim p As Range
    Set p = Sheets("Kjøreplan").Range("B4:K27")

    Dim p_max As Range
    Set p_max = Sheets("Kjøreplan").Range("S36:Y36")

    Dim statikk_start As Variant
    statikk_start = Array(6, 10, 12, 12, 12, 6, 6)

    For i = 0 To UBound(statikk_start)
        Select Case i
            Case 2
                statikk_start(i) = Application.WorksheetFunction.Transpose(Sheets("Kjøreplan").Range("E4:E27").Value)
            Case 3
                statikk_start(i) = Application.WorksheetFunction.Transpose(Sheets("Kjøreplan").Range("G4:G27").Value)
            Case 4
                statikk_start(i) = Application.WorksheetFunction.Transpose(Sheets("Kjøreplan").Range("I4:I27").Value)
        End Select
    Next i

    Dim Rs(1 To 7) As Variant
    
    For colIndex = 1 To 7
        Select Case colIndex
            Case 1, 2, 6, 7
                Rs(colIndex) = 2 * p_max(1, colIndex) / statikk_start(colIndex - 1)
            Case 3
            For rowIndex = 1 To 24
                Rs(colIndex) = 2 * p_max(1, colIndex) / statikk_start(colIndex - 1)(2, rowIndex)
            Next rowIndex
            Case 4
            For rowIndex = 1 To 24
                Rs(colIndex) = 2 * p_max(1, colIndex) / statikk_start(colIndex - 1)(3, rowIndex)
            Next rowIndex
            Case 5
            For rowIndex = 1 To 24
                Rs(colIndex) = 2 * p_max(1, colIndex) / statikk_start(colIndex - 1)(4, rowIndex)
            Next rowIndex
        End Select
    Next colIndex
英文:

I want to change the values for statikk_start(2), statikk_start(3) and statikk_start(4). When the values of statikk_start changes from just singular values (integers) to a range of values, i don't know how i call them to the Rs() calculation. I want Rs to loop through each value of statikk_start. I've tried using the Select Case, but it gives me out of range error for case 3, 4 and 5.

    Dim p As Range
    Set p = Sheets("Kjøreplan").Range("B4:K27")

    Dim p_max As Range
    Set p_max = Sheets("Kjøreplan").Range("S36:Y36")

    Dim statikk_start As Variant
    statikk_start = Array(6, 10, 12, 12, 12, 6, 6)

    For i = 0 To UBound(statikk_start)
        Select Case i
            Case 2
                statikk_start(i) = Application.WorksheetFunction.Transpose(Sheets("Kjøreplan").Range("E4:E27").Value)
            Case 3
                statikk_start(i) = Application.WorksheetFunction.Transpose(Sheets("Kjøreplan").Range("G4:G27").Value)
            Case 4
                statikk_start(i) = Application.WorksheetFunction.Transpose(Sheets("Kjøreplan").Range("I4:I27").Value)
        End Select
    Next i

    Dim Rs(1 To 7) As Variant
    
    For colIndex = 1 To 7
        Select Case colIndex
            Case 1, 2, 6, 7
                Rs(colIndex) = 2 * p_max(1, colIndex) / statikk_start(colIndex - 1)
            Case 3
            For rowIndex = 1 To 24
                Rs(colIndex) = 2 * p_max(1, colIndex) / statikk_start(colIndex - 1)(2, rowIndex)
            Next rowIndex
            Case 4
            For rowIndex = 1 To 24
                Rs(colIndex) = 2 * p_max(1, colIndex) / statikk_start(colIndex - 1)(3, rowIndex)
            Next rowIndex
            Case 5
            For rowIndex = 1 To 24
                Rs(colIndex) = 2 * p_max(1, colIndex) / statikk_start(colIndex - 1)(4, rowIndex)
            Next rowIndex
        End Select
    Next colIndex

答案1

得分: 1

我认为你可能更喜欢这样的方式?
假设你想要在将转换后的数组分配给 Rs 之前,转换存储在 statikk_start 中的每个值...

Sub test()
    Dim p As Range, p_max As Range, ws As Worksheet
    Dim statikk_start As Variant, pVal
    Dim Rs(1 To 7) As Variant
    
    Set ws = ThisWorkbook.Sheets("Kjøreplan")
    Set p = ws.Range("B4:K27")         'not used?
    Set p_max = ws.Range("S36:Y36")

    statikk_start = Array(6, 10, 12, 12, 12, 6, 6)

    'no need for loop or Select Case
    statikk_start(2) = Application.Transpose(ws.Range("E4:E27").Value)
    statikk_start(3) = Application.Transpose(ws.Range("G4:G27").Value)
    statikk_start(4) = Application.Transpose(ws.Range("I4:I27").Value)
   
    For colIndex = 1 To p_max.Columns.Count
        pVal = p_max(1, colIndex)
        v = statikk_start(colIndex - 1)
        If IsArray(v) Then   'is the value an array?
            'transform the array before assigning it to Rs
            For RowIndex = LBound(v) To UBound(v)
                v(RowIndex) = 2 * pVal / v(RowIndex)
            Next RowIndex
            Rs(colIndex) = v
        Else
            Rs(colIndex) = 2 * pVal / v
        End If
    Next colIndex
End Sub
英文:

I think you might be looking for something more like this?
Assume you want to transform each value in any arrays stored in statikk_start, before assigning the transformed array to Rs...

Sub test()
    Dim p As Range, p_max As Range, ws As Worksheet
    Dim statikk_start As Variant, pVal
    Dim Rs(1 To 7) As Variant
    
    Set ws = ThisWorkbook.Sheets("Kjøreplan")
    Set p = ws.Range("B4:K27")         'not used?
    Set p_max = ws.Range("S36:Y36")

    statikk_start = Array(6, 10, 12, 12, 12, 6, 6)

    'no need for loop or Select Case
    statikk_start(2) = Application.Transpose(ws.Range("E4:E27").Value)
    statikk_start(3) = Application.Transpose(ws.Range("G4:G27").Value)
    statikk_start(4) = Application.Transpose(ws.Range("I4:I27").Value)
   
    For colIndex = 1 To p_max.Columns.Count
        pVal = p_max(1, colIndex)
        v = statikk_start(colIndex - 1)
        If IsArray(v) Then   'is the value an array?
            'transform the array before assigning it to Rs
            For RowIndex = LBound(v) To UBound(v)
                v(RowIndex) = 2 * pVal / v(RowIndex)
            Next RowIndex
            Rs(colIndex) = v
        Else
            Rs(colIndex) = 2 * pVal / v
        End If
    Next colIndex
End Sub

huangapple
  • 本文由 发表于 2023年6月16日 14:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定