Solved

Lookup value in 2 column Text file from value in Listbox

Posted on 2013-02-01
2
290 Views
Last Modified: 2013-02-01
excel 2010  vba

Listbox1 on userform multipage
"Gsku" is a number for a specifc jpg file.

I get a value from a listbox for passing as a variable...
which is 'Gsku'
    Gsku = frmResultAll.ListBox1.Column(0)
   
' ok now that i know the sku number look it up to get the correct jpg
' Take value for Gsku and look into a text file located
' C:\Program Files\Enterprise\Databases\Image_Relationship.txt
' This text file has 2 columns with headers.  Pipe delimiter
' material_no  |   primary_image
I need to find Gsku in Column "material_no"
' and then Gsku will  =  "primary_Image"
 
material_no|primary_image
10A001|10A001_AS01.JPG
10A002|10A002_AS01.JPG
10A003|10A002_AS01.JPG
10A004|10A002_AS01.JPG
10A005|10A002_AS01.JPG
10A006|10A002_AS01.JPG
10A007|10A002_AS01.JPG
10A008|10A008_AS01.JPG
10A009|10A009_AS01.JPG
10A010|10A010_AS01.JPG
10A011|10A010_AS01.JPG
10A012|10A010_AS01.JPG
10A013|10A013_AS01.JPG


i.e. So if Gsku  =  10A005  THEN perform search and
Gsku = 10a002_AS01.jpg

So if Gsku  =  10A011  THEN perform search and
Gsku = 10a010_AS01.jpg

Fyi, I have 156,000 records in this text file...


Thanks
fordraiders.
0
Comment
Question by:fordraiders
2 Comments
 
LVL 14

Accepted Solution

by:
Faustulus earned 500 total points
Comment Utility
This is the search function you want:-
Private Function ImageFileName(MaterialNum As String, _
                               LookupFile As String)
                                      
    Const ForReading As Long = 1

    Dim FSO As Object
    Dim TxtFile As Object
    Dim TxtLine As String

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set TxtFile = FSO.OpenTextFile(LookupFile, ForReading)

    Do Until TxtFile.AtEndOfStream
        TxtLine = TxtFile.ReadLine
        If InStr(TxtLine, MaterialNum) = 1 Then
            ImageFileName = Split(TxtLine, "|")(1)
            Exit Do
        End If
    Loop
    TxtFile.Close
End Function

Open in new window

I didn't include the text file's name in it because it will be better to have that name somewhere at the top of your code. In the attached demonstration workbook it is in the procedure Test.

Let me know if you need help in integrating this procedure into your project.
130201-Lookup-TXT-File.xlsm
0
 
LVL 3

Author Closing Comment

by:fordraiders
Comment Utility
Beautiful...Thank You !
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now