简体   繁体   English

在Excel中使用VBA计算某些单元格的数量

[英]calculate count of certain cells using vba in excel

I have build a quiz and displaying score using some checks and output cells have "1" if ans correct and "0" if wrong. 我已经建立了一个测验,并使用一些检查来显示分数,如果正确,则输出单元格为“ 1”,如果错误则为“ 0”。

to calculate score I use the function 计算分数我用的功能

=COUNTIF(S7:S158,1)

Problem is the score gets updated as soon user starts writing ans. 问题是,分数会随着用户开始写ans而更新。

I want to have a Macro SUBMIT(using form control) which when clicked gives the score. 我想要一个宏SUBMIT(使用表单控件),单击该宏即可获得分数。

what should i write in Sub SUBMIT() so as to execute the above function 我应该在Sub SUBMIT()中编写什么以执行上述功能

You can write fomula: 您可以编写公式:

=IF(COUNTA(S7:S158)=x;COUNTIF(S7:S158,1);"")

where x is a number of questions (for example 100). 其中x是许多问题(例如100)。 So, function COUNTA counts number of non empty cells in the range, and if the user ansered on all questions, condition COUNTA(S7:S158)=x becames true and the COUNTIF(S7:S158,1) calculated, otherwise an empty string is displayed 因此,函数COUNTA计算该范围内的非空单元格的数量,如果用户回答了所有问题,则条件COUNTA(S7:S158)=x成立,并计算COUNTIF(S7:S158,1) ,否则为空字符串被陈列

You can set the following code in Workbook_Open 您可以在Workbook_Open设置以下代码

Sheet1.EnableCalculation = False

In Sub Submit() Sub Submit()

Sheet1.EnableCalculation = True
Sheet1.Calculate

For more, see this 如需更多信息,请参见

I'm a developer and I resort to macro only when all options are exhausted. 我是一名开发人员,只有在所有选项都用尽时才诉诸宏。 This is mainly for the benefit of people that might want use/reuse it for other purposes. 这主要是出于可能希望将其用于其他目的的人的利益。 But that's just my preference. 但这只是我的偏爱。

You can have a cell with a label of Show Score and accepts Yes/No and put an IF around your =COUNTIF(S7:S158,1) . 您可以使用带有Show Score标签的单元格并接受Yes / No ,然后在=COUNTIF(S7:S158,1)周围放置一个IF

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

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