Solved

Pull Check Number from string

Posted on 2009-04-10
7
368 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:mlcktmguy
  • 4
  • 2
7 Comments
 
LVL 7

Expert Comment

by:HoweverComma
ID: 24121012
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

0
 
LVL 7

Expert Comment

by:HoweverComma
ID: 24121028
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

0
 
LVL 65

Accepted Solution

by:
rockiroads earned 125 total points
ID: 24121146
if u want everything after the word CHECK, you can use the split command
eg Ive added 2 return values, one to return everything after the word CHECK, and one to return just the 4 digit number. See the code comments then remove the line not required
Also taken into account the word CHECK not found, just in case.

Add this code in a module, save it (ensure module name not the same as function name) then call it passing in your complete check string. This can be called from a query also. You might want to remove the msgbox "No  check found" if that warning message bugs you.


Public Function GetCheckNumber(ByVal sCheck As String)

    Dim sWords() As String
   
    If InStr(1, sCheck, "CHECK") = 0 Then
        GetCheckNumber = ""
        MsgBox "No CHECK word found"
    Else
        sWords = Split(sCheck, "CHECK")
       
        'To return everything after the word CHECK, use this
        GetCheckNumber = Trim$(sWords(1))
       
        'To return just the 4 digits after the word CHECK, use this
        GetCheckNumber = left$(Trim$(sWords(1)), 4)
    End If
End Function

0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 1

Author Comment

by:mlcktmguy
ID: 24122613
The routine from HoweverComma doesn't work in all cases, it picks up strays.

rockiroads is correct.
0
 
LVL 7

Expert Comment

by:HoweverComma
ID: 24123965
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.

0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 24124047
I used it on my data and it most definitely picks up strays.
0
 
LVL 7

Expert Comment

by:HoweverComma
ID: 24124422
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.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question