2014-02-28 97 views
0

是否有可能使其更清晰可讀?這只是一些If語句,用於檢查數組的值是否爲零。如果值爲零,則會清除一些單元格,如果不是,則會與值進行一些運算,然後將解決方案寫入單元格中,而不是清理它們。清理一些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 
+0

如果你所通過他們提出的數'varItem()'內部分成整數,然後循環?看起來''IF'語句中的代碼使用'i +(n-1)',所以你可以這樣做:'對於n = 3到6 ...'我意識到這並不完全工作,但這是一個開始。 – Sam

+0

我想我們需要看到更多的上下文來回答這個問題。我假設'我'是某種循環。你可以考慮的一件事就是清除頂部一個命令中的每個單元格,然後只在數組元素爲'<> 0'時填充它們。這會把它清理一下。 –

+0

感謝大家,你們幫我清洗。 – Eugen

回答

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