清理一些If语句

时间:2014-02-28 10:50:14

标签: excel vba

是否可以使其更干净和可读?它只是一些If语句,用于检查数组的值是否为零。如果值为零,它将清除一些单元格,如果不是,它会对值进行一些计算,并将解决方案写入单元格而不是清除它们。

            If varItem(3) = 0 Then
                .Cells(i + 1, intColumn).Value = ""
                .Cells(i + 2, intColumn).Value = ""
            Else
                .Cells(i + 1, intColumn).Value = varItem(3)                                        ' Stock on Hand
                .Cells(i + 2, intColumn).Value = "=N" & i + 1 & "-N" & i + 3 & "-N" & i + 4        ' Formula for Healthy Stock (SoH - ExV - Inac)
            End

            If varItem(4) = 0 Then
                .Cells(i + 3, intColumn).Value = ""
            Else
                .Cells(i + 3, intColumn).Value = varItem(4)                                        ' Excess Value
            End

            If varItem(5) = 0 Then
                .Cells(i + 4, intColumn).Value = ""
            Else
                .Cells(i + 4, intColumn).Value = varItem(5)                                        ' Inactive Value
            End If

            If varItem(6) = 0 Then
                 .Cells(i + 5, intColumn).Value = ""
            Else
                .Cells(i + 5, intColumn).Value = varItem(6)                                        ' Inactive 12M Value
            End


            If varItem(2) = 0 Or varItem(3) = 0 Then
                .Cells(intRow, 20).Value = ""
                .Cells(intRow, 21).Value = ""
            Else
                .Cells(intRow, 20).Value = varItem(2) / varItem(3)              ' Stockturn
                .Cells(intRow, 21).Value = varItem(3) / (varItem(2) / 365)      ' Days of Inventory
            End If

            If varItem(1) = 0 Or varItem(2) = 0 Then
                .Cells(intRow, 22).Value = ""
            Else
                .Cells(intRow, 22).Value = varItem(1) / varItem(2)              ' Safety Stock Value
            End If

            If varItem(4) = 0 Or varItem(5) = 0 Then
                .Cells(intRow, 23).Value = ""
            Else
                .Cells(intRow, 23).Value = varItem(4) / varItem(3)              ' Excess Value in %
            End If

            If varItem(6) = 0 And varItem(3) = 0 Then
                .Cells(intRow, 24).Value = ""
            Else
                .Cells(intRow, 24).Value = varItem(6) / varItem(3)              ' Inactive 12M Stock Value in %
            End

1 个答案:

答案 0 :(得分:0)

        ' Stock on Hand
        .Cells(i + 1, intColumn).Value = EmptyIfZero(varItem(3))            
        If varItem(3) = 0 Then
            .Cells(i + 2, intColumn).Value = ""
        Else
            ' Formula for Healthy Stock (SoH - ExV - Inac)
            .Cells(i + 2, intColumn).Value = "=N" & i + 1 & "-N" & i + 3 & "-N" & i + 4        
        End
        'Excess Value
        .Cells(i + 3, intColumn).Value = EmptyIfZero(varItem(4))
        'Inactive Value
        .Cells(i + 4, intColumn).Value = EmptyIfZero(varItem(5)) 
        'Inactive 12M Value                              
        .Cells(i + 5, intColumn).Value = EmptyIfZero(varItem(6))                                        

        'Stockturn
        .Cells(intRow, 20).Value = DivisionIfNotZero(varItem(2),varItem(3) )
        If varItem(2) = 0 Or varItem(3) = 0 Then
            .Cells(intRow, 21).Value = ""
        Else
            ' Days of Inventory
            .Cells(intRow, 21).Value = varItem(3) / (varItem(2) / 365)      
        End If
        ' Safety Stock Value
        .Cells(intRow, 22).Value = DivisionIfNotZero(varItem(1),varItem(2) )               

        'is this intentional? varItem(5) and  varItem(3) 
        If varItem(4) = 0 Or varItem(5) = 0 Then
            .Cells(intRow, 23).Value = ""
        Else
            ' Excess Value in %
            .Cells(intRow, 23).Value = varItem(4) / varItem(3)              
        End If
        ' Inactive 12M Stock Value in %
        .Cells(intRow, 24).Value = DivisionIfNotZero(varItem(6),varItem(3) )             



function EmptyIfZero(variant1)
    If variant1 = 0 then
        EmptyIfZero = ""
    else
        EmptyIfZero = variant1
    end if
end function

function DivisionIfNotZero(variant1,variant2)
    If variant1 = 0 or variant2 = 0 then
        DivisionIfNotZero= ""
    else
        DivisionIfNotZero= variant1/variant2
    end if
end function