Click here to Skip to main content
15,880,608 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Tip/Trick

Tip: Format an Excel Range as a Table Programatically

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
26 Aug 2011CPOL 78.8K   10   8
C# code to format a certain range of Excel cells using the Format As Table button -- but from C# interop code
Sometimes, you have a range of cells and you want to choose the Format As Table button to format the cells as a nice table.

Here's how to do that using Excel interop:

C#
public void FormatAsTable(Excel.Range SourceRange, string TableName, string TableStyleName)
{
    SourceRange.Worksheet.ListObjects.Add(XlListObjectSourceType.xlSrcRange,
    SourceRange, System.Type.Missing, XlYesNoGuess.xlYes, System.Type.Missing).Name =
        TableName;
    SourceRange.Select();
    SourceRange.Worksheet.ListObjects[TableName].TableStyle = TableStyleName;
}


That's it! To apply a table style, e.g. TableStyleMedium15, to a range of cells, you say:

C#
Excel.Range SourceRange = (Excel.Range)oSheet.get_Range("A6","X10"); // or whatever range you want here
FormatAsTable(SourceRange, "Table1", "TableStyleMedium15");


The "Table1" is just a random name for the table; it's arbitrary, but every table you format must be a unique range. The table style names you can find out by recording a macro, applying the formatting by hand, and then reading off of the VBA module what style name Excel filled in once you've stopped the recording.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
Brian C. Hart, Ph.D., is a strategic engagement leader on a mission to leverage space technology to protect U.S. interests and assets against adversaries. Throughout Dr. Hart's career, he has enjoyed: Working closely with business executives to provide strategic direction and leadership, translating customer and competitive intelligence into compelling capture strategies and solutions, and mentoring teams to enhance individual and company capabilities while fostering an engaging and accountable environment, being involved in STEAM initiatives and education to develop greater awareness in the community, and serving the armed forces with the U.S. Navy and U.S. Army National Guard. He is excited to begin developing his career in Jacobs's Critical Mission Systems business unit, supporting NORAD and the U.S. Space Force.

Comments and Discussions

 
QuestionTIP: If style is not working Pin
Leo Gurdian14-Jun-17 12:25
Leo Gurdian14-Jun-17 12:25 
QuestionHow to find the strings corresponding to Table Style Pin
Sri Lakshmanan22-Mar-14 6:42
Sri Lakshmanan22-Mar-14 6:42 
AnswerRe: How to find the strings corresponding to Table Style Pin
Brian C Hart23-Mar-14 17:17
professionalBrian C Hart23-Mar-14 17:17 
GeneralGood Tip Pin
Kit Fisto2-Aug-12 6:43
Kit Fisto2-Aug-12 6:43 
GeneralRe: Good Tip Pin
Brian C Hart2-Aug-12 7:00
professionalBrian C Hart2-Aug-12 7:00 
GeneralReason for my vote of 5 My Vote 5 Pin
Abdul Quader Mamun17-Dec-10 5:11
Abdul Quader Mamun17-Dec-10 5:11 
GeneralGood tips Pin
Abdul Quader Mamun16-Dec-10 8:36
Abdul Quader Mamun16-Dec-10 8:36 
GeneralExcellent tip, firm 5! Pin
DrABELL17-Dec-10 16:14
DrABELL17-Dec-10 16:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.