VBA select 变量设置为相对单元格的 case 语句发出循环引用错误

VBA select case statements with variable set as relative cell giving off circular reference error

我希望利用下面的用户定义公式来创建一个变量 (cell_val),该变量在许多 select 个案例语句中与 return 个特定的 sumif函数结果。

在给定 cell_val 值(字符串)的情况下,sumif 函数中唯一发生变化的变量是 sumif 函数的求和列。

我收到循环引用/#value 错误,希望你们中的一些好心人可以提供一些建议给我 =))

Function pop_sched()
    
    Dim cell_val As String
    cell_val = ActiveCell.Offset(0, -5).Value
    
    Select Case cell_val
        
        Case "1.Provision_Net_Revenue"
            ActiveCell.FormulaR1C1 = "=SUMIFS(stage3!C[-7],stage3!C[-12],'schedule updated'!RC[-12],stage3!C[-11],'schedule updated'!RC[-8])"
        
        Case "2.Credit_Losses_PCL"
            ActiveCell.FormulaR1C1 = "=SUMIFS(stage3!C[-9],stage3!C[-12],'schedule updated'!RC[-12],stage3!C[-11],'schedule updated'!RC[-8])"
        
        Case "3.Trading_Losses"
            ActiveCell.FormulaR1C1 = "=SUMIFS(stage3!C[-10],stage3!C[-12],'schedule updated'!RC[-12],stage3!C[-11],'schedule updated'!RC[-8])"
        
        Case "9.Tax"
            ActiveCell.FormulaR1C1 = "=SUMIFS(stage3!C[-8],stage3!C[-12],'schedule updated'!RC[-12],stage3!C[-11],'schedule updated'!RC[-8])"
        
        Case Else
            ActiveCell.Value = 0

    End Select
    
End Function

在单元格 M4 中输入 =pop_sched(H4)

Function pop_sched(cell As Range)
    
    Dim c As String
    Dim ws As Worksheet
    
    Set ws = Sheets("stage3")
    Select Case cell.Value
        
        Case "3.Trading_Losses":  c = "C"
        Case "2.Credit_Losses_PCL": c = "D"
        Case "9.Tax": c = "E"
        Case "1.Provision_Net_Revenue": c = "F"
        Case Else
           pop_sched = 0
           Exit Function

    End Select
    pop_sched = WorksheetFunction.SumIfs(ws.Columns(c), ws.Columns("A:A"), _
                cell.Offset(, -7), ws.Columns("B:B"), cell.Offset(, -3))

End Function