ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel sort vs VBA (https://www.excelbanter.com/excel-programming/365485-excel-sort-vs-vba.html)

Glynn Consulting Ltd

Excel sort vs VBA
 
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...

Norman Jones

Excel sort vs VBA
 
Hi Glynn,

Perhaps:

Ad Spend - Arbeitsagentur (Group Label)


should be

Ad Spend - Arbeitsagentur - (Group Label)

( missing hyphen ?)


---
Regards,
Norman


"Glynn Consulting Ltd" <Glynn Consulting
wrote in message ...
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...




Glynn Consulting Ltd[_2_]

Excel sort vs VBA
 
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"



"Norman Jones" wrote:

Hi Glynn,

Perhaps:

Ad Spend - Arbeitsagentur (Group Label)


should be

Ad Spend - Arbeitsagentur - (Group Label)

( missing hyphen ?)



Bob Phillips

Excel sort vs VBA
 
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)

"Glynn Consulting Ltd" wrote
in message ...
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"



"Norman Jones" wrote:

Hi Glynn,

Perhaps:

Ad Spend - Arbeitsagentur (Group Label)


should be

Ad Spend - Arbeitsagentur - (Group Label)

( missing hyphen ?)





Peter T

Excel sort vs VBA
 
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


"Glynn Consulting Ltd" wrote
in message ...
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"



"Norman Jones" wrote:

Hi Glynn,

Perhaps:

Ad Spend - Arbeitsagentur (Group Label)


should be

Ad Spend - Arbeitsagentur - (Group Label)

( missing hyphen ?)






All times are GMT +1. The time now is 01:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com