Library code snippets

Working with Excel Files Using VB6

The code is totally self explanatory, In the load event we're going to open the new instance of the excel library and our excel file “book1.xls” will be accessible from our code. Then we'll use Command1 to retrieve data from book1, please note that you must have some data in the excel file. Similarly Command2 is used to put/replace the data in the excel sheet cells.

'do declare these variables you need to add a reference
'to the microsoft excel 'xx' object library. 

'you need two text boxes and two command buttons
'on the form, an excel file in c:\book1.xls 

Dim xl As New Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlwbook As Excel.Workbook 

Private Sub Command1_Click()
'the benifit of placing numbers in (row, col) is that you
'can loop through different directions if required. I could
'have used column names like "A1" 'etc. 

    Text1.Text = xlsheet.Cells(2, 1) ' row 2 col 1
    Text2.Text = xlsheet.Cells(2, 2) ' row 2 col 2 

'don't forget to do this or you'll not be able to open
'book1.xls again, untill you restart you pc.
    xl.ActiveWorkbook.Close False, "c:\book1.xls"
    xl.Quit
End Sub 

Private Sub Command2_Click()
    xlsheet.Cells(2, 1) = Text1.Text
    xlsheet.Cells(2, 2) = Text2.Text
    xlwbook.Save 

'don't forget to do this or you'll not be able to open
'book1.xls again, untill you restart you pc.
    xl.ActiveWorkbook.Close False, "c:\book1.xls"
    xl.Quit
End Sub 

Private Sub Form_Load()
    Set xlwbook = xl.Workbooks.Open("c:\book1.xls")
    Set xlsheet = xlwbook.Sheets.Item(1)
End Sub 

Private Sub Form_Unload(Cancel As Integer)
    Set xlwbook = Nothing
    Set xl = Nothing
End Sub 

