Excel

Create new style from formatted cell

Ease of Use

Easy

Version tested with

2000 

Submitted by:

johnske

Description:

After running the code the new style will be added to your styles collection 

Discussion:

You may have gone to great lengths to format a cell. To add this format 'manually' to your styles collection you would normally have to re-specify all the format conditions to add them to the collection. This provides a quick and easy way to add styles. 

Code:

instructions for use

			

Option Explicit 'This adds a style that's based on a cell you've selected Sub AddaStyle() Dim NewStyle$ NewStyle = InputBox("What name do you want for this style?" & vbLf & _ "" & vbLf & _ "NOTE: The style will be based entirely on the format" & vbLf & _ "of a cell that you have selected. If the cell currently" & vbLf & _ "selected is not the one you want to use - then Click" & vbLf & _ "Cancel and select the correct cell...", "New Style - Style Name") If NewStyle = Empty Then Exit Sub ActiveWorkbook.Styles.Add Name:=NewStyle, BasedOn:=ActiveCell End Sub

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Insert/Module
  4. Copy and paste the code into the Module
  5. Now select File/Close and Return To Microsoft Excel
  6. Save your work
 

Test the code:

  1. Format a cell in whatever way you want
  2. Select the cell
  3. Select Tools/Macro/Macros.../AddaStyle/Run
  4. Now select a range of other cells
  5. Go to Format/Styles..., select the style you've added then click OK
  6. The selected cells are now formatted in this style
  7. Note: If you have a very large style collection that you want to clean out then go to http://www.vbaexpress.com/kb/getarticle.php?kb_id=367 for a procedure that does this
 

Sample File:

AddStyles.zip 10.49KB 

Approved by mdmackillop


This entry has been viewed 102 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express