+ Reply to Thread
Results 1 to 5 of 5

How to check sheet Name exist or not in particuler workbook

Hybrid View

  1. #1
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    How to check sheet Name exist or not in particuler workbook

    Hi Experts,

    i have multiple folders and have multiple excel files in it.
    i am trying to open each folder and open each excel file
    when i open excel file of that particular folder i have extract some date from the sheet Name ("Enter Data").

    Problem is that some of the excel have no such type of sheets so i just want to know how do i check the sheet name "Enter Data") is available or not

    Please suggest.

    My Code is below

     For Each m In mFol.SubFolders
          sFileName = Dir(m.Path & "\*.xls*")
          
                  Do While Len(sFileName) > 0
                        
                      Workbooks.Open (m.Path & "\" & sFileName)
                      
                            Sheets("ENTER DATA").Select
                        
                      ActiveWorkbook.Close False
                  
                  
                  sFileName = Dir
                  Loop
      Next m

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to check sheet Name exist or not in particuler workbook

    You could use this UDF to find if a given workbook, has a sheet with a particular name.

    Function SheetExists(SheetName as String, optional wb as Workbook) As Boolean
        If wb Is Nothing then Set wb=ActiveSheet
    
        On Error Resume Next
        SheetExists =(LCase(wb.Sheets(sheetName).Name) = LCase(SheetName))
        On Error Goto O
    Exit Function
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513
    Quote Originally Posted by mohan.r1980 View Post
    Hi Experts,

    i have multiple folders and have multiple excel files in it.
    i am trying to open each folder and open each excel file
    when i open excel file of that particular folder i have extract some date from the sheet Name ("Enter Data").

    Problem is that some of the excel have no such type of sheets so i just want to know how do i check the sheet name "Enter Data") is available or not

    Please suggest.

    My Code is below

     For Each m In mFol.SubFolders
          sFileName = Dir(m.Path & "\*.xls*")
          
                  Do While Len(sFileName) > 0
                        
                      Workbooks.Open (m.Path & "\" & sFileName)
                      
                            Sheets("ENTER DATA").Select
                        
                      ActiveWorkbook.Close False
                  
                  
                  sFileName = Dir
                  Loop
      Next m
    Not test maybe like this
    sub test()
    Dim myfile as object,f as object
    Dim s as string 
    Dim fso as object,arr(),j as long
    Set fso = CreateObject("Scripting.FileSystemObject")
    s ="Enter Data"
    Set f =fso.Getfolder("your path")
    For each myfile in f.subfolders.files
       Dim wb as workbook,ws as worksheet
       Set wb = Workbooks.open(myfile)
         With wb
           If not Evaluate("isreff('" & s & "'!A1")") then
              j = j + 1
              Redim Preserve arr(1 to j)
              arr(j) = wb.name
           End if
          .close ,false
         End with
    Next myfile
        
    Dim v
      For each v in arr
       Msgbox "This file " & v & " Not have sheet Enter Data",64
      Next v
    End sub
    Last edited by daboho; 10-15-2018 at 09:54 AM.

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: How to check sheet Name exist or not in particuler workbook

    I'd do it like mikerickson, but using IsObject function instead of using text comparison.

    Ex:
    Function ExistSheet(Ws As String, Optional Wb As Workbook) As Boolean
        If Wb Is Nothing Then Set Wb = ActiveWorkbook
        On Error Resume Next
        ExistSheet = IsObject(Wb.Sheets(Ws))
    End Function
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  5. #5
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: How to check sheet Name exist or not in particuler workbook

    thanks for reply

    mikerickson and CK76 your solution working fine.

    thanks you very much to all.

+ 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] Check if sheet name already exist
    By Tpleme in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-13-2018, 09:39 AM
  2. Before run code Check first if sheet name exist or not
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-22-2017, 06:19 AM
  3. [SOLVED] Check First IF Sheet Is Exist With The Name Of Cell K7 If exist Then run the code
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-18-2015, 10:08 AM
  4. [SOLVED] check column headers against sheet names if sheet not exist create it
    By AkaTrouble in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2015, 05:59 PM
  5. Function to check the sheet name exist or not based on sheet values
    By narendrabr in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-12-2014, 09:37 AM
  6. macro to check if a value exist in sheet and add it if it doesnt
    By RetroGun in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-01-2011, 02:35 AM
  7. macro to check if a value exist in sheet and add it +other parts of row if it doesnt
    By RetroGun in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2011, 07:14 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