find 12 characters in a string

is it possible to find 12 characters within a string, [but with a catch]

I will have a lookup table that will have a list of shrortened currency codes, so US, GB, CH, HK etc.

I need to look up this table and the find the code within the string that starts with US or GB or CH etc

so if the cell contains this:

GSB142839981001 //TAG1410210509596 1710598400 US455760CW45 SALE [in column 'N in sheet called RawData]

I need to extract US455760CW45

The Static codes will be in a sheet called Static in column 'A'

The cell containing the data will be in a sheet called RawData in Column 'N'

and I need to put the resultant answer [US455760CW45] in column 'O'

Is this doable?

Thanks in advance
JagwarmanAsked:
Who is Participating?
 
Robberbaron (robr)Connect With a Mentor Commented:
ok, the reason was that you added blank cells to the CurrencyTable.

you want this table to be as small as possible to keep speed ok as it search's for each and every cell, incl blank ones.

I have added a check for blank that fixes the issue for now
    'try to find a currency code in the text (preceded by space)
    For Each xCell In Range("CurrencyTable")
        curTxt = xCell.Value
        ix = InStr(1, myValue, vbSpace & curTxt)
        If ix > 0 And curTxt <> "" Then    '<<<<added check for curTxt is empty
            'found the code at least
            
            'now check for exactly 10 characters before next space
            If InStr(ix + 2, myValue, vbSpace) = ix + 13 Then
                'extract the data
                GetCurrencyCode = Mid$(myValue, ix + 1, 12)
                Exit Function
            End If
        End If
        
    Next xCell

Open in new window

0
 
ProfessorJimJamCommented:
Do u want VBA solution or formula?
0
 
slubekCommented:
See attached file.
t5.xlsx
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
JagwarmanAuthor Commented:
ProfessorJimJam ideally VBA

thanks
0
 
JagwarmanAuthor Commented:
Also the data I am looking for could be anywhere in the string and not in the position I put it
0
 
JagwarmanAuthor Commented:
slubek I have to use a Static data file because there will be many different ones and although I can see what you have done there I cannot put them across the top of the file we will be downloading from our system
0
 
slubekCommented:
You can use addresses from different file in your formulas. So your formula can be in output file, and you don't even have to open input file to calculate it - it can stay as you got it (if I understand correctly what you mean by "Static data file").
0
 
Rob HensonFinance AnalystCommented:
Jagwarman - is it feasible/possible that a currency code combination could occur before the required string that you wish to extract. For example, using the sample in the question, what if the GSB at the start of your sample was actually GBS? The formula/VBA would then find the GB first.

Thanks
Rob H
0
 
JagwarmanAuthor Commented:
Rob that is possible but that is a chance I have to take. I mentioned this exact same thing to the user but they are happy to go with the 80/20 scanario because if we can automate this process it will save a huge amout of time. My belief is if the GSB was input as GBS when they review the 12 digit Reference it will pop up as invalid anyway.

Thanks
0
 
krishnakrkcCommented:
UDF

Function EXTRACT12(V) As String
    
    Dim i   As Long, Curr As String, x
    
    Curr = "US,GB,CH,HK"   '<< add more currency codes here
    
    If TypeOf V Is Range Then V = V.Value2
    x = Split(V)
    
    For i = 0 To UBound(x)
        If Len(x(i)) Then
            If InStr(1, Curr, Left(x(i), 2), 0) Then
                If Left(x(i), 3) Like "??#" Then
                    EXTRACT12 = x(i)
                    Exit Function
                End If
            End If
        End If
    Next
    
End Function

Open in new window


use like

=EXTRACT12(A2)

Kris
0
 
JagwarmanAuthor Commented:
krishnakrkc

thanks but I need to call the info from a Static Data table that the user would be able to update as and when necessary. I do not want them going inot the Macro code.
0
 
JagwarmanAuthor Commented:
Rob Hanson or krishnakrkc will you be picking up on this one for me?
0
 
Robberbaron (robr)Commented:
try this code as  function..

it looks for space + code  + space
Option Explicit
Function GetCurrencyCode(testCell As Range)

    Dim myValue As String, curTxt As String
    GetCurrencyCode = "Not found"
    Dim xCell As Range, ix As Integer
    Dim vbSpace As String
    vbSpace = Chr$(32)
    myValue = testCell.Value
    
    'try to find a currency code in the text (preceded by space)
    For Each xCell In Range("CurrencyTable")
        curTxt = xCell.Value
        ix = InStr(1, myValue, vbSpace & curTxt)
        If ix > 0 Then
            'found the code at least
            
            'now check for exactly 10 characters before next space
            If InStr(ix + 2, myValue, vbSpace) = ix + 13 Then
                'extract the data
                GetCurrencyCode = Mid$(myValue, ix + 1, 12)
                Exit Function
            End If
        End If
        
    Next xCell
    
End Function

Open in new window

findcurr-v2.xlsm
0
 
JagwarmanAuthor Commented:
Robberbaron

It is proberblt me but when I add new codes into the Tab Static it does not pick upi items starting with these codes. If I put the new code in cells A2, 3 or 4 it works fine but if I put the code in any other cell it does not work
0
 
JagwarmanAuthor Commented:
sorry one more thing. It seems like it does not like some codes.

 DK1234567811       DK1234567811
 CK1234567811       Not found
 JK1234567811       Not found


Static

CurrencyTable
US
DK
CH
HK
XS
JK
DK
CK

So DK works because it is in A3
0
 
Robberbaron (robr)Commented:
you have to extend the named range.
inset new rows in the middle of the range (ie between US & DK), as this automatically extends it.  then copy the new currency codes into the blank cells.

it should then update the found codes list.
findcurr-v3.xlsm
0
 
JagwarmanAuthor Commented:
brilliant thanks
0
 
JagwarmanAuthor Commented:
Rob I tried to 'accept' but for some reason it didn't work. In the meantime I have another problem. For some reason even if the two digit code is not in the Static table it is selecting the data from column N if there is a space after 12 characters.

i.e.

142909767.00 1 //8678008 and it selects 142909767.00
NCBHKH426598 8201 and it selects NCBHKH426598

any ideas ?

Thanks for your help
0
 
Robberbaron (robr)Commented:
i cant reproduce your problem. see attached sample with the data you indicated.

perhaps you need to load up the sample workbook with more real data and post it so can check further.
findcurr-v3a.xlsm
0
 
JagwarmanAuthor Commented:
Very odd. Thanks file attached
findcurr-v3b.xlsm
0
 
JagwarmanAuthor Commented:
Thanks Rob I think you have cracked. Thank you for your patience. Great Job
0
All Courses

From novice to tech pro — start learning today.