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

我有两个工作表。 WS1和WS2

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

我想要做的是比较WS1-A和WS2-A。
如果string匹配,则从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 
Interesting Posts