gpt4 book ai didi

excel - 测试或检查工作表是否存在

转载 作者:行者123 更新时间:2023-12-01 16:16:42 28 4
gpt4 key购买 nike

Dim wkbkdestination As Workbook
Dim destsheet As Worksheet

For Each ThisWorkSheet In wkbkorigin.Worksheets
'this throws subscript out of range if there is not a sheet in the destination
'workbook that has the same name as the current sheet in the origin workbook.
Set destsheet = wkbkdestination.Worksheets(ThisWorkSheet.Name)
Next

基本上,我循环遍历源工作簿中的所有工作表,然后将目标工作簿中的 destsheet 设置为与源工作簿中当前迭代的工作表同名的工作表。

如何测试该工作表是否存在?像这样的东西:

If wkbkdestination.Worksheets(ThisWorkSheet.Name) Then 

最佳答案

有些人不喜欢这种方法,因为错误处理的“不恰当”使用,但我认为它在 VBA 中被认为是可以接受的...另一种方法是循环遍历所有工作表,直到找到匹配项。

Function WorksheetExists(shtName As String, Optional wb As Workbook) As Boolean
Dim sht As Worksheet

If wb Is Nothing Then Set wb = ThisWorkbook
On Error Resume Next
Set sht = wb.Sheets(shtName)
On Error GoTo 0
WorksheetExists = Not sht Is Nothing
End Function

关于excel - 测试或检查工作表是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6688131/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com