如果已经存在此床单(iGeneral,iinput,iresult),我该如何停止制作新床单



vba代码通过文件夹中的所有excel文件,并检查是否存在某些特定纸张。但是,如果我第二次运行代码,它会创建新的床单。我该如何停止?

Sub LoopAllExcelFilesInFolder()
    Dim wb As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
    Dim FldrPicker As FileDialog
    Dim CurrentSheetName As String
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    With FldrPicker
        .Title = "Select A Target Folder"
        .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & ""
    End With
NextCode:
    myPath = myPath
    If myPath = "" Then GoTo ResetSettings
    myExtension = "*.xls*"
    myFile = Dir(myPath & myExtension)
    Do While myFile <> ""
        Set wb = Workbooks.Open(Filename:=myPath & myFile)
        DoEvents
        CurrentSheetName = ActiveSheet.Name         
        Sheets.Add    
        On Error Resume Next     
        ActiveSheet.Name = "IGeneral"               
        CurrentSheetName = ActiveSheet.Name
        Sheets(CurrentSheetName).Select
        Sheets.Add      
        On Error Resume Next   
        ActiveSheet.Name = "IInput"         
        Sheets(CurrentSheetName).Select
        CurrentSheetName = ActiveSheet.Name
        Sheets.Add    
        On Error Resume Next     
        ActiveSheet.Name = "IResult"            
        Sheets(CurrentSheetName).Select                    
        wb.Close SaveChanges:=True              
        DoEvents
        myFile = Dir
    Loop
    MsgBox "Task Complete!"
ResetSettings:
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

如果要确定是否存在表格,并且做某件事是否存在/不存在,那么您只需要确定是否有评估指定纸中的第一个单元格时的错误,例如:

If IsError(Evaluate("IGeneral!A1")) AND IsError(Evaluate("IInput!A1")) AND IsError(Evaluate("IResult!A1")) Then 'IsError returns boolean
    'Do something when there is an error (>=1 sheets DO NOT exist)
Else
    'Do something when there is no error (all sheets DO exist)
End If

关于您的一般编码,请在帖子下面查看 @pᴇʜ的评论,因为那里有很多重要且有用的信息。


edit1:添加表格名称(igeneral,iinput和iresult(

您的所有其他代码甚至可以进入True方案,因此仅在一个/任何床单不存在时才运行。

最新更新