You are on page 1of 2

How to Access Excel Spreadsheets With Visual Basic 6

By Robert Karr, eHow Contributor updated: January 21, 2010 Creating, accessing and adding or deleting data in Excel is all possible through Visual Basic 6. Some brief lines of code in Visual Basic will accomplish the first two actions. However, changing Excel data is more complicated and requires considerably more explanation than possible in this article. Writing programs to do this can be helpful if the developer needs to simplify the use of Excel for untrained users. Using Visual Basic forms allow for more modification and instruction than might be possible in Excel. Difficulty: Moderately Challenging

Instructions
1. 1 Open the Microsoft Visual Basic 6 program; click "File" and "New Project." Choose "Standard.EXE" from the list of templates. A new form will open on the screen, labeled "Project 1 -- Form1(Form)." Double-click three times on the Command Button icon in the left panel to add three buttons to the form. Save the form with a file name of your choice. 2. 2 Click on "View" and "Properties Pages" (this may already be showing in the right-hand panel), and click on the first button on the form. In the Properties window, double-click "Caption" and change the text to "Open Existing Worksheet." Change the second button caption to "Create New Worksheet." Repeat for the third button, but change this caption to "Exit." Drag the first two buttons so they are opposite each other. Place the third one below the other two. 3. 3 Double-click on the "Open Existing Spreadsheet" button. This changes the view from "Object" to "Code" and inserts a new procedure. Type these lines exactly as they appear between the "Private Sub Command1_Click()" and "End Sub," which should already be there. Option Explicit Dim MyExcel As New Excel.Application Dim MyWorkbook As Excel.Workbook

Dim MyWorksheet As Excel.Worksheet Dim FileName As String FileName = InputBox("Enter full name and location of Excel file") Set MyWorkbook = MyExcel.Workbooks.Open(FileName) Set MyWorksheet = MyExcel.ActiveSheet MyExcel.Visible = True 4. 4 Double-click on the "Create New Worksheet" and type these lines of code.

Set MyWorkbook = MyExcel.Workbooks.Add Set MyWorksheet = MyWorkbook.Worksheets("Sheet1") Set MyWorksheet = MyExcel.ActiveSheet MyExcel.Visible = True 5. 5 Finish the code by double-clicking on the "Exit" button on the form and type these lines. MyExcel.Quit End 6. 6 Test the program by pressing "F5." If it does not work, go back and double-check the code. It must be exact. If you get an error saying the existing file cannot be found, check the location and ensure you entered it exactly. When everything is working, click on "File," then "Make" and follow it by the name you assigned to the project at the start. This will create an executable version you can run directly.

Read more: How to Access Excel Spreadsheets With Visual Basic 6 | eHow.com http://www.ehow.com/how_5895699_access-spreadsheets-visual-basic-6.html#ixzz138P8NkkJ

You might also like