在输入数组之前先从variant-post中减去值

时间:2018-09-24 10:19:09

标签: excel vba excel-vba

我使用以下内容从报告中提取数据,此外,我使用公式从“金额”中减去“增值税”。 我的问题是,是否有可能避免使用公式,而只是在将“ VAT”单元格值输入到数组中之前从“金额”单元格值中减去(可能在第23行)

Option Explicit
Option Base 1

Sub ExtractData()

    Dim ws As Worksheet, MyRange As Range, destination As Range
    Dim pName As Integer, pNr As Integer, amount As Integer, vat As Integer, dDate As Integer, i As Long
    Dim sourceData() As Variant, targetData() As Variant, x As Double


    Set ws = Sheets(1)
    Set MyRange = ws.Range("A1").CurrentRegion
    Set destination = MyRange.Cells(1).Offset(, MyRange.Columns.Count + 1)
    sourceData = MyRange.Value2


    With MyRange
        pName = WorksheetFunction.Match("Product Name", .Rows(1), 0)
        pNr = WorksheetFunction.Match("Product Number", .Rows(1), 0)
        amount = WorksheetFunction.Match("Amount", .Rows(1), 0)
        vat = WorksheetFunction.Match("VAT", .Rows(1), 0)
        dDate = WorksheetFunction.Match("Date", .Rows(1), 0)
    End With

    ReDim targetData(UBound(sourceData, 1), 5)

    For i = 1 To UBound(sourceData, 1)
        targetData(i, 1) = sourceData(i, pName)
        targetData(i, 2) = sourceData(i, pNr)
        targetData(i, 3) = sourceData(i, amount)'-sourceData(i, vat)????
        targetData(i, 4) = sourceData(i, vat)
        targetData(i, 5) = sourceData(i, dDate)
    Next

    destination.Resize(UBound(targetData, 1), UBound(targetData, 2)).Value = targetData

End Sub

1 个答案:

答案 0 :(得分:1)

第一行尝试减去两个字符串。在For块中尝试一下,“ IF”是我更改的部分。

For i = 1 To UBound(sourceData, 1)
    targetData(i, 1) = sourceData(i, pName)
    targetData(i, 2) = sourceData(i, pNr)
    If (i <> 1) Then
        targetData(i, 3) = sourceData(i, amount) - sourceData(i, vat)
    Else
        targetData(i, 3) = "Net"
    End If
    targetData(i, 4) = sourceData(i, vat)
    targetData(i, 5) = sourceData(i, dDate)
Next