473,399 Members | 4,177 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,399 software developers and data experts.

how to insert data into an excel spreadsheet cell

I need some help with this please: I'm looping through some data to
total some figures. Each time the total is calculated I need to
insert that total into an excel spreadsheet cell, A2 for instance.
The next time the total would be written to cell A3 and so on. Here
is how I create the spreadsheet:

oExcel = New Microsoft.Office.Interop.Excel.Application
oBook = oExcel.Workbooks.Add
oSheet = CType(oBook.Worksheets(1),
Microsoft.Office.Interop.Excel.Worksheet)

I can't quite figure out how to write each total to a different cell.

Sep 19 '07 #1
3 9969
"David Jeavons" wrote:
>...
One other thing to note is that you cannot use Option Strict On with this
type of coding as you are using COM objects and unless I am mistaken there
is no way to utilise the Value property of the cells collection whilst
Option Strict is on. Obviously this could cause some problems as you should
always have Option Strict On so I would suggest that you move this type of
code to a module where it can be called without hindering the rest of your
code.
...
I dont agree with that point ("Option Strict On is not possible"). :-)
Instead of osheet.cells(1,1).value = "Hello World" you can use
osheet.range(osheet.cells(1,1).item).value = "Hello World"

As an alternative, you can use a string to specify a range:
osheet.range("A1").value = "Hello World"

To change the value of cells A1, A2, B1 and B2 in one go you can use another
range like osheet.range("A1", "B2").value = "Hello World"

I totally agree to your point ("Always have Option Strict On !"). :-)
Sep 19 '07 #2
Hi Oliver

Thanks for pointing that out, it didn't occur to me to use the Range method.
To be honest, it has been years since I did any automation with Excel using
the Object Library but when I did I remember not being able to find the
appropriate method that I could use whilst still keeping Option Strict On.

Learn something new everyday :)

--
MVP (Visual Developer - Visual Basic)
"Oliver Tell" <Ol********@discussions.microsoft.comwrote in message
news:6A**********************************@microsof t.com...
"David Jeavons" wrote:
>>...
One other thing to note is that you cannot use Option Strict On with this
type of coding as you are using COM objects and unless I am mistaken
there
is no way to utilise the Value property of the cells collection whilst
Option Strict is on. Obviously this could cause some problems as you
should
always have Option Strict On so I would suggest that you move this type
of
code to a module where it can be called without hindering the rest of
your
code.
...

I dont agree with that point ("Option Strict On is not possible"). :-)
Instead of osheet.cells(1,1).value = "Hello World" you can use
osheet.range(osheet.cells(1,1).item).value = "Hello World"

As an alternative, you can use a string to specify a range:
osheet.range("A1").value = "Hello World"

To change the value of cells A1, A2, B1 and B2 in one go you can use
another
range like osheet.range("A1", "B2").value = "Hello World"

I totally agree to your point ("Always have Option Strict On !"). :-)

Sep 19 '07 #3
David, thank you for your example, that worked for me.

Sep 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kevin Payne | last post by:
Hello, I have an intranet application written in asp using access as the backend database. My users have requested they be able to download certain data into an excel spreadsheet. I have limited...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
8
by: Edgar | last post by:
Tools: SQL Server 2K, Excel 2000 Hi, I have an Excel report worksheet with formatted headings. What I want to do is to export data from the SQL server into a specific cell of the excel file....
0
by: Grip | last post by:
Hi, I have gone throught the group and Microsoft's online help and have seen many suggestions but I am still seeking clarity: 1. I have an excel spreadsheet. Column A contains text that may...
0
by: sradoff | last post by:
I wish to use XPath to access data from within an Excel generated XML Spreadsheet (generated with MS Excel 2003). I am using an asp Web page (not .NET, not at this time). I instantiate...
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
3
by: JFKJr | last post by:
Hello everyone, I am trying to insert a picture in an excel spreadsheet cell using Access VBA. The following excel VBA code is working perfectly fine and I am able to insert picture in "A1" cell....
1
by: =?Utf-8?B?QWRyaWFuTW9ycmlz?= | last post by:
Hello All, I am trying to read an Excel spreadsheet (xls) programmatically. I have used the ODBC database route and have opened the Excel spreadsheet and then using a 'CRecordset', I read each...
2
by: ravir81 | last post by:
Hi, I am working on excel reporting using Perl. I am facing problem with writing the header part only once for all the excels created using Perl. Here is the code : ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...

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.