473,320 Members | 2,202 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,320 software developers and data experts.

Probably a silly vb.net question

Hi, I've used vb.net for 3 hours(!) now and have probably
a very easy question. In my VB6 programs I use For Each
Cell in Range... very often which not seems to work in
vb.net.

Ex.

Dim Cell As Excel.Range
Dim xl as Excel.Application

xl = New Excel.Application

For Each Cell In xl.Range("A1:C4")
What is wrong here? I get "Unknown interface" (if I have
translated it correctly) in the For... statement.

Thanks in advance.

/Nicke
Jul 21 '05 #1
3 1585
Thank you Bruce!

It wasn't the Add (I had that in the original code). It
was "Worksheets.Item(1)" that was missing so I changed it
to "For Each Cell In xl.ActiveSheet.Range("A1:C4")" and
it worked... I think I have a little to do before my
program is upgraded to .net...

BTW, I tested a similar code but with a much larger range
(5000 rows, 1 column). VB6 did the code in 2.5 sec
and .net in 15 sec!!! I was hoping on a speed enhancment.

Thanks for the help.

/Nicke
-----Original Message-----
I believe the problem is that you need to open or add a
Workbook before the Range method has any meaning. I usedthe following code to get rid of the error that you were
having.

Dim xl As Excel.Application = New Excel.Application
Dim cell As Excel.Range

xl.Workbooks.Add()
For Each cell In xl.Worksheets.Item(1).Range("A1:C4")
Console.WriteLine("Value: " & cell.Value)
Next

Hope this helps

Bruce Johnson
http://www.ObjectSharp.com/Bruce
-----Original Message-----
Hi, I've used vb.net for 3 hours(!) now and have

probably
a very easy question. In my VB6 programs I use For Each
Cell in Range... very often which not seems to work in
vb.net.

Ex.

Dim Cell As Excel.Range
Dim xl as Excel.Application

xl = New Excel.Application

For Each Cell In xl.Range("A1:C4")
What is wrong here? I get "Unknown interface" (if I havetranslated it correctly) in the For... statement.

Thanks in advance.

/Nicke
.

.

Jul 21 '05 #2
Keep in mind that, when you use VB.Net to talk to Office,
you are actually interacting with Excel automation using
COM Interop. The cost of crossing the managed/unmanaged
boundry might account for the slowdown you are seeing.
-----Original Message-----
Thank you Bruce!

It wasn't the Add (I had that in the original code). It
was "Worksheets.Item(1)" that was missing so I changed it
to "For Each Cell In xl.ActiveSheet.Range("A1:C4")" and
it worked... I think I have a little to do before my
program is upgraded to .net...

BTW, I tested a similar code but with a much larger range
(5000 rows, 1 column). VB6 did the code in 2.5 sec
and .net in 15 sec!!! I was hoping on a speed enhancment.

Thanks for the help.

/Nicke
-----Original Message-----
I believe the problem is that you need to open or add a
Workbook before the Range method has any meaning. I

used
the following code to get rid of the error that you were
having.

Dim xl As Excel.Application = New Excel.Application
Dim cell As Excel.Range

xl.Workbooks.Add()
For Each cell In xl.Worksheets.Item(1).Range("A1:C4")
Console.WriteLine("Value: " & cell.Value)
Next

Hope this helps

Bruce Johnson
http://www.ObjectSharp.com/Bruce
-----Original Message-----
Hi, I've used vb.net for 3 hours(!) now and have

probably
a very easy question. In my VB6 programs I use For Each
Cell in Range... very often which not seems to work in
vb.net.

Ex.

Dim Cell As Excel.Range
Dim xl as Excel.Application

xl = New Excel.Application

For Each Cell In xl.Range("A1:C4")
What is wrong here? I get "Unknown interface" (if Ihavetranslated it correctly) in the For... statement.

Thanks in advance.

/Nicke
.

.

.

Jul 21 '05 #3
OK, I thought it would something like that =). Is there
antoher way to do it?
-----Original Message-----
Keep in mind that, when you use VB.Net to talk to Office,you are actually interacting with Excel automation using
COM Interop. The cost of crossing the managed/unmanaged
boundry might account for the slowdown you are seeing.
-----Original Message-----
Thank you Bruce!

It wasn't the Add (I had that in the original code). It
was "Worksheets.Item(1)" that was missing so I changed itto "For Each Cell In xl.ActiveSheet.Range("A1:C4")" and
it worked... I think I have a little to do before my
program is upgraded to .net...

BTW, I tested a similar code but with a much larger range(5000 rows, 1 column). VB6 did the code in 2.5 sec
and .net in 15 sec!!! I was hoping on a speed enhancment.
Thanks for the help.

/Nicke
-----Original Message-----
I believe the problem is that you need to open or add aWorkbook before the Range method has any meaning. I

used
the following code to get rid of the error that you werehaving.

Dim xl As Excel.Application = New Excel.Application
Dim cell As Excel.Range

xl.Workbooks.Add()
For Each cell In xl.Worksheets.Item(1).Range("A1:C4")
Console.WriteLine("Value: " & cell.Value)
Next

Hope this helps

Bruce Johnson
http://www.ObjectSharp.com/Bruce

-----Original Message-----
Hi, I've used vb.net for 3 hours(!) now and have
probably
a very easy question. In my VB6 programs I use For EachCell in Range... very often which not seems to work invb.net.

Ex.

Dim Cell As Excel.Range
Dim xl as Excel.Application

xl = New Excel.Application

For Each Cell In xl.Range("A1:C4")
What is wrong here? I get "Unknown interface" (if I

have
translated it correctly) in the For... statement.

Thanks in advance.

/Nicke
.

.

.

.

Jul 21 '05 #4

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

Similar topics

5
by: Pjotr Wedersteers | last post by:
This may be a silly question, but I was wondering. If 10 clients fill out a form on my page simultaneaously and hit submit, how does my (Apache2.0.50/PHP4.3.8) server exactly ensure each gets...
15
by: Jim | last post by:
This is probably a common question - What is the best CSS editor? I'm an old HTML dinosaur that just getting into CSS. My HTML editor from way back is Homesite. They (Macromedia) tout Topstyle Pro...
4
by: Jenny | last post by:
Hi you al I have two very silly question The first one is In vb 6.0 you can add a procedure, sub or function, by clicking add procedure from the tools item on the menuba I cannot seem to find...
3
by: Nicke | last post by:
Hi, I've used vb.net for 3 hours(!) now and have probably a very easy question. In my VB6 programs I use For Each Cell in Range... very often which not seems to work in vb.net. Ex. Dim Cell...
6
by: Adam Honek | last post by:
Hi, I've looked over and over and in MSDN 2005 and just can't find what is a really simple answer. How do I select the first value in a combo box to be shown? I tried the .selectedindex -1...
7
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a...
2
by: Willem Voncken | last post by:
Hi guys, might be a silly question, but i'm wondering whether it is even possible to generate access violations when using C#? I'm new at c# programming, but i have some experience with c++....
1
by: Pontifex | last post by:
Hi all, Has anyone devised a simple method for passing a string to an external script? For instance, suppose I want to produce a title bar that is very fancy and I don't want my main HTML...
4
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.