Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
find and replace numeric strings in larger text strings | Excel Worksheet Functions | |||
comparable week numbers overs years | Excel Discussion (Misc queries) | |||
comparable week numbers over years | Excel Discussion (Misc queries) | |||
How to find number of pairs of strings from list of strings? | Excel Worksheet Functions | |||
Looking for comparable data records between Sheet1 and Sheet2 | Excel Discussion (Misc queries) |