将列正值转换为负值VBA

时间:2016-11-22 02:31:07

标签: vba excel-vba loops excel

我知道这已被问过好几次,但是我对如何将L:L列的负值放在一个循环中感到很困惑。我无法让它发挥作用。我已经尝试过我所研究的一切。我很感激任何帮助。

 Option Explicit
Sub Importpaymentsales()
    Dim fpath As Variant
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    Dim Text As String
    On Error GoTo terminatemsg

    Set wb = Excel.ActiveWorkbook
    Set ws = Excel.ActiveSheet

    fpath = Application.GetOpenFilename(Filefilter:="text Files(*.txt; *.txt), *.txt; *.txt", Title:="Open Prepayment Sales Report")

    If fpath = False Then Exit Sub

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Text = getTextfileData(fpath)
    If Len(Text) Then
        ProcessData Text
        AdjustDates
    Else
        MsgBox fpath & " is empty", vbInformation, "Import Cancelled"
        Exit Sub

    End If
    ws.Range("J:L").Value = ws.Range("J:L").Value
    ws.Range("J:L").numberformat = "#,##0.00"


    ws.Range("O:Q").Value = ws.Range("O:Q").Value
    ws.Range("O:Q").numberformat = "#,##0.00"

    Columns.EntireColumn.AutoFit
    Sheets(1).Move Before:=wb.Sheets(1)

terminatemsg:
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    If Err.Number <> 0 Then MsgBox Err.Number & " " & Err.Description

End Sub

Sub ProcessData(Text As String)
    Dim x As Long, y As Long, z As Long
    Dim data, vLine

    data = Split(Text, vbCrLf)
    x = 2
    Range("A1:R1").Value = Array("Supplier Name", "Supplier Number", "Inv Curr Code CurCode", "Payment CurCode", "Invoice Type", "Invoice Number", "Voucher Number", "Invoice Date", "GL Date", "Invoice Amount", "Withheld Amount", "Amount Remaining", "Description", "Account Number", "Invoice in USD", "Withheld in USD", "Amt in USD", "User Name")

    For y = 0 To UBound(data)
        If InStr(data(y), "|") Then
            vLine = Split(data(y), "|")
            If Not Trim(vLine(0)) = "Supplier" Then

                For z = 0 To UBound(vLine)
                    vLine(z) = Trim(vLine(z))

                    If vLine(z) Like "*.*.*.*.*.*.*.*.*.*.*.*.*.*.*" Then vLine(z) = Left(vLine(z), InStr(vLine(z), ".") + 2)

                Next
                Cells(x, 1).Resize(1, UBound(vLine) + 1).Value = vLine
                x = x + 1
                'Range("L2:L").Value = Range("L2:L").Value * (-1)
                Range("L2:L").Value = Abs(rng.Offset(teller - 1, -2).Value) * -1
            End If

        End If
    Next

End Sub

1 个答案:

答案 0 :(得分:3)

试试这个:

  Dim r As Range
  For Each r In Range(Range("L2"), Range("L2").End(xlDown))
    If IsNumeric(r.Value) Then r.Value = -Abs(r.Value)
  Next

ps:我想你在L栏中没有空白单元格,如果你这样做,那么需要稍作修改。

这是:

Dim r As Range
For Each r In Range(Range("L2"), Range("L" & Rows.Count).End(xlUp))
    If Not IsEmpty(r.Value) Then If IsNumeric(r.Value) Then r.Value = -Abs(r.Value)
  Next