.net Option Strict Excel For Each In Loop Question

madferretnick

New Member
Joined
Jul 1, 2002
Messages
7
Has anybody tried automating Excel / creating COM Add in from Vb.net and using the For Each In Loop with Option Strict on for a list of cells in a range?

I get a System. Runtime. InteropServices. COMException!

'This Fails
Option Strict On
Private Sub TestForEachLoop()

Dim cell As Excel.Range
Dim ws As Excel.Worksheet
Dim rng As Excel.Range
ws = CType(m_ExcelApp.ActiveWorkbook.Worksheets.Item("Sheet1"), Excel.Worksheet)
rng = ws.Range("a1").CurrentRegion
For Each cell In rng
Debug.WriteLine(cell.Value)
Next

End Sub

'This Works
Option Strict Off
Private Sub TestForEachLoop()

Dim cell As Excel.Range
Dim ws As Excel.Worksheet
Dim rng As Object
ws = CType(m_ExcelApp.ActiveWorkbook.Worksheets.Item("Sheet1"), Excel.Worksheet)
rng = ws.Range("a1").CurrentRegion
For Each cell In rng
Debug.WriteLine(cell.Value)
Next

End Sub


_COMPlusExceptionCode -532459699
_message "Member not found."
StackTrace " at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Excel.Range.GetEnumerator()
at TestForEachLoop() in
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
OK - this is an ancient thread. I just happened across it due to a search. But should it be of any help to madferret or another reader, in VBA you'd need Set rng = ws.range("a1").currentregion

Don't know if the same rules would apply in vb.net or not since I've not programmed in it.
 
Upvote 0
No "Set" in "Net". You need to Implement the IEnumerable interface to enum through objects.

Tom
 
Upvote 0

Forum statistics

Threads
1,215,230
Messages
6,123,752
Members
449,118
Latest member
kingjet

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top