简体   繁体   English

VBA SUMIF 公式未填充任何内容

[英]VBA SUMIF formula isn't populating anything

I'm trying to populate multiple cells within a summary sheet by using the SUMIF formula in VBA and I can't figure out why it isn't working.我正在尝试使用 VBA 中的 SUMIF 公式在汇总表中填充多个单元格,但我不知道为什么它不起作用。

I have 2 sheets - Summary and CPTView I want cell C7 to populate the result of the sumif formula.我有 2 张工作表 - 摘要和 CPTView 我希望单元格 C7 填充 sumif 公式的结果。 I want it to look in CPTView A:A for dates that match Summary cell C4 and sum CPTView C:C.我希望它在 CPTView A:A 中查找与摘要单元格 C4 匹配的日期,并对 CPTView C:C 求和。

Here's the code I have.这是我的代码。 I want to try and avoid using Application.WorksheetFunction because I want this cell re-calculate when the data is changed.我想尝试避免使用 Application.WorksheetFunction 因为我希望在数据更改时重新计算此单元格。

Please help:( I have to populate a ton of cells with a similar formula请帮助:(我必须用类似的公式填充大量单元格

 Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Summary") Dim cptrng As Range Dim sumrng As Range Dim cpt As String Set cptrng = Sheets("CPTView").Range("A1:A1000") Set sumrng = Sheets("CPTView").Range("C1:C1000") cpt = ws.Range("C4").Value ws.Range("C7").Formula = "=SumIf(cptrng, cpt, sumrng)" End Sub

Update I changed the above to更新我将上面的内容更改为

Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Summary") Dim cptrng As Range Dim sumrng As Range Dim cpt As String Set cptrng = Sheets("CPTView").Range("A1:A1000") Set sumrng = Sheets("CPTView").Range("C1:C1000") cpt = ws.Range("C4").Value ws.Range("C7").Formula = "=SumIf(" & cptrng & ", " & cpt & ", " & sumrng & ")"

The formula shows all of the correct cells and ranges but i'm now getting a Type Mismatch error.该公式显示了所有正确的单元格和范围,但我现在收到类型不匹配错误。 I'm still fairly new to VBA so i'm not sure how to fix this.我对 VBA 还是很陌生,所以我不知道如何解决这个问题。 I think it's because the date formats aren't matching.. the criteria date shows as "mm/dd/yyyy" but the range shows the dates as "mm/dd/yyyy h:mm"我认为这是因为日期格式不匹配。标准日期显示为“mm/dd/yyyy”,但范围显示日期为“mm/dd/yyyy h:mm”

You need to build the formula string from a combination of text and variables.您需要结合文本和变量构建公式 string。

Instead, you are literally inserting this formula:相反,您实际上是在插入这个公式:

 =SumIf(cptrng, cpt, sumrng)

To get the effect you want, you have two options:要获得您想要的效果,您有两种选择:

Option 1选项1

Build it on the fly, in this line:在这一行中即时构建它:

 ws.Range("C7").Formula = "=SumIf(cptrng, cpt, sumrng)"

By replacing it with:通过将其替换为:

 ws.Range("C7").Formula = "=SumIf(" & cptrng.address & ", " & cpt & ", " & sumrng.address & ")"

Option 2选项 2

Build a formula variable first, then set the formula as that variable.首先构建一个公式变量,然后将公式设置为该变量。

Write a SUMIF Formula With VBA用 VBA 写一个 SUMIF 公式

Sub SumIfFormula() ' Source Const sName As String = "CPTView" Const slAddr As String = "A1:A1000" ' Lookup Const ssAddr As String = "C1:C1000" ' Sum ' Destination Const dName As String = "Summary" Const dlAddr As String = "C4" ' Lookup Const dsAddr As String = "C7" ' Sum Dim Formula As String: Formula = "=SUMIF('" & sName & "'," & slAddr & "," _ & dlAddr & "." & "'" & sName & "'." & ssAddr & ")" 'Debug.Print Formula ThisWorkbook.Worksheets(dName) Range(dsAddr) Formula = Formula End Sub

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

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