nsy
nsy

Reputation: 35

Conditional check positive value against negative

In a If statement, how do I match a positive number with a negative one e.g. if 500 matches with -500, cut and paste

One solution I thought of is changing one of the columns by *-1 before I do anything but is there anyway I could rephrase the below If statement to match a positive cell and a negative cell?


Dim sh1 As Worksheet, sh2 As Worksheet
Dim j As Long, i As Long, lastrow1 As Long, lastrow2 As Long

    Set sh1 = Worksheets("WorksheetA")
    Set sh2 = Worksheets("WorksheetB")

    lastrow1 = sh1.Cells.SpecialCells(xlCellTypeLastCell).Row
    lastrow2 = sh2.Cells.SpecialCells(xlCellTypeLastCell).Row

    For i = 1 To lastrow1

        For j = 1 To lastrow2

            If sh1.Cells(i, "H").Value = sh2.Cells(j, "E").Value Then
               sh2.Cells(j, "O").Value .Cut sh1.Cells(i, "L").Value
            End If

        Next j
    Next i
End Sub

Upvotes: 1

Views: 1547

Answers (2)

Joel Goodwin
Joel Goodwin

Reputation: 5086

The ABS function returns the absolute value, i.e. the positive value. So ABS(-14) = 14. You could use this around both values.

Upvotes: 2

Adriaan Stander
Adriaan Stander

Reputation: 166396

Have you tried using Abs

If (Abs(sh1.Cells(i, "H").Value) = Abs(sh2.Cells(j, "E").Value)) Then

Upvotes: 5

Related Questions