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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:43:36+00:00 2026-06-11T02:43:36+00:00

Possible Duplicate: How to get the range of occupied cells in excel sheet Im

  • 0

Possible Duplicate:
How to get the range of occupied cells in excel sheet

Im trying to find the last used row in an Excel worksheet. For doing this, I’m using this code:

int lastUsedRow = currentWS.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell,Type.Missing).Row;

Most of the time it works fine, but sometimes Excel thinks that there’s more rows in the sheet than theres suppose to be.

Fx: If I copy data from sheet1, containing 12 rows, to an empty sheet2, then deleting all data in sheet2, by right clicking and press “Delete…” and copy data from sheet3, containing 5 rows, to sheet2, then lastUsedRow will give me the value of 12 which should have been 5.

enter image description here
The image example above is suppose to give my the value of 22 as row count, but instead i’ll get 634. Notice the scroll bar to the right.

It seems like Excel thinks that some cells are filled even though I just deleted all cells, before copying new data with fewer rows into the sheet.

Are there any way to “resize” the view of the data in the sheet, so that i’ll get the right number of used cells or maybe another way to find the number of the last used row?

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-06-11T02:43:38+00:00Added an answer on June 11, 2026 at 2:43 am

    Here is the code I use:

    public static string GetMinimalUsedRangeAddress(Excel.Worksheet sheet)
    {
        string address = String.Empty;
        try
        {
            int rowMax = 0;
            int colMax = 0;
    
            Excel.Range usedRange = sheet.UsedRange;
            Excel.Range lastCell = usedRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            int lastRow = lastCell.Row;
            int lastCol = lastCell.Column;
            int rowMin = lastRow + 1;
            int colMin = lastCol + 1;
    
            int rr = usedRange.Rows.Count;
            int cc = usedRange.Columns.Count;
            for (int r = 1; r <= rr; r++)
            {
                for (int c = 1; c <= cc; c++)
                {
                    Excel.Range cell = usedRange.Cells[r, c] as Excel.Range;
                    if (cell != null && cell.Value != null && !String.IsNullOrEmpty(cell.Value.ToString()))
                    {
                        if (cell.Row > rowMax)
                            rowMax = cell.Row;
                        if (cell.Column > colMax)
                            colMax = cell.Column;
                        if (cell.Row < rowMin)
                            rowMin = cell.Row;
                        if (cell.Column < colMin)
                            colMin = cell.Column;
                    }
                    MRCO(cell);
                }
            }
    
            if (!(rowMax == 0 || colMax == 0 || rowMin == lastRow + 1 || colMin == lastCol + 1))
                address = Cells2Address(rowMin, colMin, rowMax, colMax);
    
            MRCO(lastCell);
            MRCO(usedRange);
        }
        catch (Exception ex)
        {
            // log as needed
        }
        return address; // caller should test return for String.Empty
    }
    
    
    public static string Cells2Address(int row1, int col1, int row2, int col2)
    {
        return ColNum2Letter(col1) + row1.ToString() + ":" + ColNum2Letter(col2) + row2.ToString();
    }
    
    
    public static string ColNum2Letter(int colNum)
    {
        if (colNum <= 26) 
            return ((char)(colNum + 64)).ToString();
    
        colNum--; //decrement to put value on zero based index
        return ColNum2Letter(colNum / 26) + ColNum2Letter((colNum % 26) + 1);
    }
    
    
    public static void MRCO(object obj)
    {
        if (obj == null) { return; }
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        }
        catch
        {
            // ignore, cf: http://support.microsoft.com/default.aspx/kb/317109
        }
        finally
        {
            obj = null;
        }
    }
    

    Note: you might be tempted to replace all the individual cell value checks with CountA but that will fail in certain cases. For example, if a cell contains the formula =IF(A1=55,"Y",""), a resulting empty string will count as a non-blank cell using CountA.

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

Sidebar

Related Questions

Possible Duplicate: Reading Datetime value From Excel sheet I'm trying to read in a
Possible Duplicate: In C, how do I get a specific range of numbers from
Possible Duplicate: Get URL of ASP.Net Page in code-behind I'm trying to hold current
Possible Duplicate: jQuery get input value after keypress I'm trying to get an input
Possible Duplicate: How to implement re-ordering of CoreData records? I'm trying to find a
Possible Duplicate: Get first day of week in PHP? Hi, I want to find
Possible Duplicate: Fastest way to get range complement I have a sorted array of
Possible Duplicate: Get next/previous ISO week and year in PHP I am trying to
Possible Duplicate: Get last n lines of a file with Python, similar to tail
Possible Duplicate: Get first and last day of the week in JavaScript Can any

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.