如何从两个表中读取相同的数据,然后使用vba减去数据

时间:2017-05-17 07:54:37

标签: excel vba

我需要一些公式或函数来使用相同的id(条形码)从另一张纸上减去。

当您单击按钮保存或提交时,数据(数量)将使用宏更改。

stock enter image description here

invoice ![enter image description here

我想知道如何从发票到库存表中减去数据:

answer = invoice (Quantity) - stock (quantity)

这是我的代码:

Option Explicit

Sub inv()


  Dim rng As Range
  Dim rng1 As Range
  Dim i As Long
  Dim A As Long
  Dim B As Range
  Dim rng_dest As Range
  Dim RANGE1 As Range
  Dim RANGE2 As Range


    'for subtract quantity in invoice and stock
    Dim num1 As Integer
    Dim num2 As Integer
    Dim ans As Integer
    Dim Stock As Long
    Dim invoice As Range
    Dim stk As Range




 Application.ScreenUpdating = False

    num1 = ThisWorkbook.Sheets("Invoice").Range("B8:B37").Value
    num2 = ThisWorkbook.Sheets("STOCK").Range("B8:B").Value

    ans
     If num2 = num1 Then
      ans = num2 - num1





  'Check if invoice # is found on sheet "Invoice data"

  i = 1

  Do Until Sheets("Invoice data").Range("A" & i).Value = ""

    If Sheets("Invoice data").Range("A" & i).Value = Sheets("Invoice").Range("H3").Value Then

      'Ask overwrite invoice #?

      If MsgBox("Invoice Already Exist.. Do you want to Overwrite.?", vbYesNo) = vbNo Then

        Exit Sub

      Else

        Exit Do

      End If

    End If

    i = i + 1

  Loop


  i = 1

  Set rng_dest = Sheets("Invoice data").Range("D:D")
    Set RANGE1 = Sheets("Invoice data").Range("G:G")

  'Delete rows if invoice # is found

  Do Until Sheets("Invoice data").Range("A" & i).Value = ""

    If Sheets("Invoice data").Range("A" & i).Value = Sheets("Invoice").Range("H3").Value Then

      Sheets("Invoice data").Range("A" & i).EntireRow.Delete

      i = 1

    End If

    i = i + 1

  Loop

  ' Find first empty row in columns D:G on sheet Invoice data

  Do Until WorksheetFunction.CountA(rng_dest.Rows(i)) = 0

    i = i + 1
     Do Until WorksheetFunction.CountA(RANGE1.Rows(i)) = 0

    i = i + 1
    Loop

  Loop

      'Copy range B16:I38 on sheet Invoice

  Set rng = Sheets("Invoice").Range("B8:B37")
    Set RANGE2 = Sheets("Invoice").Range("D8:D37")


  ' Copy rows containing values to sheet Invoice data

    For A = 1 To rng.Rows.Count


    If WorksheetFunction.CountA(rng.Rows(A)) <> 0 Then

      rng_dest.Rows(i).Value = rng.Rows(A).Value

       If WorksheetFunction.CountA(RANGE2.Rows(A)) <> 0 Then

      RANGE1.Rows(i).Value = RANGE2.Rows(A).Value


      Sheets("Invoice data").Range("A" & i).Value = Sheets("Invoice").Range("H3").Value

      'Copy Date

      Sheets("Invoice data").Range("B" & i).Value = Sheets("Invoice").Range("H2").Value

      'Copy Company name

      Sheets("Invoice data").Range("C" & i).Value = Sheets("Invoice").Range("B1").Value



      i = i + 1
    End If
       End If




  Next A

  MsgBox ("Invoice saved!")


    Application.ScreenUpdating = True
    Sheets("Invoice").Range("H3").Value = _
    Sheets("Invoice").Range("H3").Value + 1




End Sub







Sub Formc()

  UserForm1.Show

End Sub

1 个答案:

答案 0 :(得分:2)

这种情况不是Excel的构建方式。它可以做到,但是由于不了解依赖关系,因此在解决问题时存在许多危险。

首先,Quantity列应该相对于起始库存计数,因此您可以使用起始计数作为基准水平,然后减去发票中引用的任何库存数量。

我会添加一个新列“起始库存余额”,其中包含每个项目的初始库存计数。

然后,您可以创建一个包含当前库存的列,该库考虑了起始余额,并使用发票表中的相同代码减去所有项目的总和。如果起始库存余额在D列中,并且当前库存应在E列中计算,请使用以下行的公式:

=D2-Sumifs(Invoice!$D:$D,Invoice!$C:$C,$B2)

单词:取D2中的值并减去发票栏D中可找到的所有项目的总和,其中发票栏C与当前行中的库存表栏B的描述相同。

根据您的情况调整单元格范围和工作表名称。

尽管@vacip声称这可以通过Vlookup完成,但它不能。

编辑:或者如果您希望将公式基于条形码而不是描述,请使用

=D2-Sumifs(Invoice!$D:$D,Invoice!$B:$B,$A2)