首页 > 解决方案 > 仅当 Range 不完全为空白时,Excel VBA 到 SUMIF Range

问题描述

我有一个数据问题困扰了我几个星期。

目标: - 将集合范围汇总到新的参考单元格中。对跨越 42 列的 8 个不同范围执行此操作。这需要逐行执行,因此每个条目都有相应的编号。- 只有当 SUM 范围本身有任何值时,L:S 列才应该有一个值。没有假 0。

问题: - SUM 函数有效,但在 Sum 没有值的情况下返回 0。这是一个问题,因为 0 是一个有效值,而额外的 0 会影响那些实际拥有值的人的平均值。- SUMIFS 返回 TRUE 而不是一个值(我使用带有 1 个标准的 SUMIFS,因为它更容易理解,不要专注于那个位,因为它在 SUMIF 公式中做了同样的事情)。

这是我的 VBA 的全部内容:

Sub BE_Candidate_Flow_Time()
'
' BE_Candidate_Flow_Time Macro
'
Dim StartCell As Range
Dim RangeName As String
Dim myValue As Variant

Set StartCell = Range("A1")

myValue = InputBox("Enter Date: YY-MMM")

StartCell.CurrentRegion.Select
RangeName = "Dataset"

Dim LRow As Long
Dim lCol As Long

LRow = Cells(Rows.Count, 1).End(xlUp).Row

lCol = Cells(1, Columns.Count).End(xlToLeft).Column

Columns("J:Q").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("A:B").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").FormulaR1C1 = "Application ID"
Range("B1").FormulaR1C1 = "Timeframe"
Range("K1").FormulaR1C1 = "Job Code"
Range("L1").FormulaR1C1 = "Time to Complete the Assessment"
Range("M1").FormulaR1C1 = "Applied/Assessed and Ready for Review"
Range("N1").FormulaR1C1 = "Time being Reviewed and Interviewed"
Range("O1").FormulaR1C1 = "Time to Accept the Offer"
Range("P1").FormulaR1C1 = "Consent to Pre-Hire Screenings"
Range("Q1").FormulaR1C1 = "Run the Pre-Hire Screenings"
Range("R1").FormulaR1C1 = "Waiting to be sent to Onboard"
Range("S1").FormulaR1C1 = "In Onboard"

Range("A2", "A" & LRow).FormulaR1C1 = "=CONCATENATE(RC[2],RC[5])"
Range("B2", "B" & LRow).Value = myValue
Columns("T:BI").NumberFormat = "0.00"
Range("L2", "L" & LRow).FormulaR1C1 = "=SUMIFS({RC[9];RC[13]},{RC[9];RC[13]}," <> ")"
Range("M2", "M" & LRow).FormulaR1C1 = "=SUMIFS({RC[7];RC[9]:RC[11];RC[13]:RC[16]},{RC[7];RC[9]:RC[11];RC[13]:RC[16]}," <> ")"
Range("N2", "N" & LRow).FormulaR1C1 = "=SUMIFS({RC[16]:RC[27]},{RC[16]:RC[27]}," <> ")"
Range("O2", "O" & LRow).FormulaR1C1 = "=SUMIFS({RC[27]:RC[31];RC[33]:RC[34]},{RC[27]:RC[31];RC[33]:RC[34]}," <> ")"
Range("P2", "P" & LRow).FormulaR1C1 = "=SUMIFS({RC[34]},{RC[34]}," <> ")"
Range("Q2", "Q" & LRow).FormulaR1C1 = "=SUMIFS({RC[35]:RC[40]},{RC[35]:RC[40]}," <> ")"
Range("R2", "R" & LRow).FormulaR1C1 = "=SUMIFS({RC[40]:RC[41]},{RC[40]:RC[41]}," <> ")"
Range("S2", "S" & LRow).FormulaR1C1 = "=SUMIFS({RC[41]:RC[42]},{RC[41]:RC[42]}," <> ")"

Range("K1", "K" & LRow).AutoFilter 1, ""
Range("K2", "K" & LRow).FormulaR1C1 = "=RC[-1]"
[K1].AutoFilter

Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False

Range("J:J,T:BI").Delete Shift:=xlToLeft

Range("A1").Select

结束子

标签: vbaexcelsumsumifs

解决方案


您可以直接将 if 添加到公式中。类似的东西

=IF(
     count({RC[7];RC[9]:RC[11];RC[13]:RC[16]})>0;
     SUM({RC[7];RC[9]:RC[11];RC[13]:RC[16]};
     ""
 )

推荐阅读