嵌套循环和无限循环麻烦

时间:2019-05-24 18:00:23

标签: excel vba

我无法遍历列中的单元格。当使用注释掉的If语句而不是其下面的“ Exit Do”时,我开始并无限循环。由于某些原因,即使使用“ cell.Offset()。Select”,我的代码也不会在单元格中向下移动。运行代码时,它只返回第一个框,而不是选中每个框直到它变成一个空框,然后离开“ Do循环”以再次启动“ for循环”。

'Macro is designed to Fill in blank spaces in column "F" depending on Passed, Non-Executed, or Failed Steps.
'Should fill in "F" if there exists an "F" in the filled in steps under it.
'Fills in "NE" if there is one present and no "F" present
'Fills in "P" if all else fails
Sub AutoFill()

Dim rng As Range, cell As Range
'Setting Variables, cell is used as current cell and rng is used as complete column of cells to check

Dim pf As String
'Variable that is assigned the cell values to check

'Set rng = Application.InputBox("Select a range", "Get Range", Type:=8)
Set rng = Range("F36:F66")
'Range setters, top one is input by the user once ran. It is input like follows '$Column$RowStart:$Column$RowEnd

For Each cell In rng
'Loops through set range and check each cell for the following

    If IsEmpty(cell) = True And IsEmpty(Range("B" & cell.Row)) = True Then
    'Checks if cell is empty and if the cell in the same row and column "B"

        If IsEmpty(cell.Offset(1, 0)) = False Then
        'Checks if cell under 'current cell' is empty

            Dim TrackedCell As Range
            Set TrackedCell = cell
            'Creates variable that marks 'current empty cell'

            pf = cell.Offset(1, 0).Value
            'Assigns cell values of the cell under current cell to variable

            cell.Offset(1, 0).Select
            'Moves down 1 cell


            'MsgBox "The value is" & TrackedCell.Value


            Do
            'Creates loop


            'MsgBox "Loop starts"



                If IsEmpty(TrackedCell) = True And pf = "F" Or (TrackedCell.Value = "P" And pf = "F") Or (TrackedCell.Value = "NE" And pf = "F") Then
                'Checks if 'current empty cell' is still empty and if current cell value equals "F"
                    TrackedCell.Value = "F"
                    TrackedCell.Interior.ColorIndex = 6
                    'Sets 'current empty cell' to "F" and highlights cell
                    'MsgBox "If works"




                ElseIf (IsEmpty(TrackedCell) = True And pf = "NE") Or (TrackedCell.Value = "P" And pf = "NE") Then
                'Checks if 'current empty cell' is still empty and if current cell value equals "NE"
                    TrackedCell.Value = "NE"
                    TrackedCell.Interior.ColorIndex = 6
                    'Sets 'current empty cell' to "NE" and highlights cell




                ElseIf IsEmpty(TrackedCell) = True And pf = "P" Then
                'Checks if 'current empty cell' is still empty and if current cell value equals "P"
                    TrackedCell.Value = "P"
                    TrackedCell.Interior.ColorIndex = 6
                    'Sets 'current empty cell' to "P" and highlights cell
                    'Exit Do
                    'exits loop
                End If

                'MsgBox "What now"

                pf = cell.Offset(1, 0).Value
                cell.Offset(1, 0).Select
                'Moves down 1 cell

                'MsgBox "Should have moved down a cell"
                'If IsEmpty(pf) = True Then
                    'MsgBox "Exit If Used"
                    'Exit Do
                'End If
                'exits loop
                Exit Do
                'MsgBox "Not Exit If"
            Loop Until IsEmpty(pf) = True
            'Loops through range checking and moving down cells until the next empty cell is found


        End If
    End If
Next cell
End Sub

我需要MsgBoxes在运行时检查程序中哪里有问题。

进一步解释。我正在尝试在充满“ F”,“ P”和“ NE”的列中填充一个空单元格。这个想法是,如果该列中的该空白单元格与同一列中的下一个空白单元格之间存在一个“ F”,则最上面的空白单元格应放置一个“ F”。 “ NE”也有相同的想法,但“ F”将胜过“ NE”。如果在空单元格下面的完整单元格中都不存在,则将其置于“ P”。基本上已经完成了测试,并将测试分为几类。如果一项测试失败或未在类别中执行,则将整个项目标记为其各自的标签。

在下面的表格中,我有一个外观示例。我正在尝试根据宏下的过程使宏自动填充子测试或超级测试的空白框。

Excel test sheet

0 个答案:

没有答案