Hi, I recorded a macro in Excel and now I want to use it in VBScript. I am
having a heck of a time to find any information on how to do a sort in Excel
using VBScript...

Range("A1:H227").Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range
("D2"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase
:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2:=xlSortNormal

Re: VBA to VBScript - Sorting by Michael

Michael
Tue Jan 25 20:02:17 CST 2005

JM wrote:
> Hi, I recorded a macro in Excel and now I want to use it in VBScript.
> I am having a heck of a time to find any information on how to do a
> sort in Excel using VBScript...
>
> Range("A1:H227").Sort Key1:=Range("A2"), Order1:=xlAscending,
> Key2:=Range ("D2"), Order2:=xlAscending, Header:=xlGuess,
> OrderCustom:=1, MatchCase :=False, Orientation:=xlTopToBottom,
> DataOption1:=xlSortNormal, DataOption2:=xlSortNormal


When you read the help on the various objects, properties, methods, etc. you
have to keep in mind that it's written in the context of VBA hosted by a
specific VBA-enabled application, not VBScript. VBA is hosted "from the
inside" by the specific application. As the host, it automatically provides
things to the VBA code that aren't automatic when you automate an
application's object model "from the outside" using VBScript hosted by WSH.

The key items to remember:

--- No objects are automatically exposed to VBScript. You generally use an
explicit CreateObject to get an instance of an object to use as the "root",
usually the ".Application" object.

---In VBA that Application object and it's immediate interface members
(properties/methods) are automatically exposed. In VBScript you refer to the
Application object and it's properties/methods through the object variable
reference returned by the CreateObject.

---Named constants specific to the application aren't exposed. You can
either look them up and code them locally in the VBScript code as Const
variables, or you can use the .wsf file format and a <reference> element to
automatically expose them.

---VBA supports named argument syntax (e.g., ArgName:="argvalue") in method
calls. In WSH hosted VBScript, you have to code all arguments as positional
arguments since named argument syntax is not supported.

Once you understand the "VBA from the inside" vs "VBScript from the outside"
issues and the fundamental differences between VBA and VBScript as separate
but similar languages, you should be able to mentally "port" VBA and even
full VB examples to VBScript.

--
Michael Harris
Microsoft MVP Scripting



Re: VBA to VBScript - Sorting by JM

JM
Wed Jan 26 11:11:08 CST 2005

Here is what worked for me

' Set the ranges so that we can sort.
Set objRange = objExcel.Range("A:H")
Set objRange2 = objExcel.Range("A2")

' Sort
objRange.Sort objRange2,1,,,,,,1



"Michael Harris (MVP)" wrote:

> JM wrote:
> > Hi, I recorded a macro in Excel and now I want to use it in VBScript.
> > I am having a heck of a time to find any information on how to do a
> > sort in Excel using VBScript...
> >
> > Range("A1:H227").Sort Key1:=Range("A2"), Order1:=xlAscending,
> > Key2:=Range ("D2"), Order2:=xlAscending, Header:=xlGuess,
> > OrderCustom:=1, MatchCase :=False, Orientation:=xlTopToBottom,
> > DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
>
>
> When you read the help on the various objects, properties, methods, etc. you
> have to keep in mind that it's written in the context of VBA hosted by a
> specific VBA-enabled application, not VBScript. VBA is hosted "from the
> inside" by the specific application. As the host, it automatically provides
> things to the VBA code that aren't automatic when you automate an
> application's object model "from the outside" using VBScript hosted by WSH.
>
> The key items to remember:
>
> --- No objects are automatically exposed to VBScript. You generally use an
> explicit CreateObject to get an instance of an object to use as the "root",
> usually the ".Application" object.
>
> ---In VBA that Application object and it's immediate interface members
> (properties/methods) are automatically exposed. In VBScript you refer to the
> Application object and it's properties/methods through the object variable
> reference returned by the CreateObject.
>
> ---Named constants specific to the application aren't exposed. You can
> either look them up and code them locally in the VBScript code as Const
> variables, or you can use the .wsf file format and a <reference> element to
> automatically expose them.
>
> ---VBA supports named argument syntax (e.g., ArgName:="argvalue") in method
> calls. In WSH hosted VBScript, you have to code all arguments as positional
> arguments since named argument syntax is not supported.
>
> Once you understand the "VBA from the inside" vs "VBScript from the outside"
> issues and the fundamental differences between VBA and VBScript as separate
> but similar languages, you should be able to mentally "port" VBA and even
> full VB examples to VBScript.
>
> --
> Michael Harris
> Microsoft MVP Scripting
>
>
>