首页 > 解决方案 > 如何比较行之间的值

问题描述

我在这里需要帮助。我有一个包含超过 6K 数据的电子表格。我需要使用 VBA比较“ MOVE_IN_QTY ”和“ MOVE_OUT_QTY ”之间的值。这里的问题是我需要在代码列中的代码从“ CV64 ”和“ TW78 ”更改后立即比较值。我用红色突出显示的值和我用蓝色和黄色突出显示的代码。我将不胜感激任何帮助。谢谢。

在此处输入图像描述

标签: excelvba

解决方案


在这里做一些假设:

  1. 搬入和搬出始终是数字。
  2. 使用 =,<,> 过程计算移动编号。
  3. 需要根据结果采取未知的进一步行动。

它还有助于包括您尝试过的内容和无效的内容。

Sub ReviewData()

Dim wkbk As Workbook
Dim xsheet As Worksheet
Dim codeColumn As String, moveIN As String, moveOUT As String
Dim rowCount As Double

Set wkbk = ThisWorkbook
Set xsheet = wkbk.Worksheets("Sheet1") 'change sheet name here
codeColumn = "B" ' change column letter here
moveIN = "C" 'set move in column
moveOUT = "D" 'set move out column

'this will loop through the Code column until the last set of data.
rowCount = xsheet.Range(codeColumn & xsheet.Rows.Count).End(xlUp).Row 'find last row

For x = 2 To rowCount
    'checks if code transitions from one code to another
    If not xsheet.Range(codeColumn & x).Value = xsheet.Range(codeColumn & x + 1).Value Then

        If xsheet.Range(moveIN & x).Value = xsheet.Range(moveOUT & x + 1).Value Then
            'do something if the code is the same
        Else
            xsheet.Range(codeColumn & x).Interior.ColorIndex = 3
            MsgBox ("Row: " & x & " is different") 'comment this out not to get the message
        End If
    Else
    End If

Next x
End Sub

推荐阅读