Link to home
Start Free TrialLog in
Avatar of mlcktmguy
mlcktmguyFlag for United States of America

asked on

Pull Check Number from string

I am trying to simplify a check reconcilliattion process for one of my customers.  They download their online banking statements as an Excel spreadhseet which we then import into a MS access table.  One of the fields is 'Description'.  The check number lies within the description field.  I want to pull the check numbers as a separate field that the user can sort on and use for reconcilation.  There are many different 'Descriptions' and types of checks.  The only thing that is consistent among all of the different types is that the 4 digit check number always follows the literal string "CHECK".  There is always at least one space between "CHECK" and the check number but there may be several.  Examples:
CHECK 1234
CHECK         1234
CHECK                   1234
are all possible.

There also may be information on the line preceeding "CHECK' and following the check number.  There will always be at least one space after the check number if another field follows it.  Examples:
Blah Blah CHECK 1234 Blah
CHECK         1234 Blah
Blah CHECK                   1234
are all possible.

Any quicky way to pull the check number from this string?
Avatar of HoweverComma
HoweverComma

Pretty easy to do the following code should get you on your way

Things to note:
My example checks Cell A1 you will need to replace the row with a variable and use a Do While loop or something similar.
 a space needs to be appended to the string that contains the line with check # or it will bomb in the case of your third example.
It is a case sensitive search by default.

Based on your examples the check number is always one space after 'check' and check always has a space after it.

It can be done on a single line by replacing all instances of tmpstr1 with UCase(Cells(row,col))  but unless it is running 10's of thousands of checks I doubt you will notice any difference.
Function GetChkNum()
Dim tmpstr1 As String
tmpstr1 = UCase((Cells(1, 1)))
GetChkNum = Mid(tmpstr1, InStr(tmpstr1, " CHECK ") + 7, InStr(InStr(tmpstr1, " CHECK ") + 7, tmpstr1, " ") - InStr(tmpstr1, " CHECK ") - 7)
End Function

Open in new window

Scratch that one comment about a single space after 'check'.
You'll have to use a fix to remove the multiple spaces.

Function GetChkNum()
Dim tmpstr1 As String
tmpstr1 = UCase((Cells(1, 1)))
While InStr(tmpstr1, "  ")
  tmpstr1 = Replace(tmpstr1, "  ", " ")
Wend
GetChkNum = Mid(tmpstr1, InStr(tmpstr1, " CHECK ") + 7, InStr(InStr(tmpstr1, " CHECK ") + 7, tmpstr1, " ") - InStr(tmpstr1, " CHECK ") - 7)
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mlcktmguy

ASKER

The routine from HoweverComma doesn't work in all cases, it picks up strays.

rockiroads is correct.
I can not think of an instance where mine picks up strays.
A line in my revised code was improper
tmpstr1 = UCase((Cells(x, Y)))

should have been

tmpstr1 = UCase((Cells(x, Y))) + " "

And once you go beyond 4 numbers rockiroads solution is going to return improper results so if you use that it is going to be a problem in the future use it at your own peril.

I have used the funtion I gave you for many years and it does not pick up strays.

I used it on my data and it most definitely picks up strays.
Then by all means post a sample of something that it does, if it is broken I'd like to see how and why.
It is easy enough to post a sample of something that it will not work on.