Connecting Tech Pros Worldwide Forums | Help | Site Map

Visual Basic reading Excel workbook

Newbie
 
Join Date: Jan 2009
Posts: 2
#1: Jan 3 '09
Hi List Members,

I am writting a program in Visual Basic and I need to read data from an existing Excel sheet. How can I access the data on Excel?

Thank you.

Newbie
 
Join Date: Jan 2009
Posts: 2
#2: Jan 3 '09

re: Visual Basic reading Excel workbook


Problem solved.

Thanks anyway.

I used the following command.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. 'Put a reference to:
  3. 'Microsoft Excel (highest number) Object Library
  4. Private xlapp As Excel.Application
  5. Private xlapp2 As Excel.Application
  6. Dim wkbWorkBook As Excel.Workbook
  7. Dim wksSheet As Excel.Worksheet
  8. Dim wkb2 As Excel.Workbook
  9. Dim wks2 As Excel.Worksheet
  10. 'Private Const thePath As String = "C:\"
  11. Private SelectedFile As String
  12. Private CounterTab As Integer
  13.  
  14. Private Sub Command1_Click()
  15.  Set xlapp = New Excel.Application
  16.    'xlapp.Workbooks.Open thePath & "pinpon.xls"
  17.    xlapp.Workbooks.Open SelectedFile
  18.    CounterTab = 2
  19.    Set wksSheet = xlapp.Worksheets(CounterTab)
  20.       wksSheet.Activate
  21.       MsgBox xlapp.ActiveSheet.Range("A3").Value
  22.       xlapp.ActiveSheet.Range("A1").Select
  23.       Selection.Copy
  24.       'PastetoNewFile
  25.       wksSheet.Activate
  26.       xlapp.ActiveWorkbook.Close savechanges:=False, FileName:=SelectedFile
  27.    Set wksSheet = Nothing
  28.   xlapp.Quit
  29.  Set xlapp = Nothing
  30. End Sub
  31.  
  32. Private Sub PastetoNewFile()
  33.    Set xlapp2 = New Excel.Application
  34.    Set wkb2 = xlapp2.Workbooks.Add
  35.       'Here: you did not referenced correct objects:
  36.       wkb2.Worksheets.Add
  37.       wkb2.Worksheets("Sheet1").Activate
  38.       'In Italy the above is called differently...
  39.       'wkb2.Worksheets("Foglio1").Activate
  40.       wkb2.ActiveSheet.Paste
  41.       wkb2.Close savechanges:=True, FileName:=thePath & "new.xls"
  42.    Set wkb2 = Nothing
  43.    xlapp2.Quit
  44.    Set xlapp2 = Nothing
  45. End Sub
Reply