Search code examples
excelvbacomparemsgbox

Excel VBA IF statement for Msgbox (range1<>range2+range3) then


VBA noob here,

So i have 3 Columns (A,B and C)

A = Received Goods
B = Sent Goods for Shop 1
C = Sent Goods for Shop 2

I want values cells in B+C = A otherwise give me an error message.

Here is my code:

If Range("A1").End(xlDown).Value <> Range("B1").End(xlDown).Value & Range("C1").End(xlDown).Value Then
MsgBox "Error, wrong number of sent goods"
End If

End Sub

Solution

  • This is it:

    Option Explicit
    Sub Warning()
    
        Dim A As Long, B As Long, C As Long 'in case you have floating numbers use Single or Double instead
    
        With ThisWorkbook.Sheets("NameOfYourSheet")
            A = .Cells(.Rows.Count, "A").End(xlUp)
            B = .Cells(.Rows.Count, "B").End(xlUp)
            C = .Cells(.Rows.Count, "C").End(xlUp)
        End With
    
        If B + C <> A Then
            MsgBox "Error, wrong number of sent goods"
        End If
    
    End Sub
    

    I'm assuming you are trying the last row of every column.