By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

Reading data from excel file

P: 2
Hi please help
i'm using VB 2005
I have displayed names in to a listbox from excel file, now i want to do the following when name is clicked in the listbox

Display details of the name into label, such as
Innitials,Surname and ID number

thanx
Dixon
Jan 16 '08 #1
Share this Question
Share on Google+
1 Reply


kadghar
Expert 100+
P: 1,295
Hi please help
i'm using VB 2005
I have displayed names in to a listbox from excel file, now i want to do the following when name is clicked in the listbox

Display details of the name into label, such as
Innitials,Surname and ID number

thanx
Dixon
i will supose you have an excel worksheet with 4 columns: Name, Initials, Surname, ID number, one afther other, this means, you have no blank rows between names.

if you have the column called Name in a listbox, then what i suggest you to do is to create an object with an excel application, here you'll open the file with the info, then add to a Variant (... object in 2005) the range with info, and search there, something like this will do:

On the Listbox1 double click event write:

Expand|Select|Wrap|Line Numbers
  1. dim obj1, a as object
  2. dim i as long
  3. dim Str1 as string
  4. obj1= createobject("excel.application")
  5. obj1.workbooks.open("c:\mybook.xls")
  6. with obj1.activeworkbook.worksheets("sheet1")
  7.     a = obj1.range(.cells(1,4), .cells(1,1).end(-4121))
  8. end with
  9. for i = 1 to ubound(a)
  10.     if a(i,1) = listbox1.selecteditem then 
  11.         msgbox (a(i,2) & " " & a(i,3)  & " " & a(i,4)
  12.         exit for
  13.     end if
  14. next
Well, i dont remember quite well all the sintax for listboxes in 2005, and as long as i remember there's no need to put SET obj1= createobject.(...) that's why i didnt write it, but it might need it as well.
don't forget to close the excel application,
i like doing it this way:

obj1.visible=true
obj1.close

HTH.
Jan 16 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.