First time posting a question here. Excited to be a part of the community!
I have a sticky situation where converting data for a client has me stumped.
I need to find a way to not only split a cell by multiple Delimiters, but some data points need to be removed as well. In this situation, we're talking about a list of tenants and any additional occupants on a lease.
This is how the data was exported and in its current state
You'll see that there is at least one common delimiter I can use here. The ": " would allow me to simply split the names, but the real issue here is that the tenant names listed under the data points "son" or "daughter" need to be removed. On the other hand, the tenants listed as "resident" or "tenant" must be kept.
An example of how I'd want this to look
You'll see that the names are split and only the names listed under "tenant" or "resident" are kept and the others are dropped.
I've tried to find and replace the terms I want to keep with a simple character I can use to split, but the issue is that there isn't a consistent sequence for me to use this. I've been looking for a split VBA function that would work as well, but haven't had any luck.
Thoughts here would be welcome!
I think this function is probably an over-kill. But it does what you want.
Function SplitCellValue(ByVal CellVal As String) As String
'137
Const Qualifiers As String = "tenant,resident"
Dim Fun() As String ' function return array
Dim n As Integer ' index of Fun()
Dim Qword() As String ' split Qualifiers
Dim q As Integer ' index of Qword
Dim Sp() As String ' split array
Dim i As Integer ' index of Sp()
Dim Skip As Boolean ' True if entry doesn't qualify
Dim Append As Boolean ' True to append unqualified entry to previous
If Len(CellVal) Then ' skip blank CellVal
Qword = Split(Qualifiers, ",")
Sp = Split(Trim(CellVal), ",")
ReDim Fun(1 To UBound(Sp) + 1)
For i = 0 To UBound(Sp)
If InStr(Sp(i), ":") Then
For q = LBound(Qword) To UBound(Qword)
Skip = CBool(InStr(1, Sp(i), Qword(q), vbTextCompare))
If Skip Then Exit For
Next q
If Skip Then
n = n + 1
Fun(n) = Trim(Sp(i))
Append = True
Else
Append = False
End If
Else
If n = 0 Then
' preserve unqualified, leading entry
n = 1
Append = True
End If
If Append Then
If Len(Fun(n)) Then Fun(n) = Fun(n) & ", "
Fun(n) = Fun(n) & Trim(Sp(i))
End If
End If
Next i
End If
If i Then
ReDim Preserve Fun(1 To n)
SplitCellValue = Join(Fun, ", ")
End If
End Function
List all the qualifiers in the constant Qualifiers
. Everything else will be rejected. Qualifiers only qualify an element if they appear in the same comma-separated element as a colon. (I probably might as well have combined the two but I didn't.)
You can use this function in VBA with a call like this one.
Private Sub Test_SplitCellValue()
Dim R As Integer
For R = 2 To 5
Debug.Print i, SplitCellValue(Cells(R, 1).Value)
Next R
End Sub
or as a UDF, called like, =SplitCellValue(A2)
. Install in a standard code module for this use.
The over-kill is in what the function can do that is probably not needed. (1) A blank cell will return a blank. (2) A non-blank cell will return its original value if no colon is found in it. (3) Anything before a first element with a colon in it will be included in the return. (4) blanks in the original are removed and replaced by blanks following commas which themselves are removed and then placed between qualifying elements. All of this takes a lot of code that may have to be revised if your data or expectations defy my anticipation.
So after some trial and error, this was much simpler than I was thinking. There were two different instances of ", " & "," (one had a space behind) being used in different manners.
I was able to find and replace for the instance of comma with a space, keeping the detail I needed intact, while delimiting the "," without a space.
Thanks!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.