gpt4 book ai didi

vba - 如果工作表存在,则运行 sub。如果没有,显示消息并退出子

转载 作者:行者123 更新时间:2023-12-04 21:15:04 35 4
gpt4 key购买 nike

尽管在其他线程上查找此主题,但我还是被卡住了。我需要以下帮助:

如果宏可以找到名为“Transfers”的工作表,我希望它激活该工作表并运行其余的子,这使得“Transfers”成为最后一个工作表。

如果它找不到名为“Transfers”的工作表,我想要它,所以发出一条消息(请确保您重命名了您的数据表:“Transfers”)并退出子。我下面的代码不起作用。

Sub Double_Transfer_Report()
Dim er As Boolean
er = False

On Error Resume Next
'Worksheets("Transfers").Activate
er = true

If er Then
MsgBox ("Please make sure that you renamed your data sheet : Transfers)
Exit Sub
End If

ActiveSheet.Move _
After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)

最佳答案

不要用错误对象做体操。只需通过循环检查工作表是否存在。

Sub Double_Transfer_Report()
Dim found As Boolean

found = SheetExists("Transfers")

If Not found Then
MsgBox "Please make sure that you renamed your data sheet : Transfers"
Else
ActiveSheet.Move After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)
End If
End Sub

Function SheetExists(strSheetName As String) As Boolean

Dim wks As Worksheet

For Each wks In ThisWorkbook.Worksheets

If wks.Name = strSheetName Then
SheetExists = True
Exit Function
End If
Next

SheetExists = False

End Function

关于vba - 如果工作表存在,则运行 sub。如果没有,显示消息并退出子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47190952/

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