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
Solved

Lookup value in 2 column Text file from value in Listbox

Posted on 2013-02-01
2
317 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

861 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