带参数 CountIfS 的 Excel 宏

发布于 2024-11-03 03:09:25 字数 539 浏览 4 评论 0 原文

我正在编写一个程序,用于搜索包含奥林匹克拳击数据的电子表格。用户使用组合框选择国家/地区和体重级别,然后点击 Go!按钮,程序调用 Excel 电子表格中的宏,如下所示:

Sub paramedals(class As String, country As String)

  MsgBox " Counting by weight class " & class & " and country " & country
  Range("I73").Select
    ActiveCell.FormulaR1C1 = _
    "=COUNTIFS(C[-7], class,C[-6],country,C[-4], ""Gold"")"

End Sub

我输入了 msgbox 命令以确保它正确读取参数 - 确实如此。问题在于,它不是将公式的参数设置为给定的字符串,而是字面上将公式设置为 COUNTIFS(B:B、类等)。

就像我之前说的,我知道正确的字符串被发送到宏,因为 msgbox,它显示正确。可以不使用 COUNTIFS 参数吗?

I am writing a program which searches a spreadsheet full of Olympic Boxing data. Using a combobox, the user selects a country, and a weight class, and upon hitting the Go! button, the program calls a macro in the Excel Spreadsheet which looks like this:

Sub paramedals(class As String, country As String)

  MsgBox " Counting by weight class " & class & " and country " & country
  Range("I73").Select
    ActiveCell.FormulaR1C1 = _
    "=COUNTIFS(C[-7], class,C[-6],country,C[-4], ""Gold"")"

End Sub

I put in the msgbox command to make sure it was reading the parameters correctly - and it is. The problem is that, rather than setting the formula's parameters to the given strings, it is literally setting the formula to COUNTIFS(B:B, class, etc. .).

Like I said before, I know for a fact that the correct strings are being sent to the macro, because of the msgbox, it displays correctly. Can you just not use parameters for COUNTIFS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找个人就嫁了吧 2024-11-10 03:09:25

您需要将公式的字符串部分与变量分开

Range("I73").FormulaR1C1 = _
"=COUNTIFS(C[-7]," & class & ",C[-6]," & country & ",C[-4], ""Gold"")"

编辑:

这两种方法都有效,一个是 R1C1,另一个是标准公式

Range("I73").FormulaR1C1 = _
"=COUNTIFS(C[-7],""=" & class & """,C[-6],""=" & country & """,C[-4],""=Gold"")"

Range("I73").Formula =  _ 
"=COUNTIFS(B:B,""=" & class & """,C:C,""=" & country & """,E:E,""=Gold"")"

一个好的技巧是使用 debug.print 构建语句或 msgbox 首先,然后你可以看到进入单元格时它的样子。

You need to separate out the string part of the formula from the variables

Range("I73").FormulaR1C1 = _
"=COUNTIFS(C[-7]," & class & ",C[-6]," & country & ",C[-4], ""Gold"")"

Edit:

Both of these work, one is R1C1 and the other is a standard formula

Range("I73").FormulaR1C1 = _
"=COUNTIFS(C[-7],""=" & class & """,C[-6],""=" & country & """,C[-4],""=Gold"")"

Range("I73").Formula =  _ 
"=COUNTIFS(B:B,""=" & class & """,C:C,""=" & country & """,E:E,""=Gold"")"

A good tip is to build the statement using debug.print or msgbox first and then you can see how it looks when going into a cell.

命硬 2024-11-10 03:09:25

或者,您可以替换

  Range("I73").Select
    ActiveCell.FormulaR1C1 = _
    "=COUNTIFS(C[-7], class,C[-6],country,C[-4], ""Gold"")"

By

  Range("I73").Value = application.WorksheetFunction.CountIf(<your formula>)

当然,只有当您只能显示值而不是公式时,它才有效。

Alternatively, you could replace

  Range("I73").Select
    ActiveCell.FormulaR1C1 = _
    "=COUNTIFS(C[-7], class,C[-6],country,C[-4], ""Gold"")"

By

  Range("I73").Value = application.WorksheetFunction.CountIf(<your formula>)

Of course, it only works if you can show only the value instead of the formula.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文