Excel 如何确定工作表的数量?
使用VBA,是否可以确定工作簿中的工作表数量,并在这些工作表中预定义的单元格范围内执行计算 工作表定期添加到工作簿中,我们需要计算特定范围单元格的总和 我在谷歌上搜索了一下,但没有找到任何解决方案。循环浏览工作表和例外列表 新职位(2021年3月30日)Excel 如何确定工作表的数量?,excel,vba,Excel,Vba,使用VBA,是否可以确定工作簿中的工作表数量,并在这些工作表中预定义的单元格范围内执行计算 工作表定期添加到工作簿中,我们需要计算特定范围单元格的总和 我在谷歌上搜索了一下,但没有找到任何解决方案。循环浏览工作表和例外列表 新职位(2021年3月30日) 比如说,自从这个答案发布后,我学到了一两件事。因为这是我最受欢迎的作品之一,我决定添加一个改进(更正)来解决这个问题 以下问题: 引入一个范围,使解决方案更加操作(用户友好:。。。我们需要计算特定范围的单元格的总和 通过创建对工作簿的引用来
- 比如说,自从这个答案发布后,我学到了一两件事。因为这是我最受欢迎的作品之一,我决定添加一个改进(更正)来解决这个问题
以下问题:
- 引入一个范围,使解决方案更加操作(用户友好:。。。我们需要计算特定范围的单元格的总和
- 通过创建对工作簿的引用来限定工作表:
和Dim wb…
vswb.worksheets
worksheets
- 摆脱匈牙利符号:
vsExceptions
vntExceptions
- 将数组声明为字符串:
vs()声明为字符串
声明为变量
- 使用
vsApplication.Match
For j=0摆脱内部循环。。。下一个j
- 我将离开这篇旧文章,因为它在处理
循环、使用For
和检查方面都很有教育意义 如果计数器大于数组的上限,则返回计数器 确定是否已在数组中找到值Exit For
- 对包含此代码的工作簿中的所有工作表执行“此处编码””部分中的操作,
例外列表中的工作表除外
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
如果允许异常列表为空(“
”),请使用以下代码:
不允许有例外情况
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
- 如果您没有任何异常,即希望在所有工作表上执行操作,则只需将
保留为“”ceexceptions
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
下一步的方法
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
循环浏览带有例外列表的工作表
新职位(2021年3月30日)
- 比如说,自从这个答案发布后,我学到了一两件事。因为这是我最受欢迎的作品之一,我决定添加一个改进(更正)来解决这个问题
以下问题:
- 引入一个范围,使解决方案更加操作(用户友好:。。。我们需要计算特定范围的单元格的总和
- 通过创建对工作簿的引用来限定工作表:
和Dim wb…
vswb.worksheets
worksheets
- 摆脱匈牙利符号:
vsExceptions
vntExceptions
- 将数组声明为字符串:
vs()声明为字符串
声明为变量
- 使用
vsApplication.Match
For j=0摆脱内部循环。。。下一个j
- 我将离开这篇旧文章,因为它在处理
循环、使用For
和检查方面都很有教育意义 如果计数器大于数组的上限,则返回计数器 确定是否已在数组中找到值Exit For
- 对包含此代码的工作簿中的所有工作表执行“此处编码””部分中的操作,
例外列表中的工作表除外
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
如果允许异常列表为空(“
”),请使用以下代码:
不允许有例外情况
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
- 如果您没有任何异常,即希望在所有工作表上执行操作,则只需将
保留为“”ceexceptions
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
下一步的方法
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
尝试
ThisWorkbook.Sheets.Count
TryThisWorkbook.Sheets.Count