如何比较一列的值,然后从相邻列中减去值



我有两个工作表。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。

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

相关内容

  • 没有找到相关文章

最新更新