【问题标题】:Dynamic Array with (1) Value - VBA Excel具有 (1) 值的动态数组 - VBA Excel
【发布时间】:2017-02-21 21:52:08
【问题描述】:

我有一个简短的问题,希望任何熟悉数组的人都能轻松回答。

我有一列,其中包含不同数量的列表,由空格分隔。我有程序将数字相加。当它碰到一个空单元格时,它应该将该数字添加到一个数组中,将数组中的空格数增加 (1) 并将总计数器设置回零,然后跳到循环的下一次迭代开始计数,直到它再次碰到一个空格。随着时间的推移,列表的数量会有所不同。我不认为会有超过 3-4 个,但如果将来有更多,我想对其进行编程。

因此,例如,单元格 C4:C10 包含的数字加起来为 200,则单元格 C11 为空。单元格 C12:C14 包含加起来为 150 的数字。我希望数组在第一个位置存储 200,在第二个位置存储 150。然后我想弹出消息框告诉我“列表 1 是 200”,列表 2 是 150”,等等......

现在,我的下标越界了,但我认为这是因为我没有声明数组大小。我希望它是动态的,但我不知道如何让一个数组只包含 (1) 个插槽。它总是类似于 arr(0 到 1)。我不只是在我正在查看的示例中看到单个值,而且由于它在 for 循环中,如果我为 lbound 执行了一个循环到 ubound,我认为它会说类似“列表 1 是 200”、“列表 2 是 ",第二个没有值,因为如果只有一个列表,则没有数据。

我可能会过度思考或以错误的方式处理它,但我以前从未真正使用过数组。我认为这对这个程序会有帮助。感谢您提供的任何帮助。

我将在下面提供我的烂摊子。我还没有清理它并很好地评论它,所以你可以问你是否有问题。我确定我做错了,我只是想让你知道我要去哪里。

Private Sub cmdTest_Click()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim intCounter As Integer
    Dim intValue As Integer
    Dim strCell As String
    Dim arr() As Long
    Dim intArr As Integer

    Set wb = Application.Workbooks("ListTotal.xlsm")
    Set ws = wb.Worksheets("Main")

    Call LastRowWithData_xlUp_1(lastColRow)

    MsgBox "Number of lists is: " & WorksheetFunction.CountIf(Range("C3:C" & lastColRow), "") + 1

    Call FindLastRow(LastRow)

    'Count pages left in each series
    intValue = 0

    'set number of values in array
    intArr = 1

    For i = 3 To LastRow

            strCell = "C" & i

        If Range(strCell) = "" Then
            arr(0) = intValue
            intValue = 0
            GoTo NextIteration
        End If

                openingParen = InStr(Range(strCell), "[")
                closingParen = InStr(Range(strCell), "]")
                enclosedvalue = Mid(Range(strCell), openingParen + 1,              closingParen - openingParen - 1)
                intValue = intValue + enclosedvalue
                MsgBox "The number of pages in this book is: " & enclosedvalue

        NextIteration:
        MsgBox "The total pages left in the series is: " & intValue

    Next

    For i = LBound(arr) To UBound(arr)
        MsgBox ("The number of pages left in Series " & j & " is:")  'arr(i))
    Next i

End Sub

【问题讨论】:

  • 听起来你需要调整数组的大小。您可以使用Redim 来实现此目的。像Redim Arr(99) 这样的东西会将一维数组的大小设置为 100 个空元素。您是否需要为每一行提供足够的空间?如果是这样,您可以使用 LastRow,尽管我看不到它是在哪里声明的。
  • 嘿瑞恩。是的,我确实看到了如何调整它的大小。我可以使用 Redim Preserve Arr。我没有保存每一行。我正在汇总每个列表并将总数保存在数组中。我的想法是我可以遍历数组并显示每个总数。因此,如果我有 3 个列表,我可以执行一个循环,在其中获取数组的 ubound 并说从 j=1 到 ubound, msgbox "Total is: " & arr(j).

标签: arrays excel vba


【解决方案1】:

我使用的一种方法是将数据存储在字典而不是数组中。

使用字典可以快速执行基于键的查找,并且您可以将键设置为几乎任何您想要唯一标识总数的键。这可能(我认为)比数组中的索引号更具象征意义和易于理解。

我设置为测试的工作表(在 Sheet1 上):

示例代码如下:

Option Explicit

Public Sub Example()
    Dim myDict As Object: Set myDict = CreateObject("Scripting.Dictionary")
    Dim i As Long

    'Add data to a dictionary
    With myDict
        'the first parameter is a unique key that identifies the record
        'the second parameter is the value
        'Remember to change the range reference
        .Add "the Range A1:A5", WorksheetFunction.Sum(Range("Sheet1!A1:A5"))
        .Add "the Range B1:B5", WorksheetFunction.Sum(Range("Sheet1!B1:B5"))
        .Add "the Range C1:C5", WorksheetFunction.Sum(Range("Sheet1!C1:C5"))
    End With

    'Iterate the dictionary and return the key and value
    For i = 0 To myDict.Count - 1
        Debug.Print "The sum is: " & myDict.items()(i) & " for Key: " & myDict.keys()(i)
    Next i
End Sub

立即窗口中的结果:

The sum is: 6 for Key: the Range A1:A5
The sum is: 5 for Key: the Range B1:B5
The sum is: 12 for Key: the Range C1:C5

【讨论】:

  • 瑞恩,我的范围是可变的。列表长度各不相同。
  • 范围也可以是变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
相关资源
最近更新 更多