VBScript: how to find the difference between two strings that look identical - string

There are two strings "test_name" that are compared in a VB script. They have to be identical, and they look identical in debug viewer, but StrCompare(string1, string2) returns 1.
History.
This is a test in QTP. The first string is read from Excel. The second one is from a windows application. QTP reads a value from Excel, enters it to a windows form, and then reads the same value from another place. The test passes if these two values are identical.
How to find a difference in these two strings so that I can correct the test?

I would suggest using a For loop, Mid, and Asc, to compare the actual characters one by one. Something like (untried code):
' Presume input strings named s1 and s2
' Assume Len(s1) = Len(s2)
Dim i
For i = 1 to Len(s1)
If Asc(Mid(s1, i, 1)) <> Asc(Mid(s2, i, 1)) Then
Msgbox "Strings differ at character " & i
End If
Next 'i
If they are equal by this test, and unequal by StrComp then... I don't really know. Perhaps try the same thing with LenB and AscB to see if it's a Unicode or encoding issue somehow.

Most likely you have trailing spaces at the end (or something else that prints like a space). Try to print them like this:
Debug.Print "*" & string1 & "*"
Debug.Print "*" & string2 & "*"
and see what you get.

Did you try using the parameter vbTextCompare in your StrComnpare ?
This would do case-insensitive comparison of both strings.
I also would have recommended what the 2 above said.
So it would be:
StrCompare(String1, String2, vbTextCompare)
Kind Regards,

Related

how to move special char in a string from beginning to the end in crystal reports

