473,410 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

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.
Jan 3 '09 #1
1 2572
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
Jan 3 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Geert-Pieter Hof | last post by:
Hello, My VB 6.0 application read and writes data from and to a MS Excel workbook, using the Microsoft.Jet.OLEDB.4.0 provider. Now I want to protect the Excel workbook with a password, but I...
3
by: JW | last post by:
I am looking for sample code to learn me about using excel from a visual basic program. I want to open a file, store code in a certain cell and save this again. Who can help me Regards
1
by: mail2atulmehta | last post by:
Hi, I do not know if this is right place for this, but i need some help. I have a text file, whose values are seprated by a delimiter. I want to open this file in excel, ( not import it) . I have...
10
by: Steve | last post by:
I am trying to create a DLL in Visual Studio 2005-Visual Basic that contains custom functions. I believe I need to use COM interop to allow VBA code in Excel 2002 to access it. I've studied...
3
by: Amit | last post by:
Hello group, I"m new to COM Interop and Add-Ins. Today I was reading an article that says: "Use Visual Studio Tools for the Microsoft Office System to create a new Excel Workbook project in...
2
by: Vmrincon | last post by:
Hi! I need to crete a visual basic .net application to read Excel 2000 files. Does anyone know if I need some kind of driver or some extra file to be able to do it? Thanks a lot!
10
by: Esmael | last post by:
Hi to all, /*****************************/ OS-WIn XP SP2 VB6 SP6 /*****************************/ Is their anyone who can help me with this: Source code written on VB6.
4
by: bonkbc | last post by:
hi! how can we take textbox data from a visual basic 6.0 form and write it to an excel worksheet once a command button is clicked? I've gotten very close to completing this on my own, here is...
11
by: =?Utf-8?B?UGV0ZXIgSw==?= | last post by:
I am working with Visual Studio or alternately with Expression Web. I need to create about 50 aspx pages with about 1200 thumbnali images, typically arranged in three to four groups per page,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.