Len Function to find a Mid? - string

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)

Related

Best formula/method to extract a standard set of numbers from a string?

I have the following strings from which I need to extract 6 digit numbers. Since these strings are generated by another software, they occur interchangeably and I cannot control it. Is there any one method that would extract both 6-digit numbers from each of these strings?
Branch '100235 to 100236 Ckt 1' specified in table 'East Contingency' for record with primary key = 21733 was not found in branch or transformer data.
Loadflow branch ID '256574_701027_1' defined in supplemental branch table was not found in branch or transformer input.
Transmission element from bus number 135415 to bus number 157062 circuit ID = 1 defined for corridor 'IESO-NYISO' was not found in input data
I don't know VBA, but I can learn it if it means I can get the 6 digit numbers using a single method.
thanks
I have been using LEFT(), RIGHT() & MID() previously, but it means manually applying the appropriate formula for individual string.
If you have Microsoft 365, you can use this formula:
=LET(arr,TEXTSPLIT(SUBSTITUTE(SUBSTITUTE(A1,"'"," "),"_"," ")," "),
FILTER(arr,ISNUMBER(-arr)*(LEN(arr)=6)))
Thanks to #TomSharpe for this shorter version, using an array constant within TEXTSPLIT to add on possible delimiters.
=LET(arr,TEXTSPLIT(A1,{"_"," ",","," ","'"}),FILTER(arr,(LEN(arr)=6)*ISNUMBER(-arr)))
Data
Output
An alternative is:
=LET(ζ,MID(A1,SEQUENCE(,LEN(A1)-5),6),ξ,MID(ζ,SEQUENCE(6),1),FILTER(ζ,MMULT(SEQUENCE(,6,,0),1-ISERR(0+ξ))=6))
A couple more suggestions (if you need them):
(1) Replacing all non-digit characters with a space then splitting the resulting string:
=LET(numbers,TEXTSPLIT(TRIM(REDUCE("",MID(A1,SEQUENCE(1,LEN(A1)),1),LAMBDA(a,c,IF(is.digit(c),a&c,a&" "))))," "),FILTER(numbers,LEN(numbers)=6))
Here I've defined a function is.digit as
=LAMBDA(c, IF(c = "", FALSE, AND(CODE(c) > 47, CODE(c) < 58)))
(tl;dr I quite like doing it this way because it hides the implementation details of is.digit and creates a rudimentary form of encapsulation)
(2) A UDF - based on the example here and called as
=RegexTest(A1)
Option Explicit
Function RegexTest(s As String) As Double()
Dim regexOne As Object
Dim theNumbers As Object
Dim Number As Object
Dim result() As Double
Dim i As Integer
Set regexOne = New RegExp
' Not sure how you would extract numbers of length 6 only, so extract all numbers...
regexOne.Pattern = "\d+"
regexOne.Global = True
regexOne.IgnoreCase = True
Set theNumbers = regexOne.Execute(s)
i = 1
For Each Number In theNumbers
'...Check the length of each number here
If Len(Number) = 6 Then
ReDim Preserve result(1 To i)
result(i) = CDbl(Number)
i = i + 1
End If
Next
RegexTest = result
End Function
Note - if you wanted to preserve leading zeroes you would need to omit the Cdbl() and return the numbers as strings. Returns an error if no 6-digit numbers are found.

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

Substring to get 3 characters from the last character vb.net

i have the following string "USB SERIAL PORT (COM6)" i want to get COM6 out of this.
This is the code i am trying to use
string.substring(3, string.length - 1)
something of that sort. Havent been able to get anything.
You'd be better off using the last occurrence of ( as an index, in case one day your string changes format, or com6 is longer (com10):
Dim lastBra as Integer = myString.LastIndexOf("("c)
Dim lastKet as Integer = myString.LastIndexOf(")"c)
Dim subs as String = myString.Substring(lastBra + 1, lastKet - lastBra - 1)
It's lastBra+1 because we want the character after the open bracket as a start. The length to substring is the bracket indexes, less one because we don't want the last bracket to be included:

Operator not followed by a character

I am using the find and replace function and a vba code in Excel. I want to replace all strings like "/15" by ".15" but only if "/15" is not followed by any other characters. Is there an operator for my need?
For example if I replace all "/15" it also replaces this string if it is followed by other characters.
10/15/15 -> 10.15.15
But what I want is
10/15/15 -> 10/15.15
Cheers
You could use regular expressions, a wealth of stuff on the net about that.
Or something in VBA like so, no real need for a2, could grab the last of a1 before hand, but just to show you difference in the arrays in the locals window.
Function test(strInput As String)
Dim a1() As String
Dim a2() As String
a1 = Split(strInput, "/")
a2 = a1
ReDim Preserve a2(UBound(a1) - 1)
test = Join(a2, "/") & "." & a1(UBound(a1))
End Function
or it can be done using a formula
=SUBSTITUTE(A1,"/",".",(LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))))
I think you should use the function Right for this, to avoid replacing the "/15" in the middle of the string.
Public Function ReplaceRight(strInput As String) As String
If Right(strInput, 3) = "/15" Then
ReplaceRight = Left(strInput, Len(strInput) - 3) & ".15"
Else
ReplaceRight = strInput
End If
End Function
With Excel Formula:
=IF(RIGHT(F215,3)="/15",LEFT(F215,LEN(F215)-3)&".15",F215)

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.

Resources