如何使用 for 循环检查单元格中的值是否相等?

How to check for equal values in cells with a for loop?

我想使用 for 循环检查单元格中的文本值是否与下面单元格中的文本值相同。

如果 Cell(1) 和 Cell(2) 中的值不匹配,我想将 Cell(3) 中的值写入 Cell(4)。

我收到一个错误

"Overflow (Error 6)"

Dim i As Integer

For i = 1 To Rows.Count

    If Cells(2 + i,21) = Cells(3 + i,21) Then
        i = i + 1
    Else
        a = Cells(3 + i, 1)
        j = j + 1
        Cells(228 + j, 3) = a
    End If

Next i

End Sub

我有一个生产输出和一个从早上 6 点到凌晨 12 点的时间表,我想创建一个如下所示的时间表。

截图:

这里我使用了一个字典,它每次都会存储每个以逗号分隔的产品,所以稍后会拆分它并取第一次和最后一次出现:

Sub TimeTable()

        'Declare an array variable to store the data
        'change MySheet for your sheet name
        arr = ThisWorkbook.Sheets("MySheet").UsedRange.Value 'this will store the whole worksheet, the used area.

        'Declare a dictionary object
        Dim Products As Object: Set Products = CreateObject("Scripting.Dictionary")

        'Loop through the array
        Dim i As Long
        For i = 3 To UBound(arr) 'start from row 3 because of your screenshoot
            If arr(i, 21) = vbNullString Then GoTo NextRow 'if column U is empty won't add anything
            If Not Products.Exists(arr(i, 21)) Then '21 is the column index for column U
                Products.Add arr(i, 21), arr(i, 1)
            Else
                Products(arr(i, 21)) = arr(i, 21) & "," & arr(i, 1)
            End If
NextRow:
        Next i
        Erase arr

        'Redim the array to fit your final data, 4 columns and as many rows as products
        ReDim arr(1 To Products.Count + 1, 1 To 4)

        'Insert the headers
        arr(1, 1) = "Time"
        arr(1, 4) = "Product / Error"

        'Now loop through the dictionary
        Dim Key As Variant, MySplit As Variant
        i = 2
        For Each Key In Products.Keys
            MySplit = Split(Products(Key), ",")
            arr(i, 1) = MySplit(LBound(MySplit))
            arr(i, 2) = "-"
            arr(i, 3) = MySplit(UBound(MySplit))
            arr(i, 4) = Key
            i = i + 1
        Next Key


        'I don't know where are you going to paste your data, so I'm making a new worksheet at the end of your workbook
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
        With ws
            .Range("A1").Resize(UBound(arr), UBound(arr, 2)).Value = arr
            .Range("A1:C1").Merge
        End With

End Sub

你可以使用

Option Explicit

Sub test()

    Dim LastRowA As Long, i As Long, j As Long, LastRowW As Long
    Dim StartTime As Date, EndTime As Date, strOutPut

    j = 0

    With ThisWorkbook.Worksheets("Sheet1")

        LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 3 To LastRowA

            If i > j - 1 Then

                StartTime = .Range("A" & i).Value
                strOutPut = .Range("U" & i).Value

                For j = i + 1 To LastRowA + 1

                    If strOutPut <> .Range("U" & j).Value Then

                        EndTime = .Range("A" & j - 1).Value
                        LastRow = .Cells(.Rows.Count, "W").End(xlUp).Row

                        .Range("W" & LastRow + 1).Value = StartTime
                        .Range("X" & LastRow + 1).Value = EndTime
                        .Range("Y" & LastRow + 1).Value = strOutPut

                        Exit For

                    End If

                Next j

            End If

        Next i

    End With

End Sub

结果