Solved

Using excel.application in vbscript, how do I sort text as numbers?

Posted on 2009-05-03
3
1,081 Views
Last Modified: 2012-06-27
I am able to sort multiple columns. But I am not able to sort text as numbers. That is, both 4-digit and 3-digit numbers (as text) are in ascending order but the former comes before the latter. What should my sort method look like in vbscript?
0
Comment
Question by:jgsimbulan
3 Comments
 
LVL 17

Accepted Solution

by:
NicksonKoh earned 250 total points
Comment Utility
I believe you have to convert the text numbers to real numbers. For that you can use Text To Column.

Attach is sample code which is generated from macro recording.
    Columns("A:A").Select

    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _

        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _

        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _

        :=Array(1, 1), TrailingMinusNumbers:=True
 

    'After conversion, you can sort as follows

    Range("A1").Select

    Range("A1:A7").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:= _

        xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _

        DataOption1:=xlSortNormal

Open in new window

0
 
LVL 13

Assisted Solution

by:StellanRosengren
StellanRosengren earned 250 total points
Comment Utility
Hi,
just remember that you cannot set parameters by name in vbscript. You have to put them in the correct position.
For example, Range("A:A").TextToColumns Range("A1"),1,1,0,1,,,,,,,,,1
Also, you cannot use the named constants like xlDelimited, xlDoubleQuote etc.
You have to use the number. For example, 1 instead of xlDelimited.

Kind regards,
Stellan
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now