选择除两张表之外的所有表。

huangapple go评论80阅读模式
英文:

Select all sheets except two

问题

  1. 需要选择工作簿中的所有工作表(工作表编号和名称会有所变化),除了两个名为“Overview”和“Index”的工作表之外(它们恰好是标签列表中最左边的工作表)。
  2. 是否有“通用”代码可以做到这一点,而不必为每个工作表命名(除了我不想选择的两个工作表)?
  3. 我尝试了以下代码,看看是否可以选择除一个之外的所有工作表:
  4. ```vba
  5. Sub Macro1()
  6. Dim i As Long
  7. Sheet1.Select
  8. For i = 2 To ThisWorkbook.Sheets.Count
  9. If Sheets(i).Name <> "Overview" Then Sheets(i).Select Replace:=False
  10. Next i
  11. End Sub

我得到了:

运行时错误 '1004'

当我点击调试时,它会突出显示 Sheet1.Select 行。

  1. <details>
  2. <summary>英文:</summary>
  3. I need to select all sheets (number and sheet names will vary) in a workbook except two sheets named &quot;Overview&quot; and &quot;Index&quot; (which also happen to be the left-most sheets on the tab list).
  4. Is there &quot;generic&quot; code that can do this without naming each sheet (other than the two sheets that I do not want selected)?
  5. I tried the following code to see if I could select all sheets except one:
  6. ```vba
  7. Sub Macro1()
  8. Dim i As Long
  9. Sheet1.Select
  10. For i = 2 To ThisWorkbook.Sheets.Count
  11. If Sheets(i).Name &lt;&gt; &quot;Overview&quot; Then Sheets(i).Select Replace:=False
  12. Next i
  13. End Sub

I get:

> run-time error '1004

When I click debug, it highlights the Sheet1.Select line.

答案1

得分: 1

尝试一下:

  1. 子宏1()
  2. Dim iSel As Long, ws As Worksheet
  3. 对于每个 ws ThisWorkbook.Worksheets
  4. 如果 IsError(Application.Match(ws.Name, _
  5. Array("概览", "索引"), 0)) Then
  6. ws.Select Replace:=(iSel = 0) '仅在选择的第一个工作表中进行替换
  7. iSel = iSel + 1 '增加已选择工作表计数
  8. End If
  9. 下一个 ws
  10. 结束子

(假设没有隐藏的工作表)

英文:

Try this:

  1. Sub Macro1()
  2. Dim iSel As Long, ws As Worksheet
  3. For Each ws In ThisWorkbook.Worksheets
  4. If IsError(Application.Match(ws.Name, _
  5. Array(&quot;Overview&quot;, &quot;Index&quot;), 0)) Then
  6. ws.Select Replace:=(iSel = 0) &#39;only Replace for first-selected sheet
  7. iSel = iSel + 1 &#39;increment selected sheet count
  8. End If
  9. Next ws
  10. End Sub

(assumes no hidden sheets)

答案2

得分: 1

以下是代码部分的翻译:

  1. Sub SelectWS()
  2. Dim WS As Worksheet
  3. Dim I As Long
  4. Dim N As Long
  5. Dim Fnd As Boolean
  6. Dim Vis As Boolean
  7. N = 0
  8. For Each WS In ThisWorkbook.Worksheets
  9. Vis = (WS.Visible = xlSheetVisible)
  10. If Vis = False Then N = N + 1
  11. If WS.Name &lt;&gt; &quot;Overview&quot; And WS.Name &lt;&gt; &quot;Index&quot; And Vis Then
  12. Fnd = True
  13. If ActiveSheet.Name = &quot;Overview&quot; Or ActiveSheet.Name = &quot;Index&quot; Then
  14. WS.Activate
  15. WS.Select
  16. Else
  17. WS.Select (False)
  18. End If
  19. End If
  20. Next WS
  21. If Not Fnd Then
  22. MsgBox &quot;No suitable WS found.&quot;, vbInformation + vbOKOnly, &quot;Error:&quot;
  23. ElseIf N &gt; 0 Then
  24. MsgBox &quot;Found &quot; &amp; N &amp; &quot; hidden Worksheet(s) - not selectable.&quot;, vbInformation + vbOKOnly, &quot;Notice:&quot;
  25. End If
  26. End Sub
英文:

Here's an option.

  1. Sub SelectWS()
  2. Dim WS As Worksheet
  3. Dim I As Long
  4. Dim N As Long
  5. Dim Fnd As Boolean
  6. Dim Vis As Boolean
  7. N = 0
  8. For Each WS In ThisWorkbook.Worksheets
  9. Vis = (WS.Visible = xlSheetVisible)
  10. If Vis = False Then N = N + 1
  11. If WS.Name &lt;&gt; &quot;Overview&quot; And WS.Name &lt;&gt; &quot;Index&quot; And Vis Then
  12. Fnd = True
  13. If ActiveSheet.Name = &quot;Overview&quot; Or ActiveSheet.Name = &quot;Index&quot; Then
  14. WS.Activate
  15. WS.Select
  16. Else
  17. WS.Select (False)
  18. End If
  19. End If
  20. Next WS
  21. If Not Fnd Then
  22. MsgBox &quot;No suitable WS found.&quot;, vbInformation + vbOKOnly, &quot;Error:&quot;
  23. ElseIf N &gt; 0 Then
  24. MsgBox &quot;Found &quot; &amp; N &amp; &quot; hidden Worksheet(s) - not selectable.&quot;, vbInformation + vbOKOnly, &quot;Notice:&quot;
  25. End If
  26. End Sub

huangapple
  • 本文由 发表于 2023年2月18日 08:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定