Microsoft KB Archive/305565

= XL97: How to Programmatically Change the Format of a User Input After Data Entry =

Article ID: 305565

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition

-



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:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

To create and use a sample macro to format cells after data has been entered, follow these steps:  Start Microsoft Excel. Click Tools, point to Macro, and then click Visual Basic Editor (or press ALT+F11).  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).   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(&quot;A1:A5&quot;), 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  On the File menu, click Close and Return to Microsoft Excel. Type text in any cell in A1:A5, and then press ENTER.

NOTE: The text in the cell is bold.</li></ol>

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(&quot;A1:A5&quot;), 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 &quot;$A$1&quot; Target.Font.Bold = True Case &quot;$A$2&quot; Target.Font.Italic = True Case Else Target.Font.Size = 32 End Select

End Sub

Additional query words: XL97

Keywords: kbhowto KB305565

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.