2021年1月18日星期一

Check checkbox value only once inside a nested loop

I am checking the value of a checkbox on my userform inside a nested loop. I want to know if there is a way to do this before the loops because basically the code is running the same check over and over again...

what it is doing is it loops through the entire column B and combines all the rows that have the same conditions. then it puts in into an array and prints. So I need to do this checkbox check twice. Any help is appreciated!

            For Each aCell In .Range("B2:B" & LastRow)                                'First check for the checkbox value                  If UserForm1.MPCheck1.Value = True Then                      UniqueCombo = aCell.Value & "," & aCell.Offset(0, 1) & aCell.Offset(0, 2)                  Else                      UniqueCombo = aCell.Value & "," & aCell.Offset(0, 1) & aCell.Offset(0, 2) & "," & aCell.Offset(0, 5)                  End If                            DieCoordinate = aCell.Value & "," & aCell.Offset(0, 1)              SheetName = aCell.Offset(0, 2) & "-" & aCell.Offset(0, 5)                            If Not dict.exists(UniqueCombo) Then                      VarLastRow = ThisWorkbook.Worksheets(SheetName).Cells(.Rows.Count, "E").End(xlUp).row + 1                      ThisWorkbook.Worksheets(SheetName).Cells(VarLastRow, 5) = DieCoordinate                      dict(UniqueCombo) = True 'add this value                                                            For o = 2 To LastRow                                                    'Second check for the checkbox value                          If UserForm1.MPCheck1.Value = True Then                              VariableCombo = .Cells(o, 2) & "," & .Cells(o, 3) & .Cells(o, 4)                          Else                              VariableCombo = .Cells(o, 2) & "," & .Cells(o, 3) & .Cells(o, 4) & "," & .Cells(o, 7)                          End If                                                VariableCombo = .Cells(o, 2) & "," & .Cells(o, 3) & .Cells(o, 4) & "," & .Cells(o, 7)                                                                If UniqueCombo = VariableCombo And .Cells(o, 6).Interior.ColorIndex = -4142 And _                              .Cells(o, 6) <> "*" And .Cells(o, 6) <> "0" And .Cells(o, 6) <> "" Then                                    ReDim Preserve LArr(UBound(LArr) + 1)                                  LArr(X) = .Cells(o, 6)                                  X = X + 1                                      End If                            Next                  Next aCell  
https://stackoverflow.com/questions/65784587/check-checkbox-value-only-once-inside-a-nested-loop January 19, 2021 at 10:06AM

没有评论:

发表评论