Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Manipulating data from two excel sheets into one using Visual Studio 2005
 

Manipulating data from two excel sheets into one using Visual Studio 2005

I'm pretty new to Visual Basic, so any help will be greatly appreciated!!! I have two different excel sheets with pretty much the same information but with updateddata in one of the columns(one excel sheet is from this week and one from last week). I'm trying to take this week'ssheet and subtract column B's information from column B's information from last week's sheet and copy the results into a new excel workbook. I'm creating an application with this happening on the Button Click event. I'm not really sure what to do next, help me please! I have referenced the Excel Object Library so that I can control excel through VB and I know its incomplete but I have this so far...

Dim cn1 As System.Data.OleDb.OleDbConnection

Dim cn2 As System.Data.OleDb.OleDbConnection

Dim cmd As System.Data.OleDb.OleDbDataAdapter

Dim ds As New System.Data.DataSet()

Dim NewSheet as New Excel.Application

cn1 = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & _

"data source=C:\thisWeek.XLS;Extended Properties=Excel 8.0;")

cn2 =New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & _

"data source=C:\lastWeek.XLS;Extended Properties=Excel 8.0;")

cmd = New System.Data.OleDb.OleDbDataAdapter("select * from [thisWeek$]", cn1)

cn1.Open()

cmd.Fill(ds)

NewSheet.xls=ds.copy

cn1.close()
cn2.close()

Can I just put and excel formula in my code to directly referece these two columns in their respective sheets and compute it that way? Then output into a new sheet?

HELPNewToVB  Thursday, July 05, 2007 10:36 PM

Hi,

I would write the macro in VBA and then call it from .NET ... have a look at these google results

http://www.google.co.uk/search?hl=en&q=run+vba+macro+from+.net&meta=

it will make you life a lot easier as Excel is very easy to play with from the VBA backend.

HTH,

James.

jrtttt  Friday, July 06, 2007 7:57 PM

Hi, HELPNewToVB,

James Randle's idea of using VBA is fine,

however, there is still some other way to achieve this.

You can use Microsoft Excel Object Library to control the whole Excel WorkSheet,

including manipulating data, adding formula.

Please read the following article on automate Microsoft Excel

http://support.microsoft.com/kb/301982/en-us

Hopes this helps,

Regards

Yu Guo â€?MSFT  Wednesday, July 11, 2007 3:52 AM

Hi,

I would write the macro in VBA and then call it from .NET ... have a look at these google results

http://www.google.co.uk/search?hl=en&q=run+vba+macro+from+.net&meta=

it will make you life a lot easier as Excel is very easy to play with from the VBA backend.

HTH,

James.

jrtttt  Friday, July 06, 2007 7:57 PM
I'll look at those results and try it soon! Thank you so much. So should I just open the excel sheet from my button click event and have an automatic macro on that excel sheet?
HELPNewToVB  Friday, July 06, 2007 8:40 PM

Hi,

I think (i would expect that) you can run a macro by using it's name. this would be prefered because you don't really want something running every time someone opens the worksbook up - they might try to stop it half way through and break it!

Cheers,

James.

jrtttt  Friday, July 06, 2007 8:45 PM

Hi, HELPNewToVB,

James Randle's idea of using VBA is fine,

however, there is still some other way to achieve this.

You can use Microsoft Excel Object Library to control the whole Excel WorkSheet,

including manipulating data, adding formula.

Please read the following article on automate Microsoft Excel

http://support.microsoft.com/kb/301982/en-us

Hopes this helps,

Regards

Yu Guo â€?MSFT  Wednesday, July 11, 2007 3:52 AM
Awesome! Thank you so much!
HELPNewToVB  Wednesday, July 11, 2007 6:36 PM
Do you know of any other resources that I can find code examples toautomate excel from visual studio?
HELPNewToVB  Wednesday, July 11, 2007 6:52 PM
Yu Guo â€?MSFT  Thursday, July 12, 2007 12:53 AM
You have been abig help! Thank you so much!
HELPNewToVB  Thursday, July 12, 2007 2:38 AM

You can use google to search for other answers

Custom Search

More Threads

• How do you make a form transparent and NOT it's controls?
• cloning a listview
• Opacity - Fading screens
• bring up child control's designer in a UserControl?
• Details regarding UndoEngine Class in .NET
• Accented characters in MyForm.designer.vb not loaded correctly by VS!?
• Custom Control
• windows control library and PropertyGrid issues??
• Pressing Enter Key creates Beep Sound
• scrolling message