Auto Format in Excel using VBScript

Posted on 2009-02-16
Medium Priority
Last Modified: 2012-05-06
I'm looking for some code for a VB Script so I can autoformat a worksheet in excel.  I tried using the macro recorder and got the following code but I can't get it to run properly.  
It doesn't have to use the code I have below I am just looking for somethign so I can format my worksheets.  
"ie" is Set ie = CreateObject("excel.Application")

ie.Selection.AutoFormat Format:= xlRangeAutoFormatClassic3, Number:=True, Font:=True, Alignment:=True, Border:=True, Pattern:=True, Width:=True

Open in new window

Question by:Droo1977
  • 2
LVL 93

Expert Comment

by:Patrick Matthews
ID: 23652362
ie.Selection.AutoFormat Format:=3, Number:=True, Font:=True, Alignment:=True, Border:=True, _
    Pattern:=True, Width:=True

xlRangeAutoFormatClassic3 is equal to 3; in VBScript you cannot use named constants from other libraries.
To get the value for any Excel named constant, just go to Excel's VB Editor, and run something like this in
the Immediate window:


Author Comment

ID: 23652461
Thanks, I've actually tried that and it gives me an "Expected Statement"  error. What else might I be doing wrong?
LVL 93

Accepted Solution

Patrick Matthews earned 2000 total points
ID: 23652536
Sorry, I forgot that VBScript also does not support passing arguments by name--you can only pass them
by position:

ie.Selection.AutoFormat 3, True, True, True, True, True, True

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question