【问题标题】:How to "do loop" with a filtered range - VBA Excel如何使用过滤范围“循环” - VBA Excel
【发布时间】:2019-06-14 16:57:31
【问题描述】:

我有一个宏,它在选定的过滤范围的单元格上循环,并只为选定的单元格做一些事情。

此时,循环是 For Next,带有一个消息框询问用户是否要继续(是/否)。 我想用一个包含 3 个选项(上一个、下一个、退出)而不是 2 个选项(是、否)的消息框来更改循环方法。 在这个新循环中,我将使用与过滤范围内的细胞系位置相关的计数器并执行以下操作:

   Case Previous : Counter = Counter - 1
   Case Next : Counter = Counter + 1
   Case Quit : Exit For

我面临的问题是我不知道如何轻松地使用计数器,因为我的 For Next 循环当前没有任何计数器,并且引用过滤范围的单元格位置并不容易。

关于如何进行的任何建议?

谢谢!

Sub newstuff()
    Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    Msg = "Do you want to continue ?"    ' Define message.
    Style = vbYesNo + vbCritical + vbDefaultButton2    ' Define buttons.
    Title = "MsgBox Demonstration"    ' Define title.
    Help = "DEMO.HLP"    ' Define Help file.
    Ctxt = 1000    ' Define topic
        ' context.
        ' Display message.
    Dim addr As String
    addr = Selection.Address 'Select a range of filtered cells in a column

    Dim cl As Range, rng As Range

    Set rng = Range(addr)

    For Each cl In rng.SpecialCells(xlCellTypeVisible)
        Debug.Print cl 'Do some stuff for each cell of the filtered range
    Response = MsgBox(Msg, Style, Title, Help, Ctxt)

        If Response = vbNo Then    ' User chose No.
          Exit For
        End If
    Next cl
End Sub

【问题讨论】:

  • 那么您将使用MsgBox "Message", vbYesNoCancel 还是创建userform

标签: excel vba filter


【解决方案1】:

我会尝试使用(添加用户表单)之类的东西

t=2

Do until t > rng.SpecialCells(xlCellTypeVisible).rows.count
Userform1.Show
‘Do something depending on button clicked on userform

t = t + 1

Loop

【讨论】:

    猜你喜欢
    • 2015-08-03
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多