开发者

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

         开发者_JAVA百科   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


Have you tried using Abs

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