Results 1 to 14 of 14

Thread: How to read .xls file using vb

  1. #1

    Thread Starter
    Member jalzaaal's Avatar
    Join Date
    Feb 2005
    Posts
    63

    How to read .xls file using vb

    Hi all,

    Is it possible to read an .xls file using vb unlike .txt file.

    What i want to do is .....
    i want to read the data of a cell of excel sheet and want to display it in a text box...... Is is possible?

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: How to read .xls file using vb

    Quote Originally Posted by jalzaaal
    Hi all,

    Is it possible to read an .xls file using vb unlike .txt file.

    What i want to do is .....
    i want to read the data of a cell of excel sheet and want to display it in a text box...... Is is possible?
    Couple ways to do this. First, you can use the Excel automation objects to open the workbook and retrieve the value. The example uses early binding, but can be changed to late binding. This requires a reference to Excel:
    VB Code:
    1. Dim oXL As Excel.Application, oBook As Excel.Workbook, oSheet As Excel.Worksheet, vValue As Variant
    2.  
    3. Set oXL = New Excel.Application
    4. Set oBook = oXL.Workbooks.Open("D:\Test.xls")
    5. Set oSheet = oBook.Worksheets("Sheet1")
    6.  
    7. vValue = oSheet.Cells(1, 1).Value       'Get the value from cell A1
    8. Debug.Print vValue
    9.  
    10. Set oSheet = Nothing
    11. oBook.Close
    12. Set oBook = Nothing
    13. oXL.Quit
    14. Set oXL = Nothing
    Another way would be to use ADO to open the workbook into a recordset. This does not require a reference to Excel, but I'm not exactly sure if you need Excel installed on the machine to use Excel as a data provider. I don't have any machines without Excel to test this on. Maybe someone else could shed some light on this.
    VB Code:
    1. Dim oRS As ADODB.Recordset, oConn As ADODB.Connection, sConString As String, sXLFile As String, vValue As Variant
    2.  
    3. sXLFile = "D:\Test.xls"
    4. sConString = "Provider= Microsoft.Jet.OLEDB.4.0;" & " Data Source=" & sXLFile & ";Extended Properties=Excel 8.0;"
    5. Set oConn = New ADODB.Connection
    6.  
    7. With oConn
    8.     .CursorLocation = adUseClient
    9.     .Open sConString
    10. End With
    11.  
    12. Set oRS = New ADODB.Recordset
    13.  
    14. With oRS
    15.     .CursorType = adOpenStatic
    16.     .CursorLocation = adUseClient
    17.     .LockType = adLockPessimistic
    18.     .Source = "SELECT * FROM [Sheet1$]"
    19.     .ActiveConnection = oConn
    20.     .Open
    21.     .MoveFirst
    22. End With
    23.  
    24. 'Work with the RS
    25.  
    26. oRS.Close
    27. oConn.Close
    28.  
    29. Set oRS = Nothing
    30. Set oConn = Nothing
    You can read more about this method here.

  3. #3
    New Member
    Join Date
    Oct 2008
    Posts
    7

    Re: How to read .xls file using vb

    Hi Comintern,

    I have saw ur reply but the code seems not completed. i cant compile it. Do you mind post the whole program again? Thank You so much... Your help will be greatly appreciated.

  4. #4
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: How to read .xls file using vb

    It's not meant to be compiled alone -- it's just a couple code snippets to demonstrate how to do it. What exactly are you trying to do?

  5. #5
    New Member
    Join Date
    Oct 2008
    Posts
    7

    Re: How to read .xls file using vb

    Hi Comintern,

    Thanks for replying

  6. #6
    New Member
    Join Date
    Oct 2008
    Posts
    7

    Re: How to read .xls file using vb

    We want to extract data from certain row/column of excel to text file automatically.
    Example: (row 5: column B)

    We tried alot of methods but we couldnt do it.

    Do you have any idea on how to extract data from Excel to text file automatically?

    Thank you

  7. #7
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: How to read .xls file using vb

    Try using the first snippet. Add a reference to Excel, and replace the debug statement with a write to file. Or, you could wrap it in a function and have it return the vValue variable.

    A search of the forum should give you all the info you need on writing the text file.

    I don't have VB installed on this machine, or I could be more explicit.

  8. #8
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: How to read .xls file using vb

    Kynn, I have a suspicion that you didn't add a reference to Excel and I also suspect that you didn't create 'Test.xls'. May I suggest that you read the Excel Tutorial here..
    http://www.vbforums.com/showthread.php?t=391665
    It's really the best way to learn VB/Excel.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  9. #9
    New Member
    Join Date
    Oct 2008
    Posts
    7

    Re: How to read .xls file using vb

    thank u so much...

    my excel is 2000. I am following the tutorial by adding a references but i couldnt find the references. My excel doesnt have the "project" tab. I search for the references but couldnt find it.

  10. #10
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: How to read .xls file using vb

    Quote Originally Posted by kynn
    thank u so much...

    my excel is 2000. I am following the tutorial by adding a references but i couldnt find the references. My excel doesnt have the "project" tab. I search for the references but couldnt find it.
    The Project tab is in Visual Basic, not Excel. Are you using VBA? If you're wondering what VBA is, it's the version of VB that runs behind Excel. If you're accessing VB by running Excel and then accessing VB from Tools/Macros/VB then your using VBA. VBA is not a stand alone version of VB.
    Last edited by CDRIVE; Oct 21st, 2008 at 08:02 AM.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,953

    Re: How to read .xls file using vb

    kynn, your profile says you are using VB2005. That is VB.Net, and is very different to VB6 and earlier (which is what the code here and in my tutorial are for).

    For the kind of thing you need, take a look at the .Net articles in the third post of our Office Development FAQs (at the top of the Office Development forum)

    If you need any help, it would be best to post a new thread in our VB.Net forum, or if interacting with Excel you could post in our Office Development forum instead.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to read .xls file using vb

    If using VB.NET then I would also say take a look at this faq item which shows how to connect to an excel sheet from vb.net

    http://www.vbforums.com/showthread.php?t=442232


    The thread can be moved to VB.NET also

    Ps, Thanks Si
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13
    New Member
    Join Date
    Oct 2008
    Posts
    7

    Re: How to read .xls file using vb

    hi all

    I have seen the link above. However, all the tutorials there is automated from excel to office app.

    I need the excel to send data to notepad(text file) consistently, like every 2 min send the data to text file automatically.

    Is it possible?

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,953

    Re: How to read .xls file using vb

    The link RobDog888 gave shows how to use Excel from VB.Net, as do several of the other items in the O.D. FAQs. You don't need Excel to do things, what you need to do is get your VB program to read the data from it and create the the text file.

    However, as I said before, this is not the place to post your questions - not only is this forum for a different language you are using, but you are also hijacking somebody elses thread. Instead of replying here, post a new thread in the VB.Net or O.D. forum if you need any more help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width