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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:37:04+00:00 2026-05-24T05:37:04+00:00

I have a problem selecting a cell in excel using c# while running through

  • 0

I have a problem selecting a cell in excel using c# while running through a “for” loop.

I have 3 datatables in a dataset(dsReportGrid) which I’m exporting to excel (one sheet per table).
When I try to select a range so I can freeze panes, it goes well in the first iteration of the loop (dsReportGrid.table[0]), but when it gets to second iteration when I try to select the cell in the secondsheet it throws me a COM exception on the select event (the code line with two asterisks).

Why is it happening and how can I handle it?

Thanks,

    private void saveXlS()
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        Excel.Range xlCells;
        Excel.Range freezeCell;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Add(misValue);

        for (int table = 0; table <= dsReportGrid.Tables.Count -1; table++)
        {

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(table +1);
               Excel.Range k = xlWorkSheet.get_Range("a2");
               **k.Select();**
               k.Application.ActiveWindow.FreezePanes = true;
               break;
        }
        ...rest of code writing from dataset to excel
   }
  • 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-24T05:37:04+00:00Added an answer on May 24, 2026 at 5:37 am

    Try activating the worksheet before attempting to select the range. I think you can only select a range on an active worksheet.

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

Sidebar

Related Questions

I have a problem with selecting not first selector using attributeContains jQuery('div[id*=abc]:not(:first)').hide();
I have a little problem with a query. I'm selecting data using the between
I have a problem selecting a checked radio button with jquery. The radio buttons
I have recently stumbled upon a problem with selecting relationship details from a 1
I have what seems like a simple problem with some data in Excel. I
On selecting a cell in a UITableView (which itself is placed on the rootView
I have a problem which I am trying to solve with genetic algorithms. The
I seem to have a dilemma. I have an EXCEL 2003 template which users
I have a problem with my VBA code in an excel spreadsheet containing orders.
I have the following problem in an Excel table. I want to have 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.