Add a test for 0 in testCountCurr. if 0, use brand A:
api.Data.ClearCalculatedData(True,True,True,True,,,,,,,,,,,"U7#DirCOGS_Calc",)
'Create a result data buffer and destination info to add the cells to later
Dim resultDataBuffer6 As DataBuffer = New DataBuffer()
Dim destinationInfo6 As ExpressionDestinationInfo = api.Data.GetExpressionDestinationInfo("F#EndBalLoad:O#Import:I#None" & _
":U5#None:U6#None:U7#DirCOGS_Calc:U8#None")
'Get total Direct COGS to use in Calc
Dim TotDirCOGSDataBuffer As DataBuffer = api.Data.GetDataBufferUsingFormula("FilterMembers(RemoveZeros(A#Dir_COGS:F#Top:O#Top:T#POVPrior1:I#Top" & _
":U5#None:U6#None:U7#Top:U8#Top),U2#101)")
Dim TotDirCOGSDataBuffer As DataBuffer = api.Data.GetDataBufferUsingFormula("RemoveZeros(FilterMembers(A#Dir_COGS:F#Top:O#Top:T#POVPrior1:I#Top" & _
":U5#None:U6#None:U7#Top:U8#Top,U2#101))")
For Each TotDirCOGSSourceCell As DataBufferCell In TotDirCOGSDataBuffer.DataBufferCells.Values
Dim UD1 As String = TotDirCOGSSourceCell.GetUD1Name(api).ToString 'Brand
Dim UD1A As String = $"{UD1}A" 'when brand is 0 use brand A
Dim TotDirCOGSAmount As Decimal = TotDirCOGSSourceCell.CellAmount
'Declare source data buffers for Run Rate
Dim DirCOGSDataBuffer As DataBuffer = api.Data.GetDataBufferUsingFormula("FilterMembers(RemoveZeros(F#Top:O#Top:T#POVPrior1:I#Top" & _
":U5#None:U6#None:U7#Top:U8#Top),A#Dir_COGS.Base,U1#" & UD1 & ",U2#101)")
Dim DirCOGSDataBuffer As DataBuffer = api.Data.GetDataBufferUsingFormula("RemoveZeros(FilterMembers(F#Top:O#Top:T#POVPrior1:I#Top" & _
":U5#None:U6#None:U7#Top:U8#Top,A#Dir_COGS.Base,U1#" & UD1 & ",U2#101))")
For Each DirCOGSSourceCell As DataBufferCell In DirCOGSDataBuffer.DataBufferCells.Values
Dim UD2 As String = DirCOGSSourceCell.GetUD2Name(api).ToString ' Department
Dim DirCOGSAmount As Decimal = DirCOGSSourceCell.CellAmount
Dim resultCell As New DataBufferCell(DirCOGSSourceCell)
'Declare variables for TestCount
Dim TestCountCurr As Decimal = api.data.getdatacell("A#TestCount:T#" & POVPERIOD & ":V#Periodic:F#Top:O#Top:I#Top:U1#" & UD1 & ":U2#Top:U3#Top:U4#Top:U5#None:U6#None:U7#Top:U8#Top").CellAmount
If TestCountCurr = 0 Then 'use brand A
TestCountCurr = api.data.getdatacell("A#TestCount:T#" & POVPERIOD & ":V#Periodic:F#Top:O#Top:I#Top:U1#" & UD1A & ":U2#Top:U3#Top:U4#Top:U5#None:U6#None:U7#Top:U8#Top").CellAmount
End If
'Declare variables Test Rate
Dim TestRate As Decimal = api.data.getdatacell("A#TestRate:T#" & sPOVYEAR & Quarter &":V#Periodic:F#EndBalLoad:O#Forms:I#None:U1#" & UD1 & ":U2#None:U3#None:U4#None:U5#None:U6#None:U7#None:U8#None").CellAmount
If UD2 = "101" Then
resultCell.CellAmount = DirCOGSAmount/TotDirCOGSAmount * TestCountCurr * TestRate
End If
resultDataBuffer6.SetCell(si, resultCell)
Next
Next
api.Data.SetDataBuffer(resultDataBuffer6,destinationInfo6)
ps, its always a good idea to test denominators for 0 before using them.