仅当Range不是完全空白时,Excel VBA到SUMIF范围

时间:2018-05-04 15:36:38

标签: excel vba excel-vba sum sumifs

我的数据问题让我困扰了几个星期。

目标: - 将设置范围设置为新的参考单元格。对跨越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

End Sub

1 个答案:

答案 0 :(得分:0)

您可以直接在公式中添加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]};
     ""
 )