Putting a char in an exact stringposition - string

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.

Related

Sum numbers in a delimited text and return average

I have the following numbers as text in a cell:
"12-14-14-16-18-10"
And now I need to calculate the average but I do not want to create extra columns since the length of the data varies.
Is there any way to do this using a formula?
In other words: you want to split the string value by the "-" character and calculate the average of its elements? AFAIK the only way to solve this is using a small macro (AKA user-defined function), since LO Calc doesn't provide a split/tokenize function on spreadsheet level.
A quick and dirty solution may look as follows:
Function split_average(a)
Dim theArray(UBound(Split(a, "-"))) As Integer
theArray = Split(a, "-")
Dim SumVal As Integer
For i = 0 To UBound(theArray)
SumVal = SumVal + theArray(i)
Next i
split_average = SumVal / (UBound(theArray) + 1)
End Function
Of course, there's no type checking and so on, so try on your own risk. To use it, just copy it into the StarBasic Standard module, save, and call it inside your spreadsheet using =split_average(A1). For user-defined functions in general, see the LO Calc docs.
I know this is an old thread, but let me add my two cents. Use FILTERXML and some XPATH magic =)
=FILTERXML("<t><s>"&REGEX(A1;"-";"</s><s>";"g")&"</s></t>";"sum(//s) div count(//s)")
We could even implement a check on numeric nodes:
=FILTERXML("<t><s>"&REGEX(A1;"-";"</s><s>";"g")&"</s></t>";"sum(//s[.*0=0]) div count(//s[.*0=0])")
Looking at this I find it unfortunate Excel won't allow direct manipulation inside the expression itself. Suprised LibreOffice does!

VBA: assign string character by character

Most people ask how to get the characters from a string, which can be done by Mid(). I am trying to assign a string character by character in VBA code. The characters to be assigned depend on some calculated results.
I do not want to use string concatenation to form the string.
I have searched the web, but the posted solution, strName.Chars(i) (e.g., at MS development network), is not recognized in my 2007 Access VBA.
Thanks
You can use Mid to set values, too.
Sub showMidExample()
Dim s As String
s = "aaaaa"
Dim i As Integer
For i = 1 To Len(s)
Mid(s, i) = "n"
Debug.Print s
Next i
End Sub
This prints out
naaaa
nnaaa
nnnaa
nnnna
nnnnn
Which is what you are looking for.
Since no working answer is posted. I assume that cannot be done in VBA and I have to use concatenation to form the string although that is cumbersome in my case.

Len Function to find a Mid?

I am trying to pull out a piece of a string to no avail.
string = 90-8566-Doe-20140317_122627T-2_Update.pdf
I want to pull out the "Doe portion"
I have been using a Mid function with a Len function component and it just isn't coming out right.
Split the array by the hyphen character.
Then your value is the 3rd value in the array.
Assuming first 8 characters are always the same:
=MID($A$1,9,FIND("-",$A$1,9)-9)
Or in VBA:
Range("A2") = Mid(Range("A1"), 9, InStr(9, Range("A1"), "-") - 9)

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

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,

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