这应该很简单。以下是我正在使用的代码:
Private Sub CommandButton23_Click()
Dim linerngs As Range
Dim lineitem As Range
Dim lastlinerow As Long
Dim TabLastRow
Dim claimstab As String
Dim officesrange As Range
Dim office As Range
officeslastrow = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
lastlinerow = Sheet2.Range("D" & Rows.Count).End(xlUp).Row
Set officerng = Sheet2.Range("A6:A" & officeslastrow).SpecialCells(xlCellTypeConstants, 23)
Set linerngs = Sheet2.Range("D7:D" & lastlinerow).SpecialCells(xlCellTypeConstants, 23)
'Starts evaluating the first line item at the first home office
For Each lineitem In linerngs
'evaluates line item to identify which formula is needed to count it
If InStr(1, lineitem.Value, "IN") > 0 And InStr(1, lineitem.Value, "AOS") = 0 Then
lineitem.Offset(0, 16).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R2C5&"" Claims'!B:B""),R[0]C[-19],INDIRECT(""'""&R2C5&"" Claims'!C:C""),R[0]C[-17],INDIRECT(""'""&R2C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-15],R[0]C[-14]),INDIRECT(""'""&R2C5&"" Claims'!G:G""),R[0]C[-9]:R[0]C[-1]))"
lineitem.Offset(0, 17).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R3C5&"" Claims'!B:B""),R[0]C[-20],INDIRECT(""'""&R3C5&"" Claims'!C:C""),R[0]C[-18],INDIRECT(""'""&R3C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-16],R[0]C[-15]),INDIRECT(""'""&R3C5&"" Claims'!G:G""),R[0]C[-10]:R[0]C[-2]))"
End If
If InStr(1, lineitem.Value, "IN") > 0 And InStr(1, lineitem.Value, "AOS") > 0 Then
lineitem.Offset(0, 16).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R2C5&"" Claims'!B:B""),R[0]C[-19],INDIRECT(""'""&R2C5&"" Claims'!C:C""),R[0]C[-17],INDIRECT(""'""&R2C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-15],R[0]C[-14])))-SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R2C5&"" Claims'!B:B""),R[0]C[-19],INDIRECT(""'""&R2C5&"" Claims'!C:C""),R[0]C[-17],INDIRECT(""'""&R2C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-15],R[0]C[-14]),INDIRECT(""'""&R2C5&"" Claims'!G:G""),R2C11:R2C20))"
lineitem.Offset(0, 17).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R3C5&"" Claims'!B:B""),R[0]C[-20],INDIRECT(""'""&R3C5&"" Claims'!C:C""),R[0]C[-18],INDIRECT(""'""&R3C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-16],R[0]C[-15])))-SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R3C5&"" Claims'!B:B""),R[0]C[-20],INDIRECT(""'""&R3C5&"" Claims'!C:C""),R[0]C[-18],INDIRECT(""'""&R3C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-16],R[0]C[-15]),INDIRECT(""'""&R3C5&"" Claims'!G:G""),R2C11:R2C20))"
End If
If InStr(1, lineitem.Value, "Excess MO") > 0 And InStr(1, lineitem.Value, "AOS") = 0 Then
lineitem.Offset(0, 16).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R2C5&"" Claims'!B:B""),R[0]C[-19],INDIRECT(""'""&R2C5&"" Claims'!C:C""),R[0]C[-17],INDIRECT(""'""&R2C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-15],R[0]C[-14]),INDIRECT(""'""&R2C5&"" Claims'!P:P""),"">""&R2C22,INDIRECT(""'""&R2C5&"" Claims'!G:G""),R[0]C[-9]:R[0]C[-1]))"
lineitem.Offset(0, 17).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R3C5&"" Claims'!B:B""),R[0]C[-20],INDIRECT(""'""&R3C5&"" Claims'!C:C""),R[0]C[-18],INDIRECT(""'""&R3C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-16],R[0]C[-15]),INDIRECT(""'""&R3C5&"" Claims'!P:P""),"">""&R2C22,INDIRECT(""'""&R3C5&"" Claims'!G:G""),R[0]C[-10]:R[0]C[-2]))"
End If
If InStr(1, lineitem.Value, "Excess MO") > 0 And InStr(1, lineitem.Value, "AOS") > 0 Then
lineitem.Offset(0, 16).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R2C5&"" Claims'!B:B""),R[0]C[-19],INDIRECT(""'""&R2C5&"" Claims'!C:C""),R[0]C[-17],INDIRECT(""'""&R2C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-15],R[0]C[-14]),INDIRECT(""'""&R2C5&"" Claims'!P:P""),"">""&R2C22))-SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R2C5&"" Claims'!B:B""),R[0]C[-19],INDIRECT(""'""&R2C5&"" Claims'!C:C""),R[0]C[-17],INDIRECT(""'""&R2C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-15],R[0]C[-14]),INDIRECT(""'""&R2C5&"" Claims'!P:P""),"">""&R2C22,INDIRECT(""'""&R2C5&"" Claims'!G:G""),R2C11:R2C20))"
lineitem.Offset(0, 17).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R3C5&"" Claims'!B:B""),R[0]C[-20],INDIRECT(""'""&R3C5&"" Claims'!C:C""),R[0]C[-18],INDIRECT(""'""&R3C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-16],R[0]C[-15]),INDIRECT(""'""&R3C5&"" Claims'!P:P""),"">""&R2C22))-SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R3C5&"" Claims'!B:B""),R[0]C[-20],INDIRECT(""'""&R3C5&"" Claims'!C:C""),R[0]C[-18],INDIRECT(""'""&R3C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-16],R[0]C[-15]),INDIRECT(""'""&R3C5&"" Claims'!P:P""),"">""&R2C22,INDIRECT(""'""&R3C5&"" Claims'!G:G""),R2C11:R2C20))"
End If
If InStr(1, lineitem.Value, "Medical Only") > 0 Then
lineitem.Offset(0, 16).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R2C5&"" Claims'!B:B""),R[0]C[-19],INDIRECT(""'""&R2C5&"" Claims'!C:C""),R[0]C[-17],INDIRECT(""'""&R2C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-15],R[0]C[-14]),INDIRECT(""'""&R2C5&"" Claims'!P:P""),""<""&R2C22))"
lineitem.Offset(0, 17).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R3C5&"" Claims'!B:B""),R[0]C[-20],INDIRECT(""'""&R3C5&"" Claims'!C:C""),R[0]C[-18],INDIRECT(""'""&R3C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-16],R[0]C[-15]),INDIRECT(""'""&R3C5&"" Claims'!P:P""),""<""&R2C22))"
End If
If InStr(1, lineitem.Value, "Incident Only") > 0 Then
lineitem.Offset(0, 16).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R2C5&"" Claims'!B:B""),R[0]C[-19],INDIRECT(""'""&R2C5&"" Claims'!C:C""),R[0]C[-17],INDIRECT(""'""&R2C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-15],R[0]C[-14])))"
lineitem.Offset(0, 17).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R3C5&"" Claims'!B:B""),R[0]C[-20],INDIRECT(""'""&R3C5&"" Claims'!C:C""),R[0]C[-18],INDIRECT(""'""&R3C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-16],R[0]C[-15])))"
End If
Next lineitem
'repeats the process for the next line item until it reaches the end
'Calculates Net Claims
For Each lineitem In linerngs
lineitem.Offset(0, 19).Formula = "=R[0]C[-3] - SUM(R[0]C[-2]:R[0]C[-1])"
Next lineitem
End Sub
一切正常我只是在单元格中显示公式结果而不是整个公式时我会更喜欢它。我知道这是可能的我只是不记得我过去是怎么做的。任何帮助将不胜感激。
答案 0 :(得分:1)
有多种方法可以做到这一点。
在设置公式后,我个人会添加设置单元格值等于它自己的值。请参阅下面的示例摘录:
If InStr(1, lineitem.Value, "Incident Only") > 0 Then
lineitem.Offset(0, 16).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R2C5&"" Claims'!B:B""),R[0]C[-19],INDIRECT(""'""&R2C5&"" Claims'!C:C""),R[0]C[-17],INDIRECT(""'""&R2C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-15],R[0]C[-14])))"
lineitem.Offset(0, 16).Value = lineitem.Offset(0, 16).Value
lineitem.Offset(0, 17).Formula = "=SUMPRODUCT(COUNTIFS(INDIRECT(""'""&R3C5&"" Claims'!B:B""),R[0]C[-20],INDIRECT(""'""&R3C5&"" Claims'!C:C""),R[0]C[-18],INDIRECT(""'""&R3C5&"" Claims'!E:E""),CHOOSE({1;2},R[0]C[-16],R[0]C[-15])))"
lineitem.Offset(0, 17).Value = lineitem.Offset(0, 17).Value
End If
答案 1 :(得分:0)
而不是像:
Range("C1").Formula = "=SUM(A1,B1)"
使用:
Range("C1").Value = Evaluate("=SUM(A1,B1)")