add to character to string in query

zezo2021

Member
Local time
Today, 16:08
Joined
Mar 25, 2021
Messages
412
friends
if I have a column containing a string like this
CLE Credit (50min)(29601),CLE Credit (60min)(29601),Attendance(:_____)

I want to create an expression column to check if (2961) is a number only add bb to this number

for example, the string above will be

CLE Credit (50min)(BB29601),CLE Credit (60min)(BB29601),Attendance(:_____)


IS THIS POSSIBLE


THANK YOU IN ADVANCE
 
was going to answer, but will leave for Arnel
 
Have you tried Replace()?
 
Can this code help


Code:
this code may help
Public Function fnGetState_ID(ByVal strText) As Variant
    ' the function will return a
    ' 1 dimension array in State, ID Pair.
    Dim arr() As String
    Dim allMatches As Variant, result As Variant
    Dim i As Integer, j As Integer, k As Integer
    ' create a big array
    ReDim arr(1000)
    fnGetState_ID = Null
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "([A-Z\ ]{2,3})([0-9]{4,10})"
        Set allMatches = .Execute(strText)
        i = -1
        For j = 0 To allMatches.Count - 1
            For k = 0 To allMatches.Item(j).submatches.Count - 1
                i = i + 1
                arr(i) = Trim$(allMatches.Item(j).submatches.Item(k))
            Next
        Next
    End With
    If i > -1 Then
        ReDim Preserve arr(i)
        fnGetState_ID = arr
    End If
End Function
 
Can this code help


Code:
this code may help
Public Function fnGetState_ID(ByVal strText) As Variant
    ' the function will return a
    ' 1 dimension array in State, ID Pair.
    Dim arr() As String
    Dim allMatches As Variant, result As Variant
    Dim i As Integer, j As Integer, k As Integer
    ' create a big array
    ReDim arr(1000)
    fnGetState_ID = Null
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "([A-Z\ ]{2,3})([0-9]{4,10})"
        Set allMatches = .Execute(strText)
        i = -1
        For j = 0 To allMatches.Count - 1
            For k = 0 To allMatches.Item(j).submatches.Count - 1
                i = i + 1
                arr(i) = Trim$(allMatches.Item(j).submatches.Item(k))
            Next
        Next
    End With
    If i > -1 Then
        ReDim Preserve arr(i)
        fnGetState_ID = arr
    End If
End Function
It can help depending on your needs. Without really knowing what that is, we can't say for sure if that code will cover it.
 
friends
if I have a column containing a string like this
CLE Credit (50min)(29601),CLE Credit (60min)(29601),Attendance:)_____)

I want to create an expression column to check if (2961) is a number only add bb to this number

for example, the string above will be

CLE Credit (50min)(BB29601),CLE Credit (60min)(BB29601),Attendance:)_____)


IS THIS POSSIBLE


THANK YOU IN ADVANCE
 
here test this:
Code:
Public Function fnGetState_ID(ByVal strText) As Variant
    ' the function will return a
    ' 1 dimension array in State, ID Pair.
    Dim arr() As String, s As String
    Dim allMatches As Variant, result As Variant
    Dim i As Integer, j As Integer, k As Integer
    ' create a big array
    ReDim arr(1000)
    fnGetState_ID = Null
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "([A-Z\ ]{2,3})([0-9]{4,10})|(\([0-9]{4,10}\))"
        Set allMatches = .Execute(strText)
        i = -1
        For j = 0 To allMatches.Count - 1
            For k = 0 To allMatches.Item(j).submatches.Count - 1
                s = Trim$(allMatches.Item(j).submatches.Item(k))
                If Len(s) <> 0 Then
                    i = i + 1
                    If s = "(29601)" Then
                        arr(i) = "BB"
                        i = i + 1
                        arr(i) = "29601"
                    Else
                        arr(i) = s
                    End If
                End If
            Next
        Next
    End With
    If i > -1 Then
        ReDim Preserve arr(i)
        fnGetState_ID = arr
    End If
End Function
 
here test this:
Code:
Public Function fnGetState_ID(ByVal strText) As Variant
    ' the function will return a
    ' 1 dimension array in State, ID Pair.
    Dim arr() As String, s As String
    Dim allMatches As Variant, result As Variant
    Dim i As Integer, j As Integer, k As Integer
    ' create a big array
    ReDim arr(1000)
    fnGetState_ID = Null
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "([A-Z\ ]{2,3})([0-9]{4,10})|(\([0-9]{4,10}\))"
        Set allMatches = .Execute(strText)
        i = -1
        For j = 0 To allMatches.Count - 1
            For k = 0 To allMatches.Item(j).submatches.Count - 1
                s = Trim$(allMatches.Item(j).submatches.Item(k))
                If Len(s) <> 0 Then
                    i = i + 1
                    If s = "(29601)" Then
                        arr(i) = "BB"
                        i = i + 1
                        arr(i) = "29601"
                    Else
                        arr(i) = s
                    End If
                End If
            Next
        Next
    End With
    If i > -1 Then
        ReDim Preserve arr(i)
        fnGetState_ID = arr
    End If
