[英]How to “do loop” with a filtered range - VBA Excel
我有一個宏,它循環選定的過濾單元格范圍,並只為選定的單元格做一些東西。
此時,循環使用For Next,並帶有一個消息框,詢問用戶是否要繼續(是/否)。 我想用一個包含3個選項(上一個,下一個,退出)的消息框而不是兩個選項(是,否)來改變循環方法。 在這個新循環中,我將使用與過濾范圍中的單元格位置相關的計數器,並執行以下操作:
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
我會嘗試使用類似的東西(添加userform)
t=2
Do until t > rng.SpecialCells(xlCellTypeVisible).rows.count
Userform1.Show
‘Do something depending on button clicked on userform
t = t + 1
Loop
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.