Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

Excel Formatting from VB

I've got my application to build a spreadsheet, but now I want to make that spreadsheet look good. I need to adjust cell alignments, font face size and weight, row height, column width, set borders, freeze a pane at the top, and protect the whole sheet. How is this done? Where do I find help references for Excel.Application methods and properties?
1 Solution
This will do the formatting for the cells in range B6 to I16
(probably cou can also change the rest like this way)

.Range("B6:I16").NumberFormat = "#0.00"
.Range("B6:I16").FontColor = vbRed (can also be color or textcolor)

Personally I found the built-in help for ExcelVBA quite helpful.

Since your application already builds the spreadsheet, I assume that you have a reference to the Excel Object Library in your project.
Just press F2 to show the object catalogue, select the Excel Library and look for a class called "Range". That shows you all properties, methods and events the range-object has. By pressing F1 VB should show you help to the command you've selected.
You can find help on the application, workbook and worksheet object just the same way.

Another way of finding the right properties would be to use Excel and record a macro while you do the changes manually. The you can enter the VBA-Editor (F11) and have a look at the generated code and pick the lines you want.
For information on Excel.Application, start here:


And for more specific information, search http://msdn.microsoft.com

With regards to formatting your sheet, the easiest way to let Excel do the work for you:

1) Open an Excel sheet
2) Start recording a macro (Tools -> Macros)
3) Perform the formatting you want to know how to do
4) Stop the macro recording

Now, check the source code of the macro in Excel's Visual Basic Editor (Tools -> Macros -> Visual Basic Editor) - most of the code you need will already be in there, you just need to adapt it to work with the Excel.Application object.

ThanborAuthor Commented:
Thx a lot. Very helpful.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now