XL97: How to Programmatically Change the Format of a User Input After Data Entry (305565)



The information in this article applies to:

  • Microsoft Excel 97 for Windows

This article was previously published under Q305565

SUMMARY

This article describes sample Microsoft Visual Basic for Applications Sub procedures (macros) that format a cell after data has been entered.

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 professionals 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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: To create and use a sample macro to format cells after data has been entered, follow these steps:
  1. Start Microsoft Excel.
  2. Click Tools, point to Macro, and then click Visual Basic Editor (or press ALT+F11).
  3. In the Project Explorer window, you should see entries similar to the following:
          VBAProject (Book1)
             Microsoft Excel Objects
                Sheet1 (Sheet1)
                Sheet2 (Sheet2)
                Sheet3 (Sheet3)
    						
    Double-click the entry for the worksheet to which you want to assign a Worksheet_Change macro (Sheet1, for example).
  4. In the Code window, type the following code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim r As Range
    
       'Set the range for the formatting changes to A1:A5.
       Set r = Intersect(Range("A1:A5"), Target)
    
       'If the change in the worksheet is not in the tested range, exit the macro.
       If r Is Nothing Then Exit Sub
    
       'Change to formatting of the cell that changed.
       r.Font.Bold = True
    
          
    
    End Sub
    					
  5. On the File menu, click Close and Return to Microsoft Excel.
  6. Type text in any cell in A1:A5, and then press ENTER.

    NOTE: The text in the cell is bold.
If there are multiple discontiguous ranges to be formatted, you must test against these separately.

If you want to make the formatting for each cell different, replace the sample code in the previous steps with this sample code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim r As Range

   'Set the range for the formatting changes to A1:A5
   Set r = Intersect(Range("A1:A5"), Target)

   'If the change in the worksheet is not in the tested range, exit the macro
   If r Is Nothing Then Exit Sub
   
   Select Case Target.Address
   Case "$A$1"
        Target.Font.Bold = True
   Case "$A$2"
       Target.Font.Italic = True
   Case Else
        Target.Font.Size = 32
   End Select

End Sub
				

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto KB305565