如果有负值,则其他列应获取Text

时间:2017-09-16 10:06:38

标签: excel excel-vba vba

当A列单元格中的值为负数时,如何将N作为对应于A列单元格的B列单元格中的注释。

例如:

   A     B
   12
 -123   Negative
  123
 -456   Negative

1 个答案:

答案 0 :(得分:1)

解决方案1 ​​

使用VBA

Sub Demo()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim cel As range

    Set ws = ThisWorkbook.Sheets("Sheet2")                  'change Sheet2 to your data sheet
    With ws
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row    'get last row with data
        For Each cel In range("A1:A" & lastRow)             'loop through Column A
            If cel.Value < 0 Then cel.Offset(0, 1) = "Negative" 'check if number is negative
        Next cel
    End With
End Sub

解决方案2

使用公式

Cell B1中输入以下公式,然后根据需要拖放/复制。

=IF(SIGN(A1)=-1,"Negative","")

enter image description here