Solved

Find data in a Excel spreadsheet and display it in a userform

Posted on 2011-03-22
4
416 Views
Last Modified: 2012-05-11
Hi,

I have a spreadsheet containing a lot of data. I would like to search a column (column A) for a number entered in an userform and return the value in the corresponding cell next to it (column B) to a textbox in the userform.

How do I do that?
0
Comment
Question by:Christian_G
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 35189953
let's suppose that the textbox where the value will be entered is textbox1 and the result will be displayed is called textbox2.
dim result as variant
dim lookupvalue as variant

lookupvalue = textbox1.text
result = application.worksheetfunction.vlookup(lookupvalue,range("A:B"),2,0)

textbox2.text = result

Open in new window

0
 

Author Comment

by:Christian_G
ID: 35196426
I changed the lookupvalue data type to Double as all data is decimal numbers.
But what if I also would like to return values from column D and G?
0
 
LVL 6

Accepted Solution

by:
FernandoFernandes earned 500 total points
ID: 35198392
then you'll have to
1) declare new variables to populate those results
2) determine to which textbox'es those results will be win
3) adapt the vlookup's parameters to the new realities, for instance:

result1 = application.worksheetfunction.vlookup(lookupvalue,range("A:B"),2,0)
result2 = application.worksheetfunction.vlookup(lookupvalue,range("A:D"),4,0)
result3 = application.worksheetfunction.vlookup(lookupvalue,range("A:G"),7,0)

textbox2.text = result1
textbox3.text = result1
textbox4.text = result1


if you're good at working with vlookup inside Excel, you'll see that from within the code it's pretty much the same thing :-)

p.s.: the names I used are examples, in your code you'll probably have to adapt it...
0
 

Author Closing Comment

by:Christian_G
ID: 35200258
Thanks!
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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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 …
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…

863 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

20 Experts available now in Live!

Get 1:1 Help Now