Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

Lookup value in 2 column Text file from value in Listbox

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
Fordraiders
Asked:
Fordraiders
1 Solution
 
FaustulusCommented:
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
 
FordraidersAuthor Commented:
Beautiful...Thank You !
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now