in my program I (after many procedures) get tokenized words. Unfortunately due to reversing them they hold punctuation characters at the beginning of a word eg. "BARGE "UR 106
How to move that " from the beginning to the end -> BARGE "UR 106"
another example: (.REIS (HASAN M should be -> REIS (HASAN M.)
Up to now I've tried:
{DOCHS14.SHEM__ONIA} startswith ["\"","\(","\."\,"\(\."]
then
Local StringVar str:={DOCHS14.SHEM__ONIA}[0]
TrimLeft ({DOCHS14.SHEM__ONIA})
{DOCHS14.SHEM__ONIA}&str;
But that gives me errors:
A number, currency amount, boolean, date, time, date-time, or string is expected here.
How to fix that? or is there another way to solve this problem?
There are multiple issues in your formula.
startswith expects a string not an array of strings.
Only the double qoute must be escaped, but you did it wrong. (See here)
While a solution with startswith is also possible, I have used the Left-function instead. In your third example one have to check two characters, so this must be checked first and output another result.
if Left({DOCHS14.SHEM__ONIA}, 2) = "(." Then
Mid({DOCHS14.SHEM__ONIA}, 3) + ".)"
else if Left({DOCHS14.SHEM__ONIA}, 1) in ["""", "(", ".", ","] Then
Mid({DOCHS14.SHEM__ONIA}, 2) + Left({DOCHS14.SHEM__ONIA}, 1)
else
{DOCHS14.SHEM__ONIA}

VBA - Identifying null string

One of my cells appears to be blank but has a length of 2 characters. I copied the string to this website and it has identified it as a null string.
I have tried using IsNull and IsEmpty, as well as testing to see if it is equivalent to the vbNullString but it is still coming up as False.
How do I identify this string as being Null?
A string value that "appears to be blank but has a length of 2 characters" is said to be whitespace, not blank, not null, not empty.
Use the Trim function (or its Trim$ stringly-typed little brother) to strip leading/trailing whitespace characters, then test the result against vbNullString (or ""):
If Trim$(value) = vbNullString Then
The Trim function won't strip non-breaking spaces though. You can write a function that does:
Public Function TrimStripNBSP(ByVal value As String) As String
TrimStripNBSP = Trim$(Replace(value, Chr$(160), Chr$(32)))
End Function
This replaces non-breaking spaces with ASCII 32 (a "normal" space character), then trims it and returns the result.
Now you can use it to test against vbNullString (or ""):
If TrimStripNBSP(value) = vbNullString Then
The IsEmpty function can only be used with a Variant (only returns a meaningful result given a Variant anyway), to determine whether that variant contains a value.
The IsNull function has extremely limited use in Excel-hosted VBA, and shouldn't be needed since nothing is ever going to be Null in an Excel worksheet - especially not a string with a length of 2.
Chr(160) Issue
160 is the code number of a Non-Breaking Space.
Let us say the cell is A1.
In any cell write =CODE(A1) and in another (e.g. next to) write =CODE(MID(A1,2,1)).
The results are the code numbers (integers e.g. a and b) of the characters.
Now in VBA you can use:
If Cells(1, 1) = Chr(a) & Chr(b) Then
End If
or e.g.
If Left(Cells(1, 1), 1) = Chr(160) then
End If

Putting a char in an exact stringposition

Is there a way to put a Char in this case a "0" into a string?
I'd like to put a zero at position 8
For example:
device 4 -> device 04
I hope i could explain the problem well.
newString = Left(existingString, 7) & "0" & Mid(existingString, 8) is one way.
& is the string concatenation operator in VBA, the two argument Mid runs from a given position to the end of a string.
This can also be done using a worksheet function:
=REPLACE(D1,8,0,"0")
And the Replace method is also a member of the WorksheetFunction in VBA, so you could write it as a VBA function also. Note that this is different from the VBA Replace function.

How to check against a list of values in an IF statement?

I am trying to write an IF statement like this:
if var [is any of 1,4,5,6,12] then do stuff
But I don't know the syntax for this in VBA, other than:
if var=1 or var=4 or var=5...
which seems a bit clumsy. Is there a different way?
You can use a Select Case statement:
select case var
case 1,4,5,6,12
'do something
case else
'alternative
end select
I'm a bit late to the 'party' but how about:
If InStr(1, ",1,5,8", "," & lVal1, vbTextCompare) > 0 Then
to check if 'lVal1' to equal to 1, 5 or 8.
And
If InStr(1, ",6,8,10,12", "," & lVal2, vbTextCompare) = 0 Then
to check that 'lVal2' is not equal to 6, 8, 10, 12.
The comma delimiters are important because, without them, in the first example, '15' or '58' or '158' would all be matched if 'lVal' was able to take one of those values.
You will need to pay attention to the delimiter that you use if there was likely to be any ambiguity with the value being checked for.
Likewise, using your knowledge of the range of values that will be encountered, the clunkiness of the leading delimiter in the first string and concatenating the delimiter to the front of the search value, could be removed.
You could make a list of numbers, and then in a for-loop to compare these:
dim newNumber as Integer
dim compareList as new List Of(int)
for count as integer = 0 to compareList.count - 1
if newNumber = compareList(nCount)
'Do Stuff
end if
next
This is a simple way of doing that I like to do, but may get performance intensive if your lists are really large/you want to do a lot of code in the "if" loop.

String manipulation with Excel - how to remove part of a string if another part is there?

I've done some Googling, and can't find anything, though maybe I'm just looking in the wrong places. I'm also not very adept at VBA, but I'm sure I can figure it out with the right pointers :)
I have a string I'm building that's a concatenation of various cells, based on various conditions. I hit these in order.
=IF(A405<>A404,G405,G405&H404)
What I want to do is go back through my concatenated list, removing a superseded value if the superseder is in the list.
For example, see the following list:
A, D, G, Y, Z
I want to remove D if and only if Y is present.
How would I go about this? (VBA or in-cell, though I'd prefer in-cell)
Try:
=IF(ISERROR(FIND("Y",A1)),A1,SUBSTITUTE(A1,"D, ",""))
But that assumes you always have the comma and space following the D.
Firstly, why not keep a string array instead as you go through all the cells, then concatenate it all at the end?
Otherwise, you'll be using string functions like INSTR and MID to do something like:
start1 = instr(myLongString,"Y, ")
if start1 > 0 Then
start2 = instr(myLongString,"D, ")
if start2 > 0 then
newLongString = left(myLongString, start2 - 1) & _
mid(myLongString, start2 + 3)
end if
end if
But, as I said, I would keep an array that is easy to loop through, then once you have all the values you KNOW you will use, just concatenate them at the end.
VBA : You can always use the regexp object.
I think that gives you the ability to test anything on your script as long as you build correctly the regular expression.
Check out : http://msdn.microsoft.com/en-us/library/yab2dx62(VS.85).aspx ( for regexp reference )
and a simple tool to test your regexps : http://www.codehouse.com/webmaster_tools/regex/
In-cell: you could do it in a more excel friendly way:
suppose on column A:A you have the values.
You can add a new column where you perform the check
if(indirect("A"&row()) <> indirect("A"&row()-1), indirect("G"&row()), indirect("G"&row())& indirect("H"&row()))
or whatever the values are. I guess however that on one branch of the if statement the value should be blank. After that you concatenate only the B:B column values ( skipping blanks if needed ).
Hope this helps.
It's probably easier to start at the end, make your additions to the beginning of the string, and only add D if Y is not present.
I guess D could appear anywhere, so how about:
If InStr(strString, "Y") > 0 Then
strString = Replace(strString, "d", "")
strString = Replace(strString, " ", "")
strString = Replace(strString, " ,", "")
strString = Replace(strString, ",,", ",")
End If
If there are not too many of these combinations that you want to remove, you can use =IF(FIND("D"; A2)> 0; REPLACE(A2;1;3;"");A2).
I just got this as a possible solution via email, too:
=IF(A15<>A14,G15,IF(OR(AND(G15="CR247, ",ISNUMBER(FIND("CR247, ",H14))),AND(G15="CR149, ",ISNUMBER(FIND("CR215, ",H14))),AND(G15="CR149, ",ISNUMBER(FIND("CR180, ",H14))),AND(G15="CR180, ",ISNUMBER(FIND("CR215, ",H14))),G15="CR113, "),H14,G15&H14))
(this has the "real" values with precedence rules)
It looks relatively similar to #Joseph's answer.
Is there a better solution?

Resources