1

I have thousands of lines of text that I need to work through and the lines I am interested with lines that look like the following:

01/04/2019 09:35:41 - Test user (Additional Comments)

I am currently using this code to filter out all the other rows:

If InStr(FullCell(i), " - ") <> 0 And InStr(FullCell(i), ":") <> 0 And InStr(FullCell(i), "(") <> 0 Then

FullCell is the array that I am working through.

which I know is not the best way to do it. Is there a way to check that there is a date at the beginning of the string in the format dd/mm/yyyy and then extract the user name inbetween the '-' and the '(' symbol.

I had a play with regex to see if that could help but i'm limited in skills to be able to pull off both VBA and regex in the same code.

Whats the best way to do this.

2
  • 1
    Try a regex like ^\d?\d\/\d?\d\/\d\d\d\d[^-]*-([^(]*)\(.* where the name is in group 1 Commented May 13, 2019 at 17:45
  • 1
    Msgbox CDate(Left("01/04/2019 09:35:41 - Test user (Additional Comments)", 19)) Commented May 13, 2019 at 18:33

4 Answers 4

3

Assuming Fullcell(i) contains the string,

If Left(Fullcell(i), 10) Like "##/##/####"

Will return True if you have a date (note that it will not differentiate between dd/mm/yyyy and mm/dd/yyyy.

And

Mid(Fullcell(i), InStr(Fullcell(i), " - ") + 2, InStr(Fullcell(i), " (") - InStr(Fullcell(i), " - ") - 2)

Will return the username

Sign up to request clarification or add additional context in comments.

Comments

2

I'm sure there is a more efficient way to do this, but I've used the following solution quite a few times:

This will select the date:

x = 1
Do While Mid(FullCell,1,x) <> " "
    x = x + 1
Loop
strDate = Left(FullCell,x)

This will find the character number of the hyphen, the username starts 2 characters after.

x = 1
Do While Mid(FullCell,x,1) <> "-"
    x = x + 1
Loop

Then we will find the end of the username

y = x + 2
Do While Mid(FullCell,y,1) <> " "
    y = y + 1
Loop

The username should now be characters (x+2 to y-1)

strUsername = Mid(FullCell, x + 2, y - (x + 2) - 1)

1 Comment

Is there a reason why you are using a loop instead of Instr to determine the first ocurence of the searched for string?
2

Here's how I would do it

Dim your variables

Dim ring as Range
Dim dat as variant
Dim FullCell() as string
Dim User as string
Dim I as long

Set your range

Set rng = ` any way you choose
Dat = rng.value2

Loop dat

For i = 1 to UBound(dat, 1)

Split the data

    FullCell = Trim(Split(FullCell, "-"))

Test if it split

    If UBound(FullCell) > 0 Then

Test if it matches

    If IsDate(FullCell(0)) Then
        i  = Instr(FullCell(1), "(")-1)
        If i  then
            User = left$(FullCell(1), i)
            '  Found a user 

        End If
    End If

    End If

Next

Comments

1

Abstraction is your friend, it's always helpful to break these into their own private functions whenever you can. You could put your code in a function and call it something like ExtractUsername.

Below I did an example of this, and I decided to go with the RegExp approach (late binding), but you could use string functions like the examples above as well.

This function returns the username if it finds the pattern you mentioned above, otherwise, it returns an empty string.

Private Function ExtractUsername(ByVal SourceString As String) As String

    Dim RegEx As Object
    Set RegEx = CreateObject("vbscript.regexp")

    '(FIRST GROUP FINDS THE DATE FORMATTED AS DD/MM/YYY, AS WELL AS THE FORWARD SLASH)
    '(SECOND GROUP FINDS THE USERNAME) THIS WILL BE SUBMATCH 1
    With RegEx
        .Pattern = "(^\d{2}\/\d{2}\/\d{4}.*-)(.+)(\()"
        .Global = True
    End With

    Dim Match As Object
    Set Match = RegEx.Execute(SourceString)

    'ONLY RETURN IF A MATCH WAS FOUND
    If Match.Count > 0 Then
        ExtractUsername = Trim(Match(0).SubMatches(1))
    End If

    Set RegEx = Nothing

End Function

The regex pattern is grouped into three parts, the date (and slash), username, and opening parentheses. What you are interested in is the username, which in the SubMatch would be number 1.

Regexr is a helpful site for practicing regular expressions and can show you a bit more of what the pattern I went with is doing.

Please note that using regular expressions might give you performance issues and you should test it against regular string functions to see what works best for your situation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.