有条件检查正值对负面

时间:2009-11-09 12:34:03

标签: excel vba excel-vba

If声明中,如何将正数与负数匹配 例如如果500匹配-500,剪切和粘贴

我想到的一个解决方案是在我做任何事情之前用*-1更改其中一列,但无论如何我可以改写下面的If语句以匹配正单元格和负单元格吗? / p>


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

2 个答案:

答案 0 :(得分:5)

您是否尝试过使用Abs

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

答案 1 :(得分:2)

ABS函数返回绝对值,即正值。所以ABS(-14) = 14。您可以在两个值周围使用它。