【问题标题】:How do I compare the values of a column and then subtract values from the adjacent column如何比较列的值,然后从相邻列中减去值
【发布时间】:2015-02-25 09:24:05
【问题描述】:

我有两个工作表。 WS1 和 WS2

WS1 - A 列和 WS2 - A 列有产品代码。
WS1 - B 列和 WS2 - B 列有订购数量。

我想做的是比较 WS1-A 和 WS2-A。
如果字符串匹配,则从 WS1-B 中减去 WS2-B。
如果没有匹配,则转到下一行。

我找到了一些代码,但由于我是 VBA 新手,我不太清楚如何修改它以满足我的需要。

Public Sub CompareRange(range1 As Range, range2 As Range)
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim lastRow1 As Integer, lastrow2 As Integer
    Dim rng1 As Range, rng2 As Range
    Dim CompareCell As Range
    Dim CheckCell As Range
    Dim CellFound As Boolean

    Application.ScreenUpdating = False

    Set ws1 = ThisWorkbook.Sheets("Sheet9")
    Set ws2 = ThisWorkbook.Sheets("Sheet12")

    lastRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
    lastrow2 = ws2.Range("A" & Rows.Count).End(xlUp).Row

    Set rng1 = ws1.Range("A1:A" & lastRow1)
    Set rng2 = ws2.Range("A1:A" & lastrow2)

    Set qty1 = ws1.Range("B1:B" & lastRow1)
    Set qtyair = ws2.Range("B1:B" & lastrow2)

    For Each CompareCell In rng1.Cells
        CellFound = False
        For Each CheckCell In rng2.Cells
            If CheckCell.Text = CompareCell.Text Then

            End If
        Next CheckCell
        If Not CellFound Then
        End If
    Next CompareCell
End Sub

请告知我如何在 Excel VBA 中完成这项工作。我正在使用 Excel 2013。

【问题讨论】:

  • 如果我正确理解了你的问题,你能不能一起使用VLOOKUPIFERROR 函数来查找值并减去它是否存在。如果 if 不存在,什么都不做?

标签: vba excel compare subtraction


【解决方案1】:
sub match_col()

Set ws1 = ThisWorkbook.Sheets("Sheet9")
Set ws2 = ThisWorkbook.Sheets("Sheet12")

lastRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
lastrow2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
i = 1 to lastRow1
j = 1 to lastRow2

if worksheets("ws1_A").range("a" & i).value =  worksheets("ws2_A").range("a" & i).value 

msgBox worksheets("ws2_B").range("B" & i).value - worksheets("ws1_B").range("B" & i).value

end if
next j
next i

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    相关资源
    最近更新 更多