我正在使用 vb6 并自动执行一些要在大量电子表格中执行的任务。
问题是设置我的工作表。我正在使用的工作簿上的大多数语法都是相同的,但有些工作表具有不同的名称。
当前使用
for x = 2 to cellcount
set worksheet = workbook.sheets("*typical sheet name*")
*rest of code
我想设置这样的东西
伪代码
for x = 2 to cellcount
On error goto errorhandler
set worksheet= workbook.sheets("*typical sheet name*")
Errorhandler:
set worksheet= workbook.sheets("*secondary sheet name*")
*rest of code
如何在 vb6 中完成此操作?
最佳答案
不要使用错误来控制逻辑流程,而是查看所有可用名称并选择您想要的名称:
Function GetBestMatchingSheet() As Worksheet
For Each GetBestMatchingSheet In ActiveWorkbook.Sheets
Select Case LCase$(GetBestMatchingSheet.Name)
Case "typical sheet name", "secondary sheet name", "third name"
Exit Function
End Select
Set GetBestMatchingSheet = Nothing
Next
End Function
...
dim foundSheet As Worksheet
set foundSheet = GetBestMatchingSheet()
If not foundSheet is Nothing then msgbox foundSheet.Name
关于vba - 更改vb6中的错误参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30734406/