您能帮我更正我的VBA代码吗?我想将U列的值转换为活动行之前的“绝对值”,以除去负数。
这是我的VBA代码:

Sub MakeColumnsAbsoluteValue()

    Dim sht As Worksheet
    Dim rngToAbs As Range
    Dim LastRow As Long
    Dim c As Range

    Set sht = ThisWorkbook.Sheets("MJEBlackline")
    LastRow = sht.Cells(sht.Rows, Count, "U").End(xlUp).Row
    Set rngToAbs = Range("U5:U" & LastRow)

    For Each c In rngToAbs
        c.Value = Abs(c.Value)

    Next c


End Sub

最佳答案

您可以尝试:

Sub MakeColumnsAbsoluteValue()

Dim sht As Worksheet
Dim rngToAbs As Range
Dim LastRow As Long

Set sht = ThisWorkbook.Sheets("MJEBlackline")
With sht
    LastRow = .Cells(.Rows.Count, "U").End(xlUp).Row
    Set rngToAbs = .Range("U5:U" & LastRow)
    rngToAbs.Value = .Evaluate("=abs(" & rngToAbs.Address & ")")
End With

End Sub


甚至(inspired through @GarysStudent):

Sub MakeColumnsAbsoluteValue()

Dim sht As Worksheet
Dim rngToAbs As Range
Dim LastRow As Long

Set sht = ThisWorkbook.Sheets("MJEBlackline")
With sht
    LastRow = .Cells(.Rows.Count, "U").End(xlUp).Row
    Set rngToAbs = .Range("U5:U" & LastRow)
    rngToAbs.Replace what:="-", lookat:=xlPart, replacement:=""
End With

End Sub


这将一次转换整个范围。假设这就是您的意思:


“我想转换U列的值直到活动行...”

关于excel - 使特定列中的所有事件单元格达到其绝对值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57585430/

10-13 04:51