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
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