Excel sort vs VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is part of a sequence that was sorted in ascending order in Excel...

Ad Spend - Arbeitsagentur - Press (€)
Ad Spend - Arbeitsagentur - Trade Magazines/Press ($)
Ad Spend - Arbeitsagentur - Trade Magazines/Press (€)
Ad Spend - Arbeitsagentur (Group Label)
Ad Spend - Cadremploi - Online ($)
Ad Spend - Cadremploi - Online (£)

When running a loop in VBA, the value in the 4th line is less than the value
in the 3rd. Why did is this so?

Is there a way of comparing cells so that the hireachy is the same as Excel
uses for sorting.

Currently I am using..

if range(CellAdress).value < range(CellAdress.offset(1,0)).value then...
 
The problem is not what the line should say, it is how Excel has sorted the
line compared to how visual basic regards the data. Excel says one is greater
than the other, while VBA says that one is less than the other.

To distill the problem to the bare minimum..

In Excel the formula =IF("-">"(","Yes","No") gives the result "No"

In VBA the routine...

Sub Test()
If "-" > "(" Then
MsgBox "Yes"
Else
MsgBox "No"
End If
End Sub

....gives the result "Yes"
 
VBA looks right to me. Asc("-") is 45, Asc ("(") is 40, so it should be. Why
sort does it the other way, is beyond me at this minute.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
There are many differences between comparisons in Excel & VBA, particularly
with different data types. However in this particular case to get VBA to
simulate Excel head your module
Option Compare Text

and you should find the result of your test demo is reversed to same as
Excel

But need to do a lot more to fully simulate Excel

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top