VBA消息框用于处理筛选的数据选择

时间:2017-11-14 13:36:41

标签: excel vba excel-vba

我正在尝试创建一个可以在使用自动过滤器的工作表上运行的宏。在理想的世界中,我的宏应该显示一个消息框,它提供了三个选项:

1)在第一个可见行(忽略标题)上运行一组特定的VBA指令(在下面的代码中为彩色单元格B2),然后移动到下一个可见行并再次显示消息框。 /> 2)跳过此行,找到下一个可见行并再次显示消息框 3)退出宏。

我有下面的宏的骨头但是我觉得我在按下前两个按钮后再次显示一些聪明的方式再次显示消息框。另外,我不相信我的代码可以结束宏。

仅供参考:消息框而非扁平循环宏的原因是过滤器会定期更改,我希望减少基于必要过滤器重写代码的需要。

Sub Msg_exe()
    Dim Option_Menu As Integer
    Dim strMsg As String
    Dim strTitle As String

    Range("B2").Select
    strMsg = "Continue with this row"
    strTitle = "Alert"

    Option_Menu = MsgBox(strMsg, vbYesNoCancel + vbQuestion, strTitle)

    Select Case Option_Menu
        Case 6 'code to colour the cell goes here
            Selection.Font.ColorIndex = 25
            Selection.Interior.ColorIndex = 33
            ActiveCell.Offset(1, 0).Activate
            Do While ActiveCell.EntireRow.Hidden = True
                ActiveCell.Offset(1, 0).Activate
            Loop
            'I need some code to show the message box again ready for the next row

        Case 7 'code to skip to the next visable line goes here
            ActiveCell.Offset(1, 0).Activate
            Do While ActiveCell.EntireRow.Hidden = True
                ActiveCell.Offset(1, 0).Activate
            Loop
            'I need some code to show the message box again ready for the next row

        Case 2 'the code to end the macro goes here (I hope this is correct)
            End 
    End Select
End Sub

1 个答案:

答案 0 :(得分:0)

你可以循环开始行,从你决定的任何一行开始,并确定此后是否需要计算(我没有经过测试,所以请先检查这符合你的要求):

Sub Msg_exe()

    Dim cur_row as long
    Dim Option_Menu As Integer
    Dim strMsg As String
    Dim strTitle As String

    For cur_row = 2 to Range("A65000").End(xlUp).Row 'Modify this row referenced to suit

        if not Range("B" & cur_row).EntireRow.Hidden then

            Range("B" & cur_row).Select
            strMsg = "Continue with this row"
            strTitle = "Alert"

            Option_Menu = MsgBox(strMsg, vbYesNoCancel + vbQuestion, strTitle)

            Select Case Option_Menu
                Case 6 'code to colour the cell goes here
                    Selection.Font.ColorIndex = 25
                    Selection.Interior.ColorIndex = 33
                Case 7 'code to skip to the next visible line goes here
                Case 2 'the code to end the macro goes here (I hope this is correct)
                    Exit Sub
            End Select

        End If

    Next

End Sub