Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Strings not comparable??

Hi!

I try to compare two Strings in Excel 2003 VBA . If both strings
contain the same, sometimes comparison delivers 'true', sometimes
'false'. Although beeing experienced in programming VBA makros, I don't
understand this problem at the moment. Here are some fragments of my
code:

Option Compare Text

Private Type DatenEintrag
kuerzel As String
mass As String
End Type
Private Daten() as DatenEintrag

Private sub getDataFromXls(row as Integer)
Dim de as DatenEintrag

de.kuerzel = Cells(row, colKuerzel) //copies a string from the
cell
de.mass = Cells(row, colMass) //copies a string from the
cell

Call addDatenEintrag(de) //adds de into array Datan()
End Sub

Private Function searchDataIndex(sKuerzel As String, sMass As
String) As Integer
Dim index As Integer //index in array Daten
Dim bound As Integer //amount of array items
Dim data As DatenEintrag //copy of an item of array Daten
Dim k As String //copy of data.kuerzel
Dim m As String //copy of data.mass
Dim found As Boolean //true if found in array Daten

bound = UBound(Daten) - 1
found = False

For index = 0 To bound
data = Daten(index) //copy item of array Daten

k = Daten.kuerzel //copy data.kuerzel (content
is String datatype!)
m = Daten.mass //copy data.mass (content is
String datatype!)

If k = sKuerzel And m = sMass Then //compare Strings
found = True //If strings are equal,
searched Daten item was found
Exit For
End If
Next

If Not found Then
index = -1
End If

searchDataIndex = index //return index of interesting item
of Daten or -1 if not found

End Function


As you can see, sub getDataFromXls copies content from cells to a
variable of type DatenElement. This variable will then be added to the
array Daten. Contents of Excel cells are strings here.
Later, if I want to search for a Daten item with given "kuerzel" and
given "mass", I use function searchDataIndex with "kuerzel" and "mass"
I want to find in array Daten. The "if"-clause should compare this
string, but sometimes it did not work correctly.

Example:
1) searchDataIndex("U", "50/38") = will be found without any
trouble (if there is an item in Daten with kuerzel = "U" and mass =
"50/38", of course ;-).

2) searchDataIndex("L", "45/5") = here comparison did NOT
work!!! Daten contains an item with that given values of kuerzel and
mass, so variables k and m contain that values after copy-operation in
searchDataIndex. BUT the comparison delivers "false" at this
constellation! I have testet seperately both if k = sKuerzel and if m =
sMass - it seems, that (remaind that here is k = sKuerzel = "L"! ) here
comparison is not able to see, that "L" = "L". I get "false" as result!

Has somebody an idea, what the problem is??

Thanks,
Marco

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Strings not comparable??

Hi Marco,

bound = UBound(Daten) - 1
For index = 0 To bound


Why the -1.
Could it be you are failing to find a match in Daten(UBound)

Regards,
Peter T

"Marco Bandner" wrote in message
ps.com...
Hi!

I try to compare two Strings in Excel 2003 VBA . If both strings
contain the same, sometimes comparison delivers 'true', sometimes
'false'. Although beeing experienced in programming VBA makros, I don't
understand this problem at the moment. Here are some fragments of my
code:

Option Compare Text

Private Type DatenEintrag
kuerzel As String
mass As String
End Type
Private Daten() as DatenEintrag

Private sub getDataFromXls(row as Integer)
Dim de as DatenEintrag

de.kuerzel = Cells(row, colKuerzel) //copies a string from the
cell
de.mass = Cells(row, colMass) //copies a string from the
cell

Call addDatenEintrag(de) //adds de into array Datan()
End Sub

Private Function searchDataIndex(sKuerzel As String, sMass As
String) As Integer
Dim index As Integer //index in array Daten
Dim bound As Integer //amount of array items
Dim data As DatenEintrag //copy of an item of array Daten
Dim k As String //copy of data.kuerzel
Dim m As String //copy of data.mass
Dim found As Boolean //true if found in array Daten

bound = UBound(Daten) - 1
found = False

For index = 0 To bound
data = Daten(index) //copy item of array Daten

