Consulting

Results 1 to 9 of 9

Thread: Check sheet names and display MsgBox

  1. #1

    Arrow Check sheet names and display MsgBox

    following are the sheet names in the workbook...

    chk-fares, fares, rules, zones, temp, sheet1, temp-zones
    from the above given sheets, 3 sheets (names given below) are always constant....
    fares, rules, zones

    workbook may contain any number of sheets and by any names. but if any 1 or 2 or all the 3 constant sheets found in the workbook, then show msgbox as="yes ap-temp" else show msgbox as "not ap-temp"
    Last edited by brettdj; 05-31-2005 at 02:29 AM. Reason: Please do not use capitals again.

  2. #2
    VBAX Contributor Richie(UK)'s Avatar
    Joined
    May 2004
    Location
    UK
    Posts
    188
    Location
    Hi,

    Please don't SHOUT when you post questions. Thanks
    (Edit : I see the post has been amended for you)

    You could try making a function that will return True or False depending upon whether the workbook contains one of the specified worksheets. Something like this perhaps:
    Sub test()
        Dim varWSNames As Variant
    varWSNames = Array("FARES", "RULES", "ZONES")
        MsgBox "Does the workbook contain one of the specified sheets?" & vbNewLine & _
            SheetsExist(varWSNames)
    End Sub
    
    Function SheetsExist(varSheetNames As Variant, Optional wbk As Workbook) As Boolean
        Dim lCnt As Long, ws As Worksheet
    SheetsExist = False
        If wbk Is Nothing Then Set wbk = ActiveWorkbook
    For lCnt = LBound(varSheetNames) To UBound(varSheetNames)
            On Error Resume Next
            Set ws = wbk.Worksheets(varSheetNames(lCnt))
            On Error GoTo 0
            If Not ws Is Nothing Then
                SheetsExist = True
                Exit For
            End If
        Next lCnt
    End Function
    HTH

  3. #3
    some or the other way its working fine...thanku

    let me check to my requirement and then I will be back.

  4. #4
    Yes it is ok.
    But now actually my need is to check if all the sheet exist which is stored in arrays.

    At present your code is checking if any of the sheet exist. I want true answer only if all the sheets exist.

  5. #5
    VBAX Contributor Richie(UK)'s Avatar
    Joined
    May 2004
    Location
    UK
    Posts
    188
    Location
    Hi,

    Like this?

    Sub test()
        Dim varWSNames As Variant
    varWSNames = Array("FARES", "RULES", "ZONES")
        MsgBox "Does the workbook contain all of the specified sheets?" & vbNewLine & _
        AllSheetsExist(varWSNames)
    End Sub
     
    Function AllSheetsExist(varSheetNames As Variant, Optional wbk As Workbook) As Boolean
        Dim lCnt As Long, ws As Worksheet
    AllSheetsExist = True
        If wbk Is Nothing Then Set wbk = ActiveWorkbook
    For lCnt = LBound(varSheetNames) To UBound(varSheetNames)
            On Error Resume Next
            Set ws = wbk.Worksheets(varSheetNames(lCnt))
            On Error GoTo 0
            If ws Is Nothing Then
                AllSheetsExist = False
                Exit For
            End If
            Set ws = Nothing
        Next lCnt
    End Function
    HTH

  6. #6
    Exactly,

    Fantastic. Thanku.

  7. #7
    if found true, how to import sheets in new workbook.

    import only which matches the names containing in arrays.

  8. #8
    ok i hv done myself. thanku..

  9. #9

Posting Permissions

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