End Function
Hello

Can this code work with the entire column not specific Value 29601


I need it for column strings not individual string


Thank you
 
Code:
Public Function fnGetState_ID(ByVal strText) As Variant
    ' the function will return a
    ' 1 dimension array in State, ID Pair.
    Dim arr() As String, s As String
    Dim allMatches As Variant, result As Variant
    Dim i As Integer, j As Integer, k As Integer
    ' create a big array
    ReDim arr(1000)
    fnGetState_ID = Null
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "([A-Z\ ]{2,3})([0-9]{4,10})|(\([0-9]{4,10}\))"
        Set allMatches = .Execute(strText)
        i = -1
        For j = 0 To allMatches.Count - 1
            For k = 0 To allMatches.Item(j).submatches.Count - 1
                s = Trim$(allMatches.Item(j).submatches.Item(k))
                If Len(s) <> 0 Then
                    i = i + 1
                    If s Like "(*)" Then
                        arr(i) = "BB"
                        i = i + 1
                        arr(i) = Replace$(Replace$(s, "(", ""), ")", "")
                    Else
                        arr(i) = s
                    End If
                End If
            Next
        Next
    End With
    If i > -1 Then
        ReDim Preserve arr(i)
        fnGetState_ID = arr
    End If
End Function
 
Code:
Public Function fnGetState_ID(ByVal strText) As Variant
    ' the function will return a
    ' 1 dimension array in State, ID Pair.
    Dim arr() As String, s As String
    Dim allMatches As Variant, result As Variant
    Dim i As Integer, j As Integer, k As Integer
    ' create a big array
    ReDim arr(1000)
    fnGetState_ID = Null
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "([A-Z\ ]{2,3})([0-9]{4,10})|(\([0-9]{4,10}\))"
        Set allMatches = .Execute(strText)
        i = -1
        For j = 0 To allMatches.Count - 1
            For k = 0 To allMatches.Item(j).submatches.Count - 1
                s = Trim$(allMatches.Item(j).submatches.Item(k))
                If Len(s) <> 0 Then
                    i = i + 1
                    If s Like "(*)" Then
                        arr(i) = "BB"
                        i = i + 1
                        arr(i) = Replace$(Replace$(s, "(", ""), ")", "")
                    Else
                        arr(i) = s
                    End If
                End If
            Next
        Next
    End With
    If i > -1 Then
        ReDim Preserve arr(i)
        fnGetState_ID = arr
    End If
End Function
testing
 
Whilst I'm sure @arnelgp will work it out, us normal mortals really need to see your starting data and what you expect to see out as a result, more than one example to see a pattern.
 
Code:
Public Function fnGetState_ID(ByVal strText) As Variant
    ' the function will return a
    ' 1 dimension array in State, ID Pair.
    Dim arr() As String, s As String
    Dim allMatches As Variant, result As Variant
    Dim i As Integer, j As Integer, k As Integer
    ' create a big array
    ReDim arr(1000)
    fnGetState_ID = Null
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "([A-Z\ ]{2,3})([0-9]{4,10})|(\([0-9]{4,10}\))"
        Set allMatches = .Execute(strText)
        i = -1
        For j = 0 To allMatches.Count - 1
            For k = 0 To allMatches.Item(j).submatches.Count - 1
                s = Trim$(allMatches.Item(j).submatches.Item(k))
                If Len(s) <> 0 Then
                    i = i + 1
                    If s Like "(*)" Then
                        arr(i) = "BB"
                        i = i + 1
                        arr(i) = Replace$(Replace$(s, "(", ""), ")", "")
                    Else
                        arr(i) = s
                    End If
                End If
            Next
        Next
    End With
    If i > -1 Then
        ReDim Preserve arr(i)
        fnGetState_ID = arr
    End If
End Function
Done
(y) (y) (y) (y)
 
@arnelgp


can you explain this line of code
.Pattern = "([A-Z\ ]{2,3})([0-9]{4,10})|(\([0-9]{4,10}\))"
 
@arnelgp


can you explain this line of code
.Pattern = "([A-Z\ ]{2,3})([0-9]{4,10})|(\([0-9]{4,10}\))"
Hi. You can also plug your expression on this website and get an explanation. Just a thought...

 
Hi. You can also plug your expression on this website and get an explanation. Just a thought...

goo site but didn't get satisfactory information



What about the space

CLE Credit (50min)(MT 29601),CLE Credit (60min)(MT 29601),Attendance:)_____)

Is there problem if there is a space between text and number?

@arnelgp

can you comment the code so I can understand and edit it later?

Thank you
 

Users who are viewing this thread

Back
Top Bottom