k = Daten.kuerzel //copy data.kuerzel (content
is String datatype!)
m = Daten.mass //copy data.mass (content is
String datatype!)

If k = sKuerzel And m = sMass Then //compare Strings
found = True //If strings are equal,
searched Daten item was found
Exit For
End If
Next

If Not found Then
index = -1
End If

searchDataIndex = index //return index of interesting item
of Daten or -1 if not found

End Function


As you can see, sub getDataFromXls copies content from cells to a
variable of type DatenElement. This variable will then be added to the
array Daten. Contents of Excel cells are strings here.
Later, if I want to search for a Daten item with given "kuerzel" and
given "mass", I use function searchDataIndex with "kuerzel" and "mass"
I want to find in array Daten. The "if"-clause should compare this
string, but sometimes it did not work correctly.

Example:
1) searchDataIndex("U", "50/38") = will be found without any
trouble (if there is an item in Daten with kuerzel = "U" and mass =
"50/38", of course ;-).

2) searchDataIndex("L", "45/5") = here comparison did NOT
work!!! Daten contains an item with that given values of kuerzel and
mass, so variables k and m contain that values after copy-operation in
searchDataIndex. BUT the comparison delivers "false" at this
constellation! I have testet seperately both if k = sKuerzel and if m =
sMass - it seems, that (remaind that here is k = sKuerzel = "L"! ) here
comparison is not able to see, that "L" = "L". I get "false" as result!

Has somebody an idea, what the problem is??

Thanks,
Marco



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Strings not comparable??

Hi Peter,

bound = UBound(Daten) - 1
For index = 0 To bound


Why the -1.


At moment of coding I thought, array are indexed from 0 to n-1 (n =
ubound). You can ignore that regarding my problem.

Could it be you are failing to find a match in Daten(UBound)


Definetely no - to test my code (and this example of the problem case),
first I insert all values into that array, I want to find. While
debugging line for line, the VBA IDE shows me
at line "If k = sKuerzel And m = sMass Then" that k and sKuerzel
contain the same(!) values. Nevertheless comparising delivers "false"
for that case that k = sKuerzel = "L". I don't understand that.

Has anybody an idea??

Regards, Marco

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Strings not comparable??

If I follow you have "L" in two variables which fail to return true if
compared. It does seem odd so as a first step verify the strings in each
variable are truly the same.

In your procedure declare
Dim bArr() as byte

Put a break at the point you think the two var's should be the same, step
through and do this

bArr = k
'look in locals
bArr = sKuerzel
'look in locals

Press alt-v,s to look in Locals, expand and examine bArr
I would expect to see bArr(0) = 76, bArr(1) = 0 for each string = "L"

Regards,
Peter T






"Marco Bandner" wrote in message
ps.com...
Hi Peter,

bound = UBound(Daten) - 1
For index = 0 To bound


Why the -1.


At moment of coding I thought, array are indexed from 0 to n-1 (n =
ubound). You can ignore that regarding my problem.

Could it be you are failing to find a match in Daten(UBound)


Definetely no - to test my code (and this example of the problem case),
first I insert all values into that array, I want to find. While
debugging line for line, the VBA IDE shows me
at line "If k = sKuerzel And m = sMass Then" that k and sKuerzel
contain the same(!) values. Nevertheless comparising delivers "false"
for that case that k = sKuerzel = "L". I don't understand that.

Has anybody an idea??

Regards, Marco



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
find and replace numeric strings in larger text strings Mr Molio Excel Worksheet Functions 8 November 9th 11 05:17 PM
comparable week numbers overs years Joël Excel Discussion (Misc queries) 1 April 16th 07 03:33 PM
comparable week numbers over years Joël Excel Discussion (Misc queries) 0 April 11th 07 02:12 PM
How to find number of pairs of strings from list of strings? greg_overholt Excel Worksheet Functions 5 January 27th 06 10:42 PM
Looking for comparable data records between Sheet1 and Sheet2 Jim May Excel Discussion (Misc queries) 3 April 1st 05 08:04 PM


All times are GMT +1. The time now is 11:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"