简体   繁体   English

VBA代码基于Excel中另一列中的值对一列中的值求和

[英]VBA Code to sum values in one column based on the value in another column in excel

I am a VBA Newbie and a first timer on this forum - I have followed all the instructions on the "how to ask" page - I may have missed something please do advise 我是VBA新手,并且是该论坛的首次参加者-我已经按照“如何提问”页面上的所有说明进行操作-我可能错过了一些内容,请务必提出建议

I am creating an EV report for project tracking based on the following column headers (showing only a few for brevity) 我正在根据以下列标题创建用于项目跟踪的EV报告(为简洁起见,仅显示了一些)

"TaskName" "Status" "BaselineStart" "BaselineFinish" "BaselineEffort"

need to sum up the values in the BaselineEffort column in 7 day increments after checking if the value in the BaselineFinish column is less than or equal to the 7th day value 在检查BaselineFinish列中的值是否小于或等于第7天值后,需要以7天为增量对BaselineEffort列中的值求和

I believe the answer lies in using arrays, but need handholding with that concept to understand how it works 我相信答案在于使用数组,但是需要掌握该概念才能理解其工作原理

Pivot and Excel formulas dont work for me because the table is dynamic while the report is static and I need to remove user intervention in creating the report 数据透视表和Excel公式对我不起作用,因为表是动态的,而报表是静态的,我需要在创建报表时消除用户干预

Thanks in advance 提前致谢

say that the criteria column starts in A1 and the colume to be summed starts in B1 说,条件列从A1开始,要求和的求和列从B1开始

do while not isempty(range("a1").offset(x,0))
  if range("a1").offset(x,0) = SomeValue then
    sum = sum + range("a1").offset(x,0)
  end if
  x = x + 1
loop

this code will run until it has looked at each item in column A, and add the value in column B to a sum I called "sum" if the value in column A equals "SomeValue." 该代码将一直运行,直到查看了A列中的每个项目为止,并且如果A列中的值等于“ SomeValue”,则将B列中的值加到我称为“ sum”的总和中。 I doubt you can actually use the variable name "sum." 我怀疑您实际上可以使用变量名称“ sum”。

I hope that's useful. 我希望这是有用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM