VBA - EXCEL - 如何使用循环进行多次迭代,但只进行一次迭代?

VBA - EXCEL - 如何使用循环进行多次迭代,但只进行一次迭代?

使用下面的代码,我循环遍历工作表并创建一个数组。然后我将对数组执行一个操作,在本例中,它只是将 ws 名称输出到用户表单(用于测试目的)。一旦我弄清楚了循环,操作将是删除这些 ws。

两种情况是:1)可能有许多 ws 需要删除,因此创建数组(当前有 95 个 ws)。2)用户可能只想删除他们当前正在查看的 ws;活动工作表。

为什么不直接右键点击 ws 然后删除呢?因为我已经有一个用户窗体,其中包含许多选项来在屏幕上执行操作,所以我想也包括这个。只是不知道将这种灵活性融入代码有多难。

对于场景 1:

Option Explicit

Sub Del_WS_SeedAcct()
'Delete the individual seed account output worksheets, starting w/ "o_"

Dim wsX     As Variant
Dim ws      As Worksheet
Dim x       As Variant
Dim ArrayOSheets() As String
Dim wb      As Workbook:  Set wb = ThisWorkbook
Dim j       As Integer

'create array meeting criteria
For Each ws In ThisWorkbook.Worksheets
    ReDim Preserve ArrayOSheets(x)
    If ws.Name Like "o_*" Then            
        ArrayOSheets(x) = ws.Name
        x = x + 1
    End If
   j = j + 1
Next ws

'Action for testing
UserForm1.TextBox1.Text = "Total: " & x & vbCrLf & j & vbCrLf & Join(ArrayOSheets, vbCrLf)
UserForm1.Show vbModeless
UserForm1.TextBox1.SelStart = 0
End Sub  

这将返回正确的 95 个工作表名称。

对于场景 2:我尝试了这个,但是得到了“运行时错误 438。对象不支持此属性或方法。”我想这是有道理的。

if scenario = 2 then Set wsX = wb.ActiveSheet
if scenario = 1 then Set wsX = wb.Worksheets

For Each ws In wsX
    ReDim Preserve ArrayOSheets(x)
    If ws.Name Like "o_*" Then            
        ArrayOSheets(x) = ws.Name
        x = x + 1
    End If
   j = j + 1
Next ws

我以前做过类似的事情(在循环中重复使用动作以进行循环和单个实例),但它不是使用数组而是使用FOR计数循环。

如何才能让(技巧)这个FOR EACH循环仅在场景 2 的活动表上执行?

答案1

活动工作表在 FOR 循环中处于错误的位置。

if scenario = 2 then Set ws = wb.ActiveSheet


For Each ws In ThisWorkbook.Worksheets
    ReDim Preserve ArrayOSheets(x)
    If ws.Name Like "o_*" Then            
        ArrayOSheets(x) = ws.Name
        x = x + 1
    End If
   j = j + 1             'just a counter in this loop
Next ws

答案2

这可行...不完全是我想要的 - 实际上只是想重用 FOR EACH 循环中的现有代码。

'Userform
Private Sub CommandButton7_Click()
    If CheckBox1.Value = True Then
        Scenario = 1                        'active sheet only
    Else
        Scenario = 2
    End If
    Del_WS_SeedAcct (Scenario)
End Sub
Option Explicit

Sub Del_WS_SeedAcct(Optional ByRef Scenario As Integer)
'Delete the individual seed account output worksheets, starting w/ "o_"

'or then move to its own workbook
Dim ws      As Worksheet
Dim x       As Variant
Dim ArrayOSheets() As String
Dim wb      As Workbook:  Set wb = ThisWorkbook
Dim j       As Integer
Dim R       As String

If Scenario = 1 Then
    If ActiveSheet.Name Like "o_*" Then
        R = ActiveSheet.Name
    Else
        MsgBox "No output sheet selected"
        Exit Sub
    End If
Else
    For Each ws In ThisWorkbook.Worksheets
        ReDim Preserve ArrayOSheets(x)
        If ws.Name Like "o_*" Then
            ArrayOSheets(x) = ws.Name
            x = x + 1
        End If
       j = j + 1                    'used just to count the total # of WS
    Next ws
    R = Join(ArrayOSheets, vbCrLf)
End If

UserForm1.TextBox1.Text = "Total: " & x & vbCrLf _
    & "tot ws..." & j & vbCrLf _
    & "Scenario:  " & Scenario & vbCrLf & vbCrLf _
    & R 'Join(ArrayOSheets, vbCrLf)
UserForm1.Show vbModeless
UserForm1.TextBox1.SelStart = 0
End Sub

相关内容