如果满足条件,则将正数更改为负数

问题描述 投票:-1回答:1

我有一个列表,其中列中的所有金额都被格式化为Dr和Cr,我已经记录了一个宏,它自动过滤并搜索“Cr”符号并将其更改为否定但是当我的列表增加并更新时,此宏是不工作我在VBA中使用了Autofilters,但没有任何效果。

我正在寻找一个代码,当它在列中找到“Cr”时,它应该将其更改为负数。

click here for image

excel vba
1个回答
0
投票

尝试可能有帮助:

Option Explicit

Sub test()

    Dim ws As Worksheet
    Dim Lastrow As Long, Row As Long

    With ThisWorkbook.Worksheets("Sheet1")
        'Find Last row of column A in sheet ws
        Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

            'Loop rows from bottom to top
            For Row = 1 To Lastrow

                'Check value lenght
                If Right(.Range("A" & Row).Value, 2) = "Cr" Then
                    .Range("A" & Row).Value = Abs(.Range("A" & Row).Value) * -1
                End If

            Next Row

    End With

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.