Solved

Lookup value in 2 column Text file from value in Listbox

Posted on 2013-02-01
2
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

733 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