Link to home
Start Free TrialLog in
Avatar of Jagwarman
Jagwarman

asked on

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
Avatar of Professor J
Professor J

Do u want VBA solution or formula?
See attached file.
t5.xlsx
Avatar of Jagwarman

ASKER

ProfessorJimJam ideally VBA

thanks
Also the data I am looking for could be anywhere in the string and not in the position I put it
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
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").
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
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
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
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.
Rob Hanson or krishnakrkc will you be picking up on this one for me?
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
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
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
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
brilliant thanks
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
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
Very odd. Thanks file attached
findcurr-v3b.xlsm
ASKER CERTIFIED SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia 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
Thanks Rob I think you have cracked. Thank you for your patience. Great Job