XL: Distinguishing Sheet Types with Visual Basic Macro

Last reviewed: February 3, 1998
Article ID: Q108350

The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

SUMMARY

You can use the Microsoft Visual Basic for Applications TypeName function to return each type of sheet available in Microsoft Excel. Because the Worksheet type can apply to worksheets, MS Excel 4.0 Macro sheets, or MS Excel 4.0 International Macro sheets, you must use the Type property along with the TypeName function to return the specific type of worksheet.

The sample macro (Sub procedure) in the "More Information" section of this article displays the appropriate sheet name for each sheet:

   Chart
   DialogSheet
   Module (except in Microsoft Excel 97 and Microsoft Excel 98)
   MS Excel 4.0 Macro Sheet
   MS Excel 4.0 International Macro Sheet
   Worksheet

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

The following Visual Basic code examples assume that you have a workbook that contains one worksheet, one MS Excel 4.0 Macro sheet, one chart sheet, one dialog sheet, and one Visual Basic module, in that order. The code example is located in the Visual Basic module.

To run the macro, position the insertion point in the line that reads "Sub Sheet_Type()" and click Start on the Run menu.

   Option Explicit

   Sub Sheet_Type()
       ' Dimension variables.
       Dim X As Variant
       ' Iterate through the loop once for each sheet in the workbook.
       For Each X In ActiveWorkbook.Sheets

           ' If the sheet's TypeName is "Worksheet", then...
           If TypeName(X) = "Worksheet" Then
               ' Check for each Type (xlWorksheet, xlExcel4MacroSheet,
               ' xlExcel4IntlMacroSheet) and display the appropriate 
               ' message box.
               If X.Type = xlWorksheet Then
                   MsgBox "Worksheet"
               ElseIf X.Type = xlExcel4MacroSheet Then
                   MsgBox "MS Excel 4.0 Macro Sheet"
               ElseIf X.Type = xlExcel4IntlMacroSheet Then
                   MsgBox "MS Excel 4.0 International Macro Sheet"
               End If

           ' Otherwise, display a message box with the appropriate 
           ' TypeName.
           Else
               MsgBox TypeName(X)         ' Show sheet type in message box.
           End If
       Next                               ' Repeat the loop until finished.
   End Sub

When you run the Sheet_Type subroutine, the messages you receive are:

   Worksheet, MS Excel 4.0 Macro Sheet, Chart, DialogSheet, Module

NOTE: In Microsoft Excel 97 and Microsoft Excel 98, you do not receive a message box for the module. This occurs because of the design of the Visual Basic Editor in these versions of Microsoft Excel.


Additional query words: 5.00 5.00c 7.00 7.00a XL98 XL97 XL7 XL5
Keywords : kbcode kbprg PgmOthr
Version : WINDOWS:5.0,5.0c,7.0,97; MACINTOSH:5.0,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.