Solved

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

Posted on 2009-05-03
3
1,091 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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