DESKTOP

Becoming an Excel Programmer : Where's My Code?

1/10/2011 11:09:23 AM
Excel stores Visual Basic code in the workbook (.xls), template (.xlt), or add-in (.xla) file when you save it. File formats other than those omit the Visual Basic code the same way that special formatting is lost when you save a workbook as a text (.txt) or comma-delimited file (.csv).

You can view the code in a currently open Excel file by pressing Alt-F11, by choosing Tools → Macro → Visual Basic Editor or by clicking the Visual Basic Editor button on the Visual Basic toolbar (Figure 1).

Figure 1. The Visual Basic toolbar lets you edit, run, or stop code; create controls; and set macro security

Within the editor, code is organized into modules and classes . Modules are static code files that typically contain recorded code and public procedures that you want users to be able to call directly from Excel. Classes are associated with an instance of an object in Excel, such as a workbook or worksheet. Classes usually contain code that responds to Excel events, such as when a command button is clicked or when the user opens the workbook.

Excel creates a new module called Module1 when you first record code as shown earlier in this chapter. Excel provides a class for each new sheet you add to a workbook. Similarly, Excel deletes that sheet's class when you delete the sheet from the workbook, so be careful when deleting sheets while programming! You can see a workbook's classes and modules in the editor's Project window (Figure 2).

You can also use the Project window to export classes or modules to text files and to import code stored as text into the workbook. Unfortunately, there's no easy way to store code separately from the workbook (which would be nice when more than one person is working on code).

Visual Basic displays information about each class or module in the Properties window below the Project window, as shown in Figure 3. As you select a different item in the Project window, the item displayed in the Properties window changes.

You can use the Properties window to rename modules or classes or to control various aspects of a class. For example, to rename Module1 something descriptive, like RecordedCode, select Module1 in the Project window and type RecordedCode in the (Name) property of the Properties window. You can also use the Properties window to hide sheets by setting the class's Visible property.

Figure 2. Double-click on a class or module to open it in a code window

Figure 3. Select ThisWorkbook in the Project window to see the workbook's properties
Other  
  •  Becoming an Excel Programmer : View Results
  •  Becoming an Excel Programmer : Start and Stop
  •  Windows Server 2008 : Configuring and Monitoring Terminal Service Resources
  •  Visual Studio 2010 : Understanding Debugging
  •  Visual Studio 2010 : Structured Exception Handling to the Rescue
  •  Implement an Observer (aka Subscriber) Pattern
  •  Use a Stopwatch to Profile Your Code
  •  Combine Multiple Events into a Single Event
  •  Internet Security and Acceleration Server 2004 : Additional Configuration Tasks
  •  Windows Server AppFabric
  •  Cloud Application Architectures : Privacy Design
  •  Cloud Application Architectures : Machine Image Design
  •  Windows Azure : Using the Storage Client Library
  •  Windows Azure : Using the Blob Storage API
  •  Windows Azure : Blobs - Usage Considerations
  •  Windows Azure : Understanding the Blob Service
  •  Design and Deploy High Availability for Exchange 2007 : Design Edge Transport and Unified Messaging High Availability
  •  Design and Deploy High Availability for Exchange 2007 : Design Hub Transport High Availability
  •  Design and Deploy High Availability for Exchange 2007 : Design CAS High Availability
  •  Design and Deploy High Availability for Exchange 2007 : Create Bookmark Create Note or Tag Implement Standby Continuous Replication (SCR)
  •  
    Most View
    Windows Vista : Build Your Network (part 5) - Lock Out Unauthorized PCs, Connect to a Public Wireless Network
    Why Is Nobody Buying PCs?
    Top Mind Mapping Tools For Android
    Virgin Takes The Plunge On Internet Television
    Microsoft & Apple rejoin the os battle
    Sign Language Technology (Part 3) - Seimens Hearing Aids
    Toshiba Satellite P870 - Gaming-Focused Laptop
    Windows Server 2003 : Planning a Host Name Resolution Strategy - Understanding Name Resolution Requirements
    Top 10 Cameras – November 2012
    Ricoh Aficio SP 100SF - Compact Multifunctional Printer For Office
    Top 10
    Western Digital Sentinel DX4000 NAS Review (Part 4)
    Western Digital Sentinel DX4000 NAS Review (Part 3)
    Western Digital Sentinel DX4000 NAS Review (Part 2)
    Western Digital Sentinel DX4000 NAS Review (Part 1)
    Enlightenment E17 - Eauty At Your Fingertips
    Photo Editors: From Professional RAW Tools To Simple Library Management (Part 3)
    Photo Editors: From Professional RAW Tools To Simple Library Management (Part 2)
    Photo Editors: From Professional RAW Tools To Simple Library Management (Part 1)
    Getting The Most From Passbook
    HTC One SV - The Best Bits Of The Impressive One Series