"Range" not recognized?

  • Thread starter Thread starter Brian B
  • Start date Start date
B

Brian B

Hello,
Not sure what I'm doing wrong here in Visual Studio 2008, but I've included
the following Namespaces and still the compiler is complaining that 'Range'
is not a member of '[TitleofDocument].vb':
Imports Microsoft.Office.Core
Imports Excel = Microsoft.Office.Interop.Excel
Imports System
Imports System.Reflection

Imports Extensibility
imports System.Runtime.InteropServices

....I need 'Range' to be able to move about the spreadsheet that I'm
formatting. I have setup the proper COMs as well. My references are as
follows:
Microsoft Excel 12.0 Object Library
Microsoft Office 12.0 Object Library

any help would be appreciated,
thanks,
Brian
 
Brian B said:
Hello,
Not sure what I'm doing wrong here in Visual Studio 2008, but I've included
the following Namespaces and still the compiler is complaining that 'Range'
is not a member of '[TitleofDocument].vb':
Imports Microsoft.Office.Core
Imports Excel = Microsoft.Office.Interop.Excel
Imports System
Imports System.Reflection

Imports Extensibility
imports System.Runtime.InteropServices

...I need 'Range' to be able to move about the spreadsheet that I'm
formatting. I have setup the proper COMs as well. My references are as
follows:
Microsoft Excel 12.0 Object Library
Microsoft Office 12.0 Object Library

any help would be appreciated,
thanks,
Brian

I think you need to use Excel.Range, based on your imports statements.

Mike
 
Can you show us some code?

A range is an object that is actually a property of a Worksheet, so you need
to access it correctly.

-Scott

Family Tree Mike said:
Brian B said:
Hello,
Not sure what I'm doing wrong here in Visual Studio 2008, but I've
included
the following Namespaces and still the compiler is complaining that
'Range'
is not a member of '[TitleofDocument].vb':
Imports Microsoft.Office.Core
Imports Excel = Microsoft.Office.Interop.Excel
Imports System
Imports System.Reflection

Imports Extensibility
imports System.Runtime.InteropServices

...I need 'Range' to be able to move about the spreadsheet that I'm
formatting. I have setup the proper COMs as well. My references are as
follows:
Microsoft Excel 12.0 Object Library
Microsoft Office 12.0 Object Library

any help would be appreciated,
thanks,
Brian

I think you need to use Excel.Range, based on your imports statements.

Mike
 
Sure, the following is some of the code responsible for vertical merger of
blank cells in an Excel spreadsheet. I did use some VBA code to perform this
function initially...but it was too slow for the size of the spreadsheet that
I generate...this VB.Net code is much faster, I just can't get it to work for
Office 2007 b/c of this compiling challenge...

....Public Function findlast() As Long
Dim last As Long
Dim found As Boolean

last = 1
found = False
While Not found
found = True
'Debug.WriteLine("CS = " & cellstring(last, 1))
For i As Integer = 1 To 26
If Me.Range(cellstring(last, i)).Value2 <> "" Then found =
False
Next
If Not found Then last = last + 1

End While
findlast = last - 1
End Function
Public Function do_scan(ByVal row_s As Integer, ByVal row_e As Integer,
ByVal level As Integer) As Integer
Dim hit, top As Integer
Dim found As Boolean
top = row_s
While (top <= row_e)
hit = top + 1
found = False
While (hit <= row_e And Not found)
If Me.Range(cellstring(hit, level * 2 - 1)).Value2 <> "" Then
found = True
hit = hit - 1
Else
hit = hit + 1
End If
End While
If hit > row_e Then hit = row_e
Me.Range(cellstring(top, level * 2 - 1), cellstring(hit, level *
2 - 1)).Merge()
Me.Range(cellstring(top, level * 2), cellstring(hit, level *
2)).Merge()
If level < 13 Then do_scan(top, hit, level + 1)
top = hit + 1
End While
End Function...

Scott M. said:
Can you show us some code?

A range is an object that is actually a property of a Worksheet, so you need
to access it correctly.

-Scott

Family Tree Mike said:
Brian B said:
Hello,
Not sure what I'm doing wrong here in Visual Studio 2008, but I've
included
the following Namespaces and still the compiler is complaining that
'Range'
is not a member of '[TitleofDocument].vb':
Imports Microsoft.Office.Core
Imports Excel = Microsoft.Office.Interop.Excel
Imports System
Imports System.Reflection

Imports Extensibility
imports System.Runtime.InteropServices

...I need 'Range' to be able to move about the spreadsheet that I'm
formatting. I have setup the proper COMs as well. My references are as
follows:
Microsoft Excel 12.0 Object Library
Microsoft Office 12.0 Object Library

any help would be appreciated,
thanks,
Brian

I think you need to use Excel.Range, based on your imports statements.

Mike
 
Well, at first glance, I see what I was refering to earlier. You have
references to "Me.Range", but you need to be accessing ranges via a
Worksheet reference. Now, I assume the code you're showing is embedded in a
Worksheet module in Excel, but even still, if you want to refer to the
current worksheet, you should use the ActiveSheet object, rather than "Me".

Can you tell us exactly what line(s) are failing?

-Scott

Brian B said:
Sure, the following is some of the code responsible for vertical merger of
blank cells in an Excel spreadsheet. I did use some VBA code to perform
this
function initially...but it was too slow for the size of the spreadsheet
that
I generate...this VB.Net code is much faster, I just can't get it to work
for
Office 2007 b/c of this compiling challenge...

...Public Function findlast() As Long
Dim last As Long
Dim found As Boolean

last = 1
found = False
While Not found
found = True
'Debug.WriteLine("CS = " & cellstring(last, 1))
For i As Integer = 1 To 26
If Me.Range(cellstring(last, i)).Value2 <> "" Then found =
False
Next
If Not found Then last = last + 1

End While
findlast = last - 1
End Function
Public Function do_scan(ByVal row_s As Integer, ByVal row_e As Integer,
ByVal level As Integer) As Integer
Dim hit, top As Integer
Dim found As Boolean
top = row_s
While (top <= row_e)
hit = top + 1
found = False
While (hit <= row_e And Not found)
If Me.Range(cellstring(hit, level * 2 - 1)).Value2 <> ""
Then
found = True
hit = hit - 1
Else
hit = hit + 1
End If
End While
If hit > row_e Then hit = row_e
Me.Range(cellstring(top, level * 2 - 1), cellstring(hit, level
*
2 - 1)).Merge()
Me.Range(cellstring(top, level * 2), cellstring(hit, level *
2)).Merge()
If level < 13 Then do_scan(top, hit, level + 1)
top = hit + 1
End While
End Function...

Scott M. said:
Can you show us some code?

A range is an object that is actually a property of a Worksheet, so you
need
to access it correctly.

-Scott

Family Tree Mike said:
:

Hello,
Not sure what I'm doing wrong here in Visual Studio 2008, but I've
included
the following Namespaces and still the compiler is complaining that
'Range'
is not a member of '[TitleofDocument].vb':
Imports Microsoft.Office.Core
Imports Excel = Microsoft.Office.Interop.Excel
Imports System
Imports System.Reflection

Imports Extensibility
imports System.Runtime.InteropServices

...I need 'Range' to be able to move about the spreadsheet that I'm
formatting. I have setup the proper COMs as well. My references are
as
follows:
Microsoft Excel 12.0 Object Library
Microsoft Office 12.0 Object Library

any help would be appreciated,
thanks,
Brian

I think you need to use Excel.Range, based on your imports statements.

Mike
 
"Me.Range" produces an error at every instance. That's all.

there's some namespace I need to import in order for that to work? Or
should I change it to activeSheet.Range?

Thanks for your help,
Brian

Scott M. said:
Well, at first glance, I see what I was refering to earlier. You have
references to "Me.Range", but you need to be accessing ranges via a
Worksheet reference. Now, I assume the code you're showing is embedded in a
Worksheet module in Excel, but even still, if you want to refer to the
current worksheet, you should use the ActiveSheet object, rather than "Me".

Can you tell us exactly what line(s) are failing?

-Scott

Brian B said:
Sure, the following is some of the code responsible for vertical merger of
blank cells in an Excel spreadsheet. I did use some VBA code to perform
this
function initially...but it was too slow for the size of the spreadsheet
that
I generate...this VB.Net code is much faster, I just can't get it to work
for
Office 2007 b/c of this compiling challenge...

...Public Function findlast() As Long
Dim last As Long
Dim found As Boolean

last = 1
found = False
While Not found
found = True
'Debug.WriteLine("CS = " & cellstring(last, 1))
For i As Integer = 1 To 26
If Me.Range(cellstring(last, i)).Value2 <> "" Then found =
False
Next
If Not found Then last = last + 1

End While
findlast = last - 1
End Function
Public Function do_scan(ByVal row_s As Integer, ByVal row_e As Integer,
ByVal level As Integer) As Integer
Dim hit, top As Integer
Dim found As Boolean
top = row_s
While (top <= row_e)
hit = top + 1
found = False
While (hit <= row_e And Not found)
If Me.Range(cellstring(hit, level * 2 - 1)).Value2 <> ""
Then
found = True
hit = hit - 1
Else
hit = hit + 1
End If
End While
If hit > row_e Then hit = row_e
Me.Range(cellstring(top, level * 2 - 1), cellstring(hit, level
*
2 - 1)).Merge()
Me.Range(cellstring(top, level * 2), cellstring(hit, level *
2)).Merge()
If level < 13 Then do_scan(top, hit, level + 1)
top = hit + 1
End While
End Function...

Scott M. said:
Can you show us some code?

A range is an object that is actually a property of a Worksheet, so you
need
to access it correctly.

-Scott

message

:

Hello,
Not sure what I'm doing wrong here in Visual Studio 2008, but I've
included
the following Namespaces and still the compiler is complaining that
'Range'
is not a member of '[TitleofDocument].vb':
Imports Microsoft.Office.Core
Imports Excel = Microsoft.Office.Interop.Excel
Imports System
Imports System.Reflection

Imports Extensibility
imports System.Runtime.InteropServices

...I need 'Range' to be able to move about the spreadsheet that I'm
formatting. I have setup the proper COMs as well. My references are
as
follows:
Microsoft Excel 12.0 Object Library
Microsoft Office 12.0 Object Library

any help would be appreciated,
thanks,
Brian

I think you need to use Excel.Range, based on your imports statements.

Mike
 
Brian said:
"Me.Range" produces an error at every instance. That's all.

there's some namespace I need to import in order for that to work? Or
should I change it to activeSheet.Range?

Thanks for your help,
Brian

Brian,

There are big differences from how you would have done this in VBA, and
doing this in a VB.Net app. VBA runs within Excel, where your VB.net
calls into Excel. In VBA, this is why "Me" is the document you are
working within. I'm pointing this out, only because you haven't
indicated opening the Excel App, then getting the related objects from
the app. I just want to make sure you aren't missing the necessary
first steps.

If you have not looked at http://support.microsoft.com/kb/302094, you
should to understand how VB.Net works with Excel.
 
Actually, "Me" refers to the current module within the document, not the
document itself. But since Excel is made up of either chart sheets or
worksheets, and those sheets can be named and accessed as objects, you
should use the sheet name if possible or ActiveSheet, when you don't know
what the active sheet name will be, but you should shy away from using "Me"
to refer to a worksheet.

The Range object is actually returned by accessing the Range property of a
worksheet. So, you must first have a valid worksheet reference before you
can expect to get your hands on a Range object.

-Scott
 
Back
Top