Comments

  1. 11 Nov 2009 at 09:59

    can i ask... how to unmerge the sheet on the excel sheet using vb?

  2. 26 Jun 2009 at 20:18

    Vrom VB6 How would you execute a macro that was embedded in this workbook?

  3. 13 Sep 2008 at 03:27

    1. Click menu "Project"
    2. Select "Reference..."
    3. Check the item "microsoft excel 'xx' object library"

  4. 13 Sep 2008 at 03:16

    I wonder if the problem is related to the lines:

    xl.ActiveWorkbook.Close False, "c:\book1.xls"
    xl.Quit

    in Command1_Click() and Command2_Click(). When I move these two lines to Form_Unload(), before the set nothing statement, the problem solved.

     

  5. 04 Sep 2008 at 06:58

     Could somebody please help me on how to add a reference 'to the microsoft excel 'xx' object library. I read your code and seems nice but am a newbie in vb and i don't know how add reference to microsoft excell. I seriosly want to be able to send the data from a datagrid to an excel sheet for anaylsis. Please help.

  6. 04 Jun 2008 at 18:55

    Hi, I tried your code with 2 command buttons and two text boxes. There is no syntax error. However at run time ONLY 1 command button works at a time. Clicking the 2nd button causes an error. An error is highlighted in debug for the 2nd button, even though there is no error.Closing the program and re-running it , this time clicking on the 2nd command button first and the 1st one second, gives an error message and shows a line that is error free!. What should I do

    Pls help Regds Balachandran
  7. 04 May 2007 at 11:57
    Hi U Need to include "Microsoft Excel 11.0 Object Library " to get code snippet
  8. 23 Mar 2007 at 13:47
    Developer Fusion Bot wrote:
    This thread is for discussions of Working with Excel Files Using VB6
    <html>
    <head><title>Customers</title></head>

    <body background="../image/niceblue.gif">
    <TABLE cellpadding="5" border="3">
    <tr><td>

    <h1>Customers</h1>

    ECG has approximately 1.4 million customers made up of 120 thousand <br>
    Prepaid customers and 1.28 million Credit Customers as at end of May,2006.

    <br>
    Customers who had been supplied with any of the types of <a href = "meters.html">prepaid meters</a>
    are<br> known as prepaid customers.
    Customers using credit meters are known as credit<br>
     customers,
     simply because the former makes payment before power is consumed<br>
     where as the latter consumes power before payment is made.<br>
    <hr>
    <br>

    <h2>Types of Customers</h2>
    <br>

    ECG customers are categorised into 2 broad ones:<BR><BR>

    1. Special Load Tarrif -(SLT Customers)

    Special Load Tariff (SLT) is paid by <br>customers whose load requirement<br> is 200 KVA and above.
    <br><br>

    An SLT customers can be one of the following types:

    <ul>
    <Li> High Voltage - HV</Li>

    <Li> Medium Voltage - MV</Li>

    <Li> Low Voltage - LV</Li>
    </ul>

    <hr>

    2. Non Special Load Tarrif -NSLT Customers<br>
    Non Special Load Tariff (Non SLT) are paid by customers whose load requirements<br> is below 200 KVA.  


    <br>
    SLT & Non SLT receipts form the bulk of the company’s revenue.
    <br><BR>
    The NSLT Customers are of 2 types:
    <ul>
    <Li> Residential - HV</Li>

    <Li> Non Residential - MV</Li>


    </ul>


    Non Special Load Tariff (Non SLT) are paid by customers whose load requirements<br>
     is below 200 KVA.   SLT & Non SLT receipts form the bulk of the company’s revenu

    </td>



     <td><h3><font color ="red">Read More about:</font></h3><br>

    <img src ="../image/nobulls1.gif" width="20" Height="20"><a href="cus.html" target="show"> Customer Population</a><br>

    <BR>

    <img src ="../image/nobulls1.gif" width="20" Height="20"><a href="servcon.htm" target="show">Getting New Service Connection</a><br>
    <BR>

    <img src ="../image/nobulls1.gif" width="20" Height="20"><a href="elecuse.htm" target="show">Using Electricity efficiently</a><br><br>
    <BR>

    <img src="../image/subst.jpg" height="550" width="500" Alt="ECG Customer operating a Prepaid meter"><br>
    <i> a Substation ar Achimota


    </td>
    </tr>
    </table>



    </body>































































































  9. 02 Mar 2007 at 18:47

    If I wanted to read into VB6 from excel, lets say 100 rows (containg 8 or 9 cells of numerical data, e.g. A1, B1, C1...,) and i wanted to use each row's data to do certain calculations and after each row is processed I want to go to the next row. How would write the code to go from one row in excel to the next row automatically. And how would my program know when it reached the end of file (excel file)?

    If you could keep the code as simple and clear as the code you have above that would be great.

    Thanks,

    Donnovan

     

     

  10. 15 Nov 2006 at 16:26

    hey, i cannot get the code snippet to work   i guess that i need to import a namespace or something??

  11. 12 Sep 2006 at 09:56
    Big Smile [:D] juz lyk macro programming...
  12. 29 Jun 2006 at 04:27

    It is work, but there's something I want to ask you more. Can you tell me how to set coloumn/cell fillcolor with any condition? Thx

    Regards

    Ocky

  13. 01 May 2006 at 05:26

    Hi,

    Thnx for this piece of code, as this was very usefule for my application development.. Thnx a lot dude

    Harish

    Chennai, India

  14. 19 Apr 2006 at 17:02

    Hello,

    Would u know how to compare .XLS or .CSV then output the results to a txt file?

     

  15. 01 Jan 1999 at 00:00

    This thread is for discussions of Working with Excel Files Using VB6.

Leave a comment

Sign in or Join us (it's free).

Adil Hussain Raza Adil is doing MCS from Virtual University Of Pakistan. He is working as a software developer for 3 years. He has developed several customized commercial software in Haroon Abad City for various cli...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!