+ Reply to Thread
Results 1 to 4 of 4

Macro that runs when a sheetname in a file does not exist

Hybrid View

  1. #1
    Registered User
    Join Date
    02-14-2017
    Location
    Hilversum, Netherlands
    MS-Off Ver
    2010
    Posts
    92

    Macro that runs when a sheetname in a file does not exist

    Hi Guys!

    I am looking for a solution to a problem i'm facing in the code below I want to import data from a file if the file name contains "Test". If the filename contains "Test" VBA should unprotect the sheets and look for a sheet called "Check test".

    This is where the problem starts. Not every file called "Test" has a sheet called check test. I would like the macro to move on to the next workbook (and preferably give a message) if this sheet does not exist in the file. Please help out!!

    Sub Test_import()
    
    Dim wb As Workbook
    Dim wbName As String
    Dim Password As String
    Dim ws As Worksheet
    Dim activewrkbook As Workbook
    Dim lSecurity As Long
    
    'Change this line if you want to activate workbooks with a different name that are opened
    wbName = "test"
    Password = "DUMMY"
    
    Application.ScreenUpdating = False
    
    
    Workbooks("Certificates file.3.xlsm").Activate
    
    'Start of the For Each loop that checks all opened workbooks containing "wbName"
    For Each wb In Application.Workbooks
       
        
    
                    
                If LCase(wb.Name) Like "*" & wbName & "*" Then
                        Debug.Print wb.Name
                        wb.Activate
                                            
                    Application.DisplayAlerts = False
                
                    Set activewrkbook = ActiveWorkbook
                    
                'When there's an error go to the bottom error message
                'On Error GoTo errormsg
           
                        'On Error Resume Next
                        ActiveWorkbook.Unprotect Password:=Password
                        
                'Protect each sheet in the active workbook
                    For Each ws In ActiveWorkbook.Worksheets
                        
                        ws.Unprotect Password:=Password
                    Next ws
                        
                    
                    activewrkbook.Activate
                                 
    '            If WorksheetExists2("Check sheet") Then
                
                        Sheets("Check Test").Activate
                        If Err <> 0 Then
                
                'Check if cell B2 contains the text "Niet geslaagd"
                        If activewrkbook.Sheets("Check test").Range("B2").Value = "Niet geslaagd" Then
                        
                
                            MsgBox "Participant did not pass the test. Please manually review the test."
                            
                            
                        
                        ElseIf activewrkbook.Sheets("Check test").Range("B2").Value = 1 Then
                            
                            Sheets("Check test").Range("B5:J5").Copy
                            
                            Workbooks("Certificates file.3.xlsm").Activate
                            Sheets("Input sheet").Activate
                                
                            Range("B2").Select
                            
                            If ActiveCell.Offset(1, 0).Value = "" Then
                            
                                ActiveCell.Offset(1, 0).Select
                                ActiveCell.PasteSpecial xlPasteValuesAndNumberFormats
                                
                                Range("A2").Select
                                 
                            Else
                            
                                Selection.End(xlDown).Select
                                ActiveCell.Offset(1, 0).Select
                                
                                ActiveCell.PasteSpecial xlPasteValuesAndNumberFormats
                                
                                Range("A2").Select
                                
                                
                            End If
                        
                Else
                        
                        End If
                    
                         'Closes the active workbook
                '        If activewrkbook.Name Like "*" & wbName & "*" And activewrkbook.Sheets("Check Test").Range("B2").Value <> "Niet geslaagd" Then
                       ' If activewrkbook.Name Like "*" & wbName & "*"
                
                        If LCase(activewrkbook.Name) Like "*" & wbName & "*" And activewrkbook.Sheets("Check Test").Range("B2").Value = 1 Then
                        
                            activewrkbook.Close
                        
                        
                        Else
                        
                        End If
                        
                    End If
    
    nxt:
    Next wb
    
    
            MsgBox "Data has been imported"
             
            On Error Resume Next
            Workbooks("Certificates file").Worksheets("Input sheet").Activate
        
            Application.DisplayAlerts = True
            Application.ScreenUpdating = True
            
            Exit Sub
                
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Macro that runs when a sheetname in a file does not exist

    You have already a test done to check if the worksheet exit
    If WorksheetExists2("Check sheet") Then
    What do you need more ???
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    02-14-2017
    Location
    Hilversum, Netherlands
    MS-Off Ver
    2010
    Posts
    92
    Quote Originally Posted by PCI View Post
    You have already a test done to check if the worksheet exit
    If WorksheetExists2("Check sheet") Then
    What do you need more ???
    This doesn’t seem to work for Some reason...

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Macro that runs when a sheetname in a file does not exist

    Where the function WorksheetExists2("Check sheet") is ???
    Else another one

    Option Explicit
    
    Function IsSheetExists1(ByVal SheetName As String) As Boolean
    '  Only test on Sheet's name
        On Error Resume Next
        IsSheetExists1 = Len(Sheets(SheetName).Name)
        On Error GoTo 0
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] if file exist macro problem
    By etuzgen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-18-2014, 06:41 AM
  2. [SOLVED] Save as macro; File allready exist
    By tigergutt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-02-2013, 09:15 AM
  3. Have macro over write existing file from control button if file exist
    By pjbassdc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-30-2011, 10:35 AM
  4. When open File A, runs macro in File B
    By Wizz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-28-2007, 06:27 AM
  5. Replies: 3
    Last Post: 05-15-2006, 05:00 AM
  6. Replies: 3
    Last Post: 03-11-2006, 02:50 PM
  7. Replies: 0
    Last Post: 03-10-2006, 05:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1