简体   繁体   中英

VBA Excel Sum of Decimal

Source code:

Dim TH As Double
Lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
'starting point
sRow = 2
'Loop in  all cells

For i = sRow To Lr


     'check if cell value are not same
        If Cells(i, 1).Value <> Cells(i + 1, 1) Then
        'if not same then merge all the above cells

        Range("I" & sRow, "I" & i).Resize(, 7).Select
        TH = Application.WorksheetFunction.Sum(Selection)


        **If TH <> 40 Then**

            Range("A" & sRow, "A" & i).Interior.Color = RGB(255, 0, 0)
        End If

In this Code:
If TH <> 40 Then condition not working when ever the TH is Calculated on Decimal Numbers.

Such as 3.60,0.80,4.60 Sum is coming as 40 when use SUM Function but If Condition is not getting fulfilled.

Please Help

I have tried this, putting decimals all over and it works:

Sub TestMe()
    Dim lr As Long
    Dim TH As Double
    Dim i As Long
    lr = 10

    For i = 1 To lr
        If Cells(i, 1).Value <> Cells(i + 1, 1) Then
            Range("I" & 6, "I" & i).Resize(, 7).Select
            TH = Application.WorksheetFunction.Sum(Selection)
            If TH <> 40 Then
                Range("A" & 6, "A" & i).Interior.Color = RGB(255, 0, 0)
            End If
        End If
    Next

End Sub

Thus, probably the problem is the way you put the decimals. In some systems (German or French), the decimal separator is , , while in English systems it is a point - . . Thus, you might be using the wrong one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM