excel - VBA 使用递归而不是循环

标签 excel algorithm vba recursion sum

我想请您帮忙解决我在构建算法时遇到的问题。

  • 可用数据

我们有一个包含 10 行和 30 列的数据表。

每一行对应于链接到特定参数的变量(例如苹果的重量,梨的重量,橘子的重量......参数10)

每一列对应于变量可以取的值。

这就是我们如何为每个变量/参数设置 30 个可能的值。

  • 算法的目标 给出这些参数所有可能组合的总和(Apples+Pears+ Oranges+...+Paramter10),正常应该有 30^10 种可能的组合

  • 我的算法版本的描述

    1. 创建一个包含 10 个索引的数组。每个索引对应于变量/参数。每个索引由 30 个可能值中的一个填充。

    2. 创建一个循环遍历同一行中的列。表格中单元格的每个值都放在对应的索引中(第1行,对于索引1)

    3. 每个更改对应于同一行但不同列中的下一个可用值(在右侧)。当该列终止时,算法开始对下一行执行相同的操作。

    4. 数组 varlues 中的每个更改都必须给出一个新的总和,并将其复制到表格某处的新单元格中。

子算法和()

Dim rw As Long, column1 As Long
Dim Sum1(9) As Long 'The array with 10 index 'First difficulty is to find a way how to synchornize the sum with every change in array Sum1.
    Do While i <= 8 'My version is to place it in first but not sure it is right
            For rw = 1 To 9
                For column1 = 1 To 30
                    If Not IsEmpty(Worksheets("Sheet1").Cells(rw, column1)) Then 'verify that the cell is not empty
                        Sum1(i) = Worksheets("Sheet1").Cells(rw, column1).Value
                        Worksheets("Sheet1").Cells(rw+30, column1+30).Value = Application.WorksheetFunction.Sum(Sum1)

                    Else 'normally if it is empty it should move to the next value but I didn't find the appropriate way to do it, this why I thought about this compromise
                        Sum1(i) = 0
                        Worksheets("Sheet1").Cells(rw+30, column1+30).Value = Application.WorksheetFunction.Sum(Sum1)
                    End If
                Next column1
            Next rw
        i = i + 1
    Loop

结束子

此代码未给出适当的结果。它没有给出数组中每个更改的总和,也没有按照必须的方式对数组进行更改。

我已经看到了递归的可能解决方案,但没有找到正确的方法。

非常欢迎一些帮助!

提前致谢!

最佳答案

试试这个。它是递归的(尽管它也使用循环)。

  • 使用顶部的常量来调整您的数据。
  • 尝试使用几行和几列。
  • 当前常量假定您的数据从单元格 A1 开始,并且有 3 个“变量”和 2 个“值”。
  • 它会将结果打印到即时窗口,但您当然可以调整它以存储在数组中或打印到另一张纸。

' number of "variables"
Const ROWS As Integer = 3
' number of "values"
Const COLS As Integer = 2
' upper left cell of table
Const CELL_UPPER_LEFT As String = "A1"

' Recursive method to sum the values of all the combinations of all the rows.
' Method will print the result in the immediate window for now.
' @param    row_number  Integer     Number of rows of data
' @param    sum               Double    Running sum of previous rows

Private Sub recursiveSum(ByVal row_number As Integer, ByVal sum As Double)

    ' if we've reached the bottom, then print the result and go back up
    If row_number = ROWS Then
        Debug.Print sum
        Exit Sub
    End If

    ' loop over the number of columns
    Dim col_number As Integer
    For col_number = 0 To COLS - 1

        ' make a recursive call, increasing the sum by the current row and increasing the row number by 1
        recursiveSum row_number + 1, Range(CELL_UPPER_LEFT).Offset(row_number, col_number).Value + sum
        ' when we return from the recursive call we will be here - ready to start the next time through the loop

    Next

End Sub

' Wrapper function for the recursive method.
Public Sub recursiveSumWrapper()

    ' make the initial recursive call
    recursiveSum 0, 0

End Sub

关于excel - VBA 使用递归而不是循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24253138/

相关文章:

excel - 如何将十进制时间转换为 hh :mm time format in excel

algorithm - 在排名(排序)列表上执行前缀搜索的有效方法?

vba - VBA 是否保留过去代码更改的缓存?

vba - 尽管延迟可变,但每步的平均时间是恒定的

excel - 如何在我的 excel 公式中自动更改部分数据

vba - 使用 Excel VBA 将单个工作簿拆分为包含多个工作表的多个工作簿

从下拉列表更改工作表时 VBA 崩溃

python - 最长递增子序列,算法工作错误,不知道为什么

从两个集合中匹配对的算法

Excel函数返回月份之间的差异?