English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

My task is to make a program that can import a Microsoft Excel document into my Visual Basic Program.
-I have the codes to open the Excel document.

What I am lacking now is how to make a code that makes the Visual Basics program read the Excel document and display the data.

How?? Please help!!!

2007-08-16 18:44:16 · 1 answers · asked by Anonymous in Computers & Internet Programming & Design

1 answers

Hi,
try the following:
First go to main menu, select project-> then select References-> then check the box before the item "Microsoft Excel 11.0 Object Library". Otherwise u can not use Excel workbook in Visual Basic.
Then type the following code:
-------------------------------------------
Private Sub Command1_Click()

Dim wrkBook As Workbook, wrkSheet As Worksheet
Dim i As Integer

Set wrkBook = Workbooks.Open("C:\abc.xls")
Set wrkSheet = wrkBook.Sheets(1)

i = 1
While Not IsEmpty(wrkSheet.Cells(i, 1))
Debug.Print wrkSheet.Cells(i, 1)
i = i + 1
Wend

wrkBook.Close
End Sub
---------------------------------------------------------
it opens an existing workbook for reading.
u can use wrksheet.cells(row,column) for accessing individual cell of a worksheet by supplying row and column number. First row and first column or cell A1 can be accessed by writing wrksheet.cells(1,1) and so on.

If data is in second sheet, then use
Set wrkSheet = wrkBook.Sheets(2).

the place where Debug.Print is written, u can use this place for using Excel data.

While command reads data from worksheet as long as there are values are there.

If u already know number of rows those have data, then u may use For...Next loop also.

2007-08-16 21:44:16 · answer #1 · answered by iqbal 4 · 0 0

fedest.com, questions and answers