How to show a message box that displays name for each worksheet in the excel file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JonHuff
    New Member
    • Sep 2010
    • 14

    How to show a message box that displays name for each worksheet in the excel file

    I want this Access VBA to show a message box for each worksheet in the excel file. The MsgBox should say the worksheets name. HELP.

    Code:
    Sub ShowWorkSheets()
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        
    
    Set xlApp = New Excel.Application
    Set xlBook = xlApp.Workbooks.Open("L:\DOC-ADM\Business Analyst\Apr 08, FD SLA 2.xls")
    
      For Each xlSheet In xlBook
            MsgBox xlSheet.Name
    
        Next xlSheet
    
    End Sub
    Last edited by MMcCarthy; Oct 13 '10, 06:36 PM. Reason: adding code tags
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Change For Each xlSheet In xlBook to

    Code:
    For Each xlSheet In xlBook.Worksheets
    Mary

    Comment

    • hype261
      New Member
      • Apr 2010
      • 207

      #3
      Try this code instead.

      Code:
      Sub ShowWorkSheets()
          Dim xlApp As Excel.Application
          Dim xlBook As Excel.Workbook
          Dim xlSheet As Excel.Worksheet
        
        
          Set xlApp = New Excel.Application
          Set xlBook = xlApp.Workbooks.Open("L:\DOC-ADM\Business Analyst\Apr 08, FD SLA 2.xls")  
      
        For Each xlSheet In xlBook.Worksheets
              MsgBox xlSheet.Name
        
          Next xlSheet
        
      End Sub

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Snap :D

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Should you want all Worksheet Names to appear in a single Message Box:
          Code:
          Dim xlApp As Excel.Application
          Dim xlBook As Excel.Workbook
          Dim xlSheet As Excel.Worksheet
          Dim strBuild As String
          Const conPATH_TO_EXCEL_WORKSHEET As String = "L:\DOC-ADM\Business Analyst\Apr 08, FD SLA 2.xls"
            
            
          Set xlApp = New Excel.Application
          Set xlBook = xlApp.Workbooks.Open(conPATH_TO_EXCEL_WORKSHEET)
            
          For Each xlSheet In xlBook.Worksheets
            strBuild = strBuild & xlSheet.Name & vbCrLf
          Next xlSheet
          
          strBuild = Left$(strBuild, Len(strBuild) - 2)
          
          MsgBox strBuild, vbInformation, "Worksheets in " & conPATH_TO_EXCEL_WORKSHEET
          
          xlApp.Quit

          Comment

          Working...