Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何检查带有for循环的单元格中的值是否相等?_Excel_Vba_For Loop_Comparison - Fatal编程技术网

Excel 如何检查带有for循环的单元格中的值是否相等?

Excel 如何检查带有for循环的单元格中的值是否相等?,excel,vba,for-loop,comparison,Excel,Vba,For Loop,Comparison,我想检查单元格中的文本值是否与下面带有for循环的单元格中的文本值相同 如果单元格(1)和单元格(2)中的值不匹配,我希望单元格(3)中的值写入单元格(4) 我犯了一个错误 “溢出(错误6)” 我有一个生产输出和一个从早上6点到12点的时间表,我想创建一个时间表,如下所示 截图: 这里我使用的是一个字典,它将存储每个产品的每次逗号分隔,因此稍后将拆分它并取第一次和最后一次出现的内容: Sub TimeTable() 'Declare an array variable to s

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

如果单元格(1)和单元格(2)中的值不匹配,我希望单元格(3)中的值写入单元格(4)

我犯了一个错误

“溢出(错误6)”

我有一个生产输出和一个从早上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

在这里,我使用的是一个字典,它将每次存储的每个产品都用逗号分隔,因此稍后会将其拆分,并取第一次和最后一次出现的内容:

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
选项显式
子测试()
我长,我长,j长,我长
Dim StartTime As Date,EndTime As Date,strOutPut
j=0
使用此工作簿。工作表(“表1”)
LastRowA=.Cells(.Rows.Count,“A”).End(xlUp).Row
对于i=3至LastRowA
如果i>j-1那么
StartTime=.Range(“A”&i).Value
strOutPut=.Range(“U”&i).Value
对于j=i+1到最后一行a+1
如果strOutPut.Range(“U”&j).Value,则
EndTime=.Range(“A”&j-1).Value
LastRow=.Cells(.Rows.Count,“W”).End(xlUp).Row
.Range(“W”&LastRow+1)。值=开始时间
.Range(“X”&LastRow+1)。值=结束时间
.Range(“Y”&LastRow+1)。值=输出
退出
如果结束
下一个j
如果结束
接下来我
以
端接头
结果

您可以使用

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
选项显式
子测试()
我长,我长,j长,我长
Dim StartTime As Date,EndTime As Date,strOutPut
j=0
使用此工作簿。工作表(“表1”)
LastRowA=.Cells(.Rows.Count,“A”).End(xlUp).Row
对于i=3至LastRowA
如果i>j-1那么
StartTime=.Range(“A”&i).Value
strOutPut=.Range(“U”&i).Value
对于j=i+1到最后一行a+1
如果strOutPut.Range(“U”&j).Value,则
EndTime=.Range(“A”&j-1).Value
LastRow=.Cells(.Rows.Count,“W”).End(xlUp).Row
.Range(“W”&LastRow+1)。值=开始时间
.Range(“X”&LastRow+1)。值=结束时间
.Range(“Y”&LastRow+1)。值=输出
退出
如果结束
下一个j
如果结束
接下来我
以
端接头
结果


您的
a
变量未声明:
Dim a as Range
也未设置:
set a=单元格(3+i,1)
。同时声明
i
Long
,而不是
Integer
开始时,将
替换为i=1的行数。用a来计算
。不要使用
Integer
变量(导致溢出非常容易),而是使用
Long
。根据@Plution,也要使用更可靠的方法检索最后一行。此外,我建议至少使用一个工作表引用,并通过数组(通过内存)而不是单元格进行循环。话虽如此。请您提供一些模型样本数据和预期结果好吗?@Teamothy,我不认为
变量是范围对象。相反,我认为OP试图编写
Cells(228+j,3)=Cells(3+I,1)
@JvdV是的,我的错,他想要的是一个值而不是范围,采用你的方法会更容易。你的
a
变量没有声明:
将a作为范围变暗
也没有设置:
设置a=单元格(3+I,1)
。同时声明
i
Long
,而不是
Integer
开始时,将
替换为i=1的行数。用a来计算
。不要使用
Integer
变量(导致溢出非常容易),而是使用
Long
。根据@Plution,也要使用更可靠的方法检索最后一行。此外,我建议至少使用一个工作表引用,并通过数组(通过内存)而不是单元格进行循环。话虽如此。请您提供一些模型样本数据和预期结果好吗?@Teamothy,我不认为
变量是范围对象。相反,我认为OP试图编写
单元格(228+j,3)=单元格(3+I,1)
@JvdV是的,我的错,他想要的是一个值而不是范围,采用你的方法会更容易。