2017-01-20 139 views
0

我正在使用SumIf函數將所有包含字符串dailyTotal的單元格合計到它們的左側。然後我在Macros表單上設置單元格C29爲該值,並將其與數字進行比較以查看接下來執行的代碼。我在E列單元格的值,但價值總是返回爲0SumIf值始終返回爲0 - Excel VBA

代碼:

dailyTotal = "Daily Total:" 
Dim totalHours As Double 
Dim rng As Range: Set rng = Application.Range("Totals!E11:E100") 
Dim cell As Range 

For Each cell In rng.Cells 
    With Sheets("Totals") 
    Sheets("Macros").Range("C29").Formula = Application.SumIf(cell.Offset(, -1), dailyTotal, "") 
    Sheets("Macros").Range("C29") = totalHours 
    End With 
Next cell 

' check if command is applicable 
If totalHours > 4 Then 
+1

的SUMIF公式是錯誤的:SUM(RANGE ,標準,[RANGE_TO_SUM])。你想要使用整個範圍而不是單個單元格。你有兩個標準,字符串''日常總計:''和''''你能顯示一些測試數據和預期結果嗎? –

+1

調整到一個評論,因爲這只是部分:刪除此行:「表(」宏「)。範圍(」C29「)= totalHours」 – Zerk

回答

1

你可能會在這之後

Dim totalHours As Double 
Dim dailyTotal As String: dailyTotal = "Daily Total:" 
Dim rng As Range: Set rng = Application.Range("Totals!E11:E100") 

totalHours = Application.SumIf(rng.Offset(, -1), dailyTotal, rng) 
Sheets("Macros").Range("C29") = totalHours 

' check if command is applicable 
If totalHours > 4 Then ... 
+0

這工作,謝謝。顯然,我是通過循環遍歷所有單元而不是範圍來使它複雜化。 – duberry

+0

不客氣。好的編碼! – user3598756