Connecting Tech Pros Worldwide Help | Site Map

Visual Basic reading Excel workbook

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 3rd, 2009, 03:46 AM
Newbie
 
Join Date: Jan 2009
Posts: 2
Default Visual Basic reading Excel workbook

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.
Reply
  #2  
Old January 3rd, 2009, 05:13 AM
Newbie
 
Join Date: Jan 2009
Posts: 2
Default

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

Last edited by debasisdas; January 3rd, 2009 at 05:16 AM. Reason: Added code tags for better formatting.
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.