Strings not comparable??

M

Marco Bandner

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
 
M

Marco Bandner

Hi Peter,
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
 
P

Peter T

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
 

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

Top