首页 > 解决方案 > 如何确定工作表的数量?

问题描述

使用 VBA 是否有任何方法可以确定工作簿中的工作表数量并对这些工作表中预定义的单元格范围执行计算?

工作表会定期添加到工作簿中,我们需要计算特定范围的单元格的总和。

我用谷歌搜索但没有找到任何解决方案。

标签: excelvba

解决方案


循环遍历带有例外列表的工作表

新职位(2021 年 3 月 30 日)

  • 假设自从发布此答案以来,我已经学到了一两件事。由于它是我最受好评的之一,我决定添加一个改进(更正)来解决以下问题:
    • 引入一个范围以使解决方案对 OP(用户)更友好:......我们需要计算特定范围的单元格的总和......
    • 通过创建对工作簿的引用来限定工作表:Dim wb...wb.Worksheetsvs Worksheets
    • 摆脱匈牙利符号:Exceptionsvs vntExceptions
    • 将数组声明为字符串:() As Stringvs As Variant
    • Application.Match并通过使用vs摆脱内部循环For j = 0... Next j
  • 我将离开旧帖子,因为它正在工作(劣质)并且在处理For循环、使用Exit For和检查计数器是否大于数组的上限以确定是否在数组中找到值方面具有教育意义.
  • 对包含此代码的工作簿中的所有工作表执行“此处的代码Exception List”部分中的操作,但.

至少有一个例外

Option Explicit

Sub loopThroughWorksheets()
        
    Const ExceptionsList As String = "Sheet1,Sheet2"
    Const RangeAddress As String = "A1:E5"
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim Exceptions() As String: Exceptions = Split(ExceptionsList, ",")
    
    Dim ws As Worksheet
    
    For Each ws In wb.Worksheets
        If IsError(Application.Match(ws.Name, Exceptions, 0)) Then
            ' Code in here e.g.
            Debug.Print ws.Name, Application.Sum(ws.Range(RangeAddress))
        
        End If
    Next ws
    
End Sub

如果您允许Exceptions List为空 ( ""),请使用以下代码:

不允许有任何例外

Sub loopThroughWorksheets()
        
    Const ExceptionsList As String = "Sheet1,Sheet2"
    Const RangeAddress As String = "A1:E5"
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim Exceptions() As String: Exceptions = Split(ExceptionsList, ",")
    
    Dim wsNames() As String: ReDim wsNames(1 To wb.Worksheets.Count)
    Dim ws As Worksheet
    Dim n As Long
    
    If UBound(Exceptions) = -1 Then ' no exception: 'ExceptionList = ""'
        For Each ws In wb.Worksheets
            n = n + 1
            wsNames(n) = ws.Name
        Next ws
    Else
        For Each ws In wb.Worksheets
            If IsError(Application.Match(ws.Name, Exceptions, 0)) Then
                n = n + 1
                wsNames(n) = ws.Name
            End If
        Next ws
        ReDim Preserve wsNames(1 To n)
    End If
    
    For n = 1 To n
        Set ws = wb.Worksheets(wsNames(n))
      ' Code in here e.g.
        Debug.Print ws.Name, Application.Sum(ws.Range(RangeAddress))
    
    Next n
    
End Sub

旧帖(2018 年 12 月 30 日)

  • 对所有工作表执行“此处的代码”部分中的操作,例外逗号分隔列表中的工作表除外。
  • 两个版本的区别在于第一个版本使用对象控制变量ws,而第二个版本不需要它,而是使用控制变量i和集合的.Count属性。Worksheets
  • 如果您没有任何例外,即您想对所有工作表执行操作,则只需保留cExceptions""

For Each Next 方法

Sub WorksheetsForEach()

  ' Exceptions Comma-Separated List
  Const cExceptions As String = "Sheet1,Sheet2"
  
  Dim ws As Worksheet           ' Current Worksheet
  Dim vntExceptions As Variant  ' Exceptions Array
  Dim j As Integer              ' Exceptions Counter
    
  vntExceptions = Split(cExceptions, ",")
  
  For Each ws In Worksheets
    With ws
      For j = 0 To UBound(vntExceptions)
        If .Name = vntExceptions(j) Then
          Exit For
        End If
      Next
      If j > UBound(vntExceptions) Then
      ' Code in here e.g.
        Debug.Print .Name
      
      End If
    End With
  Next

End Sub

下一个方法

Sub WorksheetsForNext()

  ' Exceptions Comma-Separated List
  Const cExceptions As String = "Sheet1,Sheet2"
  
  Dim vntExceptions As Variant  ' Exceptions Array
  Dim i As Integer              ' Worksheets Counter
  Dim j As Integer              ' Exceptions Counter
    
  vntExceptions = Split(cExceptions, ",")
  
  For i = 1 To Worksheets.Count
    With Worksheets(i)
      For j = 0 To UBound(vntExceptions)
        If .Name = vntExceptions(j) Then
          Exit For
        End If
      Next
      If j > UBound(vntExceptions) Then
      ' Code in here e.g.
        Debug.Print .Name
      
      End If
    End With
  Next

End Sub

推荐阅读