Reading from Excel using VB problem

MR Campbell

Board Regular
Joined
Aug 2, 2002
Messages
113
At present I am attempting a rather simple task ... to read a cell from an existing sheet from an existing Excel file using Visual Basic (Visual Studio 2010). I am unable to do this as I am getting an error message at the indicated line. I have already added a reference to Excel 14.0 (Excel 2010) within my VB project. Any ideas ?

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim FirstName, Surname As String

'file path and name for the Excel file
Dim FileNamePath As String = "C:\Users\MRC\Desktop\MRC Files\Visual Basic Files\SwimManager 2014\CurrentSeasonDATA\SquadData.xls"

Dim XL = CreateObject("Excel.Application") 'reference Excel as the application
XL.Visible = False 'make Excel application invisible

XL.Workbooks.Open(FileNamePath) 'open the designated workbook file

FirstName = XL.Worksheets("DOB").Cells(6, 3).Value 'read the first name <======== getting the ERROR ON THIS LINE
Surname = XL.Worksheets("DOB").Cells(6, 4).Value 'read the surname

'Write the name to a textbox
TextBox2.Text = FirstName & " " & Surname
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Dim XL = CreateObject("Excel.Application") 'reference Excel as the application
XL.Visible = False 'make Excel application invisible

XL.Workbooks.Open(FileNamePath) 'open the designated workbook file

FirstName = XL.Worksheets("DOB").Cells(6, 3).Value 'read the first name <======== getting the ERROR ON THIS LINE
Surname = XL.Worksheets("DOB").Cells(6, 4).Value 'read the surname

1. You open the workbook, but does not catch Workbook object which Open method returns. Thus "XL.Worksheets("DOB").Cells(6, 3).Value" does not make a sense.
2. Which options your project has? Option Strict? Option Explicit?
 
Last edited:
Upvote 0
Moreover, if you add reference to library, then why not using it? You, instead, you late-binding instead of early-binding. By using CreateObject you use late-binding. If you'd use early-binding then you could write like this:
Code:
Imports Excel = Microsoft.Interop....

Sub FFF()
....
    Dim xlApp As Excel.Application
    Dim book As Excel.Workbook
    Dim sheet As Excel.Worksheet
.....
     xlApp = New Excel.Application {.Visible = False}
     book = xlApp[COLOR=#333333].Workbooks.Open(FileNamePath)
[/COLOR]     sheet = DirectCast(xlBook.Worksheets("DOB"), Excel.Worksheet)
....
End Sub
Also, you enable IntelliSense, which will help you with pop-up list of methods and properties.
 
Last edited:
Upvote 0
ok ... here is what I now have :

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim sht As Excel.Worksheet

xl = New Excel.Application {.Visible = False}
wb = xl.Workbooks.Open(FileNamePath)
sht = DirectCast(wb.Worksheets("DOB"), Excel.Worksheet)

FirstName = sht.Cells(6, 3).Value 'read the first name
Surname = sht.Cells(6, 4).Value 'read the surname

Now I get 4 build errors : Excel.Application is not defined, Excel.Workbook is not defined and Excel.Worksheet is not defined.

I didn't understand what you wrote when you said : Imports Excel = Microsoft.Interop....
 
Upvote 0
I apologize that I didn't include correct Imports. Here's correct one:
Code:
Imports Excel = Microsoft.Office.Interop.Excel
The "Excel" before "equals" sign is an alias for namespace, so that it'd be easier to refer to "Microsoft.Office.Interop.Excel".
 
Upvote 0
The question I now have is how to close the file that has been read.
i.e. how to close the file ....in this case since I have simply read from it, I don't want to save the file.

Here is what I currently have :

Dim FirstName, Surname As String

'file path and name for the Excel file
Dim FileNamePath As String = "C:\Users\MRC\Desktop\MRC Files\Visual Basic Files\SwimManager 2014\CurrentSeasonDATA\SquadData.xls"

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim sht As Excel.Worksheet

xl = New Excel.Application 'run a new Excel application
xl.Visible = False 'make Excel invisible

wb = xl.Workbooks.Open(FileNamePath) 'open the workbook file
sht = DirectCast(wb.Worksheets("DOB"), Excel.Worksheet) 'define the worksheet

FirstName = sht.Cells(6, 3).Value 'read the first name
Surname = sht.Cells(6, 4).Value 'read the surname

'Write the name to a textbox
TextBox2.Text = FirstName & " " & Surname
 
Last edited:
Upvote 0
Here's working code I have tested (console app). It opens workbook and closes it without saving changes.

Code:
Imports Excel = Microsoft.Office.Interop.Excel


Module Module1


    Sub Main()


        Dim xlApp As Excel.Application
        Dim book As Excel.Workbook
        Dim sheet As Excel.Worksheet
        Dim FileNamePath As String


        FileNamePath = "C:\SomeFile.xlsx"
        xlApp = New Excel.Application With {.Visible = True}
        book = xlApp.Workbooks.Open(FileNamePath)
        sheet = DirectCast(book.Worksheets("DOB"), Excel.Worksheet)


        book.Close(SaveChanges:=False)
        xlApp.Quit()


        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
        GC.Collect()
        GC.WaitForFullGCComplete()


        Console.WriteLine("Done!")


    End Sub


End Module
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,845
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top