-
Excel Question
Hello guys! I don't have my books with me right now and I'm trying to create an application that:
1) Opens an Existing Excel Worksheet
2) Read in the Values from the Active Worksheet
3) Write data back to the Active Worksheet
Does anyone have some basic code that will do this? I've seen a lot of the code posted here on this site but it's a little too in-depth for what I'm trying to do. Thanks for the help!
-
Easy thing to do:
1st Step: Go to Project --> References and reference the Microsoft Excel Object Library
2nd Step:
'Excel Application Variable
Dim xlApp as Excel.Application
Dim strText as String
'Opens an instance of Excel
set xlApp = createobject(Excel.Application)
'Open the document
xlApp.Workbooks.Open("MyFile.XLS")
'Activate the first sheet
xlApp.Sheets(1).Activate
'Read/Write to a cell. 1,1 is A1, 2,1 would be A2, etc..
strText = xlApp.Workbooks.Application.Cells(1,1)
strText = strText & " - I changed this text"
xlApp.Workbooks.Application.Cells(2,1) = strText
xlApp.Workbooks.Item(1).SaveAs "MyNewFile.XLS"
xlApp.Workbooks.Close
xlApp.Quit
-
-
I'm getting an error (ActiveX component can't create object) on the line:
Set xlApp = CreateObject(Excel.Application)
-
Never mind I fixed it. I forgat the quotes.