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 6918127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:49:09+00:00 2026-05-27T09:49:09+00:00

This is an extension of How do I access a cell in Excel Interop

  • 0

This is an extension of How do I access a cell in Excel Interop when using filtered ranges?

Why does the first segment of code iterate through the correct number of rows, but the second segment continue beyond the range of rows? To recap:

   range = ws.UsedRange;
   for (int row = 3; row <= range.Rows.Count; row++)
    {
        Object nObj = ((Excel.Range)ws.Cells[row, "N"]).Text;
    }

Iterates through the correct number of rows but does not take into account filtered rows.

   range = ws.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);
    foreach (Excel.Range area in range.Areas)
    {
        foreach (Excel.Range row in area.Rows)
            //for (int row = 3; row <= range.Rows.Count; row++)
            {
                Object nObj = ((Excel.Range)row.Cells[1, 14]).Text;
            }
    }

Takes into account filtered rows but the inner loop continues beyond the rage of rows per area.

  • 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-27T09:49:10+00:00Added an answer on May 27, 2026 at 9:49 am

    The outer loop across areas seems redundant. You could try removing it and changing the inner loop to

    foreach (Excel.Range row in range.Rows)
    

    Furthermore, Range.UsedRange is known to sometimes return unexpected results. See http://www.vbaexpress.com/kb/getarticle.php?kb_id=82

    There are several alternatives:-

    1. range = ((Excel.Range)ws.Cells[1, 1]).CurrentRegion – if your range has at least one column which has continuous values (no gaps) then this should work well.

    2. Name the range explicitly, so that you can just do range = ws.Range("MyRange")

    3. Find the last row of your range and construct the target range using the first row and last row, e.g. range = ws.Range(ws.Cells[1, 1], ((Excel.Range)ws.Cells[1048576,1]).End(xlUp)).EntireRow (note that 1048576 is the highest row index in Excel 2007 and Excel 2010 and 65536 is the highest row index in earlier versions). Here I am using the first column of the range to determine the last row of the range by calling End(xlUp) on the cell at the bottom of the worksheet in that first column. This requires that the column does not contain any empty values within the data. If the first column can contain empty values, then you should use another column, which is guaranteed to have a value in your data.

    Also, to preserve your skipping of the first two rows, you could try:-

    using System.Linq;
    
    foreach (Excel.Range row in range.Rows.Skip(2))
    

    (Obviously if you used alternative method 3 to determine your target range then you could set the first row to be row 3 instead of row 1 by using ws.Cells[3, 1] as the top bound of the range, so you would not have to call Skip() in this manner).

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

Sidebar

Related Questions

I’m using NHibernate for data access, but accessing it through a façade layer. This
Why does this code get an access error on the Result := Buffer line
I'm currently using this extension to set specific events which handle data in the
I'm writing this code for an extension but It returns: Origin chrome-extension://gjganecebobheilkbpmhmocibjckgidc is not
I have a C extension (code below) in which I'm trying to get access
I've never come across this extension before. It's being used for a front-end interface
I am building a firefox extension. In this extension, I want to see the
I develop an extension module for Guile, written in C. This extension module embeds
The CollectionViewSource.GetDefaultView() method is not in Silverlight 3. In WPF I have this extension
For those of you that use Mercurial with the MQ extension: This is the

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.