[vba]excel中求选中数据和为给定数所有的组合

摘要:
我昨天下午开始学习的vba非常累,肯定有bug。要调试的vba程序如下:1DimaSumAsInteger2DimtSumAsInteger3Dimbridge(30)AsInteger4ImarrMaxAsInteger5Dimar6Dimlocation(30,AsInteger78FunctionTest()9DimarWmaxAsInteger10DimRngAsRange11Dimbe

昨天下午开始学习的vba,累死了,肯定有bug,待调试

vba程序如下:

 1 Dim aSum As Integer
 2 Dim tSum As Integer
 3 Dim judge(30) As Integer
 4 Dim arrMax As Integer
 5 Dim arr
 6 Dim location(30) As Integer
 7 
 8 Function Test()
 9     Dim arrWmax As Integer
10     Dim Rng As Range
11     Dim beginRow As Integer
12     Dim beginLine As Integer
13     
14     Set Rng = Application.InputBox(prompt:="Please Select....", Type:=8)
15     rr = Rng.Address
16     beginRow = Rng.Column
17     beginLine = Rng.Row
18     
19     arr = Range(rr)
20     aSum = 0
21     arrMax = UBound(arr)
22     arrWmax = UBound(arr, 2)
23 
24     For loca = 1 To arrMax
25         location(loca) = beginLine
26         beginLine = beginLine + 1
27     Next
28     
29     For col = 2 To arrWmax  'modify
30         tSum = arr(1, col)
31         Call subTest(1, beginRow)
32     Next
33 
34 End Function
35 
36 Function subTest(n As Integer, beginRow As Integer)
37     If aSum > tSum Then
38         Exit Function
39     End If
40 
41     Dim i As Integer
42     Dim j As Integer
43     If aSum = tSum Then
44         For i = 1 To n
45             If judge(i) = 1 Then
46                 Sheets(1).Cells(location(i), beginRow).Interior.Color = vbRed
47             End If
48         Next
49 
50         Exit Function
51     End If
52 
53     If n = arrMax Then
54         Exit Function
55     End If
56 
57     For j = n To arrMax
58         If judge(j) = 0 Then
59             judge(j) = 1
60             aSum = aSum + arr(j, 1)
61             Call subTest(j, beginRow)
62 
63             judge(j) = 0
64             aSum = aSum - arr(j, 1)
65             If j < arrMax Then
66                 While arr(j, 1) = arr(j + 1, 1)
67                       j = j + 1
68                 Wend
69             End If
70         End If
71     Next
72 
73 End Function

免责声明:文章转载自《[vba]excel中求选中数据和为给定数所有的组合》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇html模板导出pdf文件怎么实现web端上传超大文件,下载断点续传下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

PHP mysqli获取数据表以及表结构

<?php$mysqli = new mysqli('localhost','root','','le');$result = $mysqli->query('SHOW TABLES');//执行查询语句//输出此数据库中表结构$tables = array();while($arr = $result->fetch_assoc()){...

VBA工作表排序转载

工作表内单元格数据的排序,使用Range.Sort 命令。但是后面的参数较多,新人一般采用录制宏时出来一大堆,也不知道该如何编辑。因此,我开这一个帖子,给新人普及一下:Range("待排序数据区域").Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3,Header, OrderCustom, Match...

PyTorch笔记之 scatter() 函数

scatter() 和 scatter_() 的作用是一样的,只不过 scatter() 不会直接修改原来的 Tensor,而 scatter_() 会 PyTorch 中,一般函数加下划线代表直接在原来的 Tensor 上修改 scatter(dim, index, src) 的参数有 3 个 dim:沿着哪个维度进行索引 index:用来 sca...

【转】IIS上的反向代理

http://blog.csdn.net/yuanguozhengjust/article/details/23576033 一直说在IIS上做反向代理,由于沉迷在nginx一行指令完事的美好情景当中,所以IIS懒得折腾,今天终于下定决心在IIS上折腾一下反向代理,经过一晚上的捣鼓,终于搞定!搞定以后发现其实太简单了有木有!!(网上教程很晕!)首先说一下,...

vba Excel连接数据库

PostgreSql: 第一步 在网上下载postres的驱动程序,之后安装,下载地址:https://www.devart.com/odbc/postgresql/download.html 第二步 创建ODBC数据源 点击“开始-》控制面板-》管理工具-》数据源(ODBC)-》用户DSN-》添加” 安装上图配置好之后写入VBA代码并导入包文件...

bpc 数据处理

UJKT程序:UJO_SQE_TESTBADI:UJ_CUSTOM_LOGIC*XDIM_MEMBERSET Z_ACCOUNT = PL04 *START_BADI PL04_STEP1 WRITE = ON QUERY = ON *END_BADI *定义模型 types:begin of ty_planning, m...