Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 562857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:35:55+00:00 2026-05-13T12:35:55+00:00

I’m using Microsoft.Office.Interop.Excel, and I can’t find a way to return the selected rows.

  • 0

I’m using Microsoft.Office.Interop.Excel, and I can’t find a way to return the selected rows. What I mean by “selected” is, the row numbers themselves when you click on the row gutter on the left and select one or more contiguous or non-contiguous rows (which highlights the whole row.) This is different from selecting a region or area on the sheet itself.

So far, I’ve looked at app.Selection, app.Cells, app.Rows, and none of these appear to give me the rows. app.Selection gives me the actual cell content, which is NOT what I want.

Any ideas? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:35:56+00:00Added an answer on May 13, 2026 at 12:35 pm

    To do this, you need to enumerate each Area in the Range, and then enumerate the Rows within each area. Somewhat confusingly, the Excel.Range.Rows property returns a collection of ranges (each representing a row), while the Excel.Range.Row property returns the row number of the top-left cell in the range.

    Therefore, to get the row numbers, you should enumerate the Areas and Rows collections and then access the Range.Row. For example:

    Excel.Range range = (Excel.Range)excelApp.Selection;
    
    List<int> rowNumbers = new List<int>();
    
    // Enumerate the Rows within each Area of the Range.
    foreach (Excel.Range area in range.Areas)
    {
        foreach (Excel.Range row in area.Rows)
        {
            rowNumbers.Add(row.Row);
        }
    }
    
    // Report the results.
    foreach (int rowNumber in rowNumbers)
    {
        MessageBox.Show(rowNumber.ToString());
    }
    

    You could even use Linq if you wanted.

    Using Query Syntax:

    IEnumerable<int> rowNumbers =
        from area in range.Areas.Cast<Excel.Range>()
        from row in area.Rows.Cast<Excel.Range>()
        select row.Row;
    

    Using Method Syntax:

    IEnumerable<int> rowNumbers =
        range.Areas.Cast<Excel.Range>()
        .SelectMany(area => area.Rows.Cast<Excel.Range>()
                            .Select(row => row.Row));
    

    Update: How to Return Distinct Row Numbers:

    To return distinct row numbers (which can occur when a multi-area range is selected where the areas do not comprise entire rows).

    (1) The easiest is to use Linq and make use of the Distinct operator. For example:

    Using Query Syntax:

    IEnumerable<int> distinctRowNumbers =
        (from area in range.Areas.Cast<Excel.Range>()
         from row in area.Rows.Cast<Excel.Range>()
         select row.Row)
        .Distinct();
    

    Using Method Syntax:

    IEnumerable<int> distinctRowNumbers =
        range.Areas.Cast<Excel.Range>()
        .SelectMany(area => area.Rows.Cast<Excel.Range>()
                            .Select(row => row.Row))
        .Distinct();
    

    (2) If not making use of Linq, then you could use a HashSet. For example:

    Excel.Range range = (Excel.Range)excelApp.Selection;
    
    HashSet<int> distinctRowNumbers = new HashSet<int>();
    
    // Enumerate the Rows within each Area of the Range.
    foreach (Excel.Range area in range.Areas)
    {
        foreach (Excel.Range row in area.Rows)
        {
            distinctRowNumbers.Add(row.Row);
        }
    }
    
    // Report the results.
    foreach (int rowNumber in distinctRowNumbers)
    {
        MessageBox.Show(rowNumber.ToString());
    }
    

    (3) Perhaps the most intuitive approach is to convert your original range to entire rows first, and then iterate the rows. In this case, the rows are already guaranteed to be unique, so we don’t need to use a HashSet or the Distinct operator.

    In these examples, note the use Excel.Range.EntireRow, which is applied to the original range before enumerating the areas and rows:

    Enumerating without using Linq:

    List<int> distinctRowNumbers = new List<int>();
    
    foreach (Excel.Range area in range.EntireRow.Areas)
    {
        foreach (Excel.Range row in area.Rows)
        {
            distinctRowNumbers.Add(row.Row);
        }
    }
    

    Linq using query syntax:

    IEnumerable<int> distinctRowNumbers =
        from area in range.EntireRow.Areas.Cast<Excel.Range>()
        from row in area.Rows.Cast<Excel.Range>()
        select row.Row;
    

    Linq using method syntax:

    IEnumerable<int> distinctRowNumbers =
        range.EntireRow.Areas.Cast<Excel.Range>()
        .SelectMany(area => area.Rows.Cast<Excel.Range>()
                            .Select(row => row.Row));
    

    Note that the Range.EntireRow May Return Incorrect Result, but, if I understand this correctly, this only applies to Excel ’95 and below, which is completely obsolete. (I would not worry about any version of Excel below Excel ’97.) If you are concerned about any versioning issues, however, and do not have the luxury of testing in all Excel versions, then you might want to stick to using a HashSet, or use Linq along with the Distinct operator, to ensure that your row numbers are unique.

    Hope this helps!

    Mike

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.