Solved

Lookup value in 2 column Text file from value in Listbox

Posted on 2013-02-01
2
302 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
ID: 38845751
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
ID: 38846098
Beautiful...Thank You !
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

919 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

19 Experts available now in Live!

Get 1:1 Help Now