简体   繁体   English

Excel VBA-循环计算大于/小于特定值的单元格值

[英]Excel VBA - Looping to count cell values larger than / smaller than specific value

I'm currently working with a large amount of contract lists. 我目前正在处理大量合同清单。 Each list is in a separate worksheet for each year's quarter and it needs to stay that way. 每个列表都在每个季度的单独工作表中,并且需要保持这种状态。 Eg: 2007_Quarter1 , 2007_Quarter2 , etc. I have 10 years of data, so 40 quarter report worksheets. 例如:2007_Quarter1, 2007_Quarter2 ,等我有10年的数据,因此40个季度报告工作表。

Now I need to code a vba routine that would do the following, in the specified order: 现在,我需要编写一个vba例程,以指定的顺序执行以下操作:

  1. In each quarter report worksheet, run through the column containing contract values; 在每个季度报告工作表中,遍历包含合同价值的列; then 然后
  2. Count all values between 0 and 10,000; 计算0到10,000之间的所有值; then 然后
  3. Count all values between 10,001 and 25,000; 计算10,001和25,000之间的所有值; and so on 等等

  4. Then, when there are no more contract values in the range (which will vary from sheet to sheet), go to the next worksheet and repeat the procedure. 然后,当该范围内没有更多的合同价值时(每个工作表之间都会有所不同),请转到下一个工作表并重复该过程。

All results should be returned in a worksheet (let's call it Worksheets("Report")) in a table where rows are for value intervals (eg 0 to $10,000) and columns are for quarters (eg 2007 Q1, 2007 Q2, etc.). 所有结果都应在表的工作表中返回(我们称其为Worksheets(“ Report”)),在表中,行用于值间隔(例如0到$ 10,000),列用于季度(例如2007 Q1、2007 Q2等)。 。

One particular aspect of my problem is that, for practical reasons, I'd prefer to set the control intervals' min and max values in a table contained in another worksheet ("Variables"), where, for example, I'd have all minimum values in range C4 down and all maximum values in range D4 down. 我的问题的一个特定方面是,出于实际原因,我宁愿在另一个工作表(“变量”)中包含的表中设置控制间隔的最小值和最大值,例如,范围C4向下的最小值,范围D4向下的所有最大值。

For each contract value, an "If" condition should look like: 对于每个合同价值,“如果”条件应类似于:

If ContractVal > Worksheets("Variables").Range("C4").Value And 
If ContractVal < Worksheets("Variables").Range("D4").Value Then ...

So far I'm having no success at coding this efficiently. 到目前为止,我还没有成功进行有效的编码。 I suspect some loop would work but I can't find a way to make it happen. 我怀疑某些循环会奏效,但我找不到实现它的方法。 A loop would do: 循环将执行以下操作:

In 2007_Quarter1 ws: For each cell from A4 down to the end, count values included between Range("C4").Value and Range("D4").Value 在2007_Quarter1中ws:对于从A4到末尾的每个单元,计数包含在Range(“ C4”)。Value和Range(“ D4”)。Value之间的值

Then for each cell from A4 down to the end, count values included between Range("C5").Value and Range("D5").Value 然后,对从A4到末尾的每个单元格,计数包含在Range(“ C5”)。Value和Range(“ D5”)。Value之间的值。

... and so on, then repeat for 2007_Quarter2, 2007_Quarter3, and so on. ...,依此类推,然后对2007_Quarter2、2007_Quarter3等重复。

I need rescue! 我需要营救! Thanks in advance! 提前致谢!

If it is ok to you to prepare the resulting matrix manually (intervals in rows and quarters in columns) you could write a macro that read the right file and count the contracts in the right range for every cell in the matrix. 如果可以手动准备生成的矩阵(行间隔和列四分之一间隔),则可以编写一个宏,该宏读取正确的文件并为矩阵中的每个单元计数正确范围内的合约。

If you want to build the matrix automatically you should tell the macro the year range. 如果要自动构建矩阵,则应告诉宏年份范围。 Then you can write a loop that read the ranges and for every range you write a loop to read every file in the right order. 然后,您可以编写一个读取范围的循环,并为每个范围编写一个循环以正确的顺序读取每个文件。

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

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