Solved

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

Posted on 2009-05-03
3
1,088 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
ID: 24292749
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
ID: 24293847
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protecting vb6 & .Net code Obfuscation 18 156
Server 2012 management. 5 34
How to implement an additional command 16 40
Excel formula to report date modified 14 29
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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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