What is an Excel Range & Cells object?

J

Jialiang Ge [MSFT]

Hello Dave,

From your post, my understanding on this issue is: you wonder how to set up
an Excel.Range object with multiple cells, how to determine if an Excel
range has a single cell, and how to get its row and column. If I'm off
base, please feel free to let me know.

1. How to set up a range with multiple cells
Excel object model exposes several ways to setup a range with multiple cells
a. Using Range to specify the range in the worksheet
e.g. Excel.Range range1 = worksheet.get_Range("A1", "B2");
b. Using Application.Union to union multiple ranges
e.g. Excel.Range range2 = (Excel.Range)worksheet.Cells[3, 3];
Excel.Range union = application.Union(range1, range2, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing);
Please *note* that the union of non-adjacent ranges will creae multiple
'Areas' in the Range object. You may want to have a look at the issue:
http://msdn.microsoft.com/newsgroups/managed/default.aspx?dg=microsoft.publi
c.office.developer.vba&mid=a3c53ac4-690d-436b-b469-a60672f784ba&sloc=en-us
to have a clearer picture about the union of non-adjacent ranges.
c. Using Application.Intersect to get the intersections of multiple ranges.
e.g. Excel.Range intersection = application.Intersect(range1,
range2, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing);

2. How to determine if an Excel.Range has a single cell.
To determine if an Excel.Range has a sigle cell, we could use
range.Cells.Count property. If the property equals 1, the range is composed
of only one cell.

3. How to get the row and column of a cell.
When we get the cell object of type Excel.Range (union.Cells[1, 1]), we
could use range.Row and range.Column properties to get the information.

Please let me know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

Hi;

My question is a little different. When I get the Application.Selection
object (a Range), how do I access the cells in it - including when it is a
sparse selection? So I am not creating the Range, I get the Range from
Selection and need to know what cells it contains.

Also, can a Range ever include just some of the text in a cell or is a range
always just a collection of cells?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




Jialiang Ge said:
Hello Dave,

From your post, my understanding on this issue is: you wonder how to set up
an Excel.Range object with multiple cells, how to determine if an Excel
range has a single cell, and how to get its row and column. If I'm off
base, please feel free to let me know.

1. How to set up a range with multiple cells
Excel object model exposes several ways to setup a range with multiple cells
a. Using Range to specify the range in the worksheet
e.g. Excel.Range range1 = worksheet.get_Range("A1", "B2");
b. Using Application.Union to union multiple ranges
e.g. Excel.Range range2 = (Excel.Range)worksheet.Cells[3, 3];
Excel.Range union = application.Union(range1, range2, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing);
Please *note* that the union of non-adjacent ranges will creae multiple
'Areas' in the Range object. You may want to have a look at the issue:
http://msdn.microsoft.com/newsgroups/managed/default.aspx?dg=microsoft.publi
c.office.developer.vba&mid=a3c53ac4-690d-436b-b469-a60672f784ba&sloc=en-us
to have a clearer picture about the union of non-adjacent ranges.
c. Using Application.Intersect to get the intersections of multiple ranges.
e.g. Excel.Range intersection = application.Intersect(range1,
range2, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing);

2. How to determine if an Excel.Range has a single cell.
To determine if an Excel.Range has a sigle cell, we could use
range.Cells.Count property. If the property equals 1, the range is composed
of only one cell.

3. How to get the row and column of a cell.
When we get the cell object of type Excel.Range (union.Cells[1, 1]), we
could use range.Row and range.Column properties to get the information.

Please let me know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello Dave,

Sorry for my misunderstanding of your question. To access the cells in a
selected range, including sparse selection, we need to
1. iterate the Areas of the range, because non-adjacent ranges will be
stored in different areas.
2. iterate the Cells of each Area.

Here is a sample code for your reference:

range = (Excel.Range)application.Selection;
for (int i = 1; i <= range.Areas.Count; i++)
{
Excel.Range area = range.Areas as Excel.Range;
for (int j = 1; j <= area.Rows.Count; j++)
{
for (int k = 1; k <= area.Columns.Count; k++)
{
Excel.Range cell = (Excel.Range)area.Cells[j, k];
Console.WriteLine(cell.Value2);
}
}
}
can a Range ever include just some of the text in a cell or is a
range always just a collection of cells?
If a range is comosed of only one cell, we can access its text by calling
range.Value2 directly. If a range contains multiple cells, we need to use
the above method to reach very cells in it.

If you have any other question, please feel free to let me know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

This is weird (I think) but I think I understand now.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




Jialiang Ge said:
Hello Dave,

Sorry for my misunderstanding of your question. To access the cells in a
selected range, including sparse selection, we need to
1. iterate the Areas of the range, because non-adjacent ranges will be
stored in different areas.
2. iterate the Cells of each Area.

Here is a sample code for your reference:

range = (Excel.Range)application.Selection;
for (int i = 1; i <= range.Areas.Count; i++)
{
Excel.Range area = range.Areas as Excel.Range;
for (int j = 1; j <= area.Rows.Count; j++)
{
for (int k = 1; k <= area.Columns.Count; k++)
{
Excel.Range cell = (Excel.Range)area.Cells[j, k];
Console.WriteLine(cell.Value2);
}
}
}
can a Range ever include just some of the text in a cell or is a
range always just a collection of cells?
If a range is comosed of only one cell, we can access its text by calling
range.Value2 directly. If a range contains multiple cells, we need to use
the above method to reach very cells in it.

If you have any other question, please feel free to let me know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top