none
Problem with undesired selection change RRS feed

  • Question

  • My code performs some changes on several worksheets; particularly, it changes styles of cells. Sometimes, especially when these worksheets are not active, and my code performs style change there, it makes cell selection (on the active sheet) move. Is there any way to avoid this problem?
    Thursday, August 18, 2011 11:08 AM

Answers

  • Hi user4756,

     

    Thanks for posting in the MSDN Forum.

     

    I think this is a work round:

     

          Excel.Worksheet origin = Globals.ThisWorkbook.ActiveSheet as Excel.Worksheet;
          Excel.Worksheet sheet = Globals.ThisWorkbook.Worksheets[2];
          sheet.Activate();
          Excel.Range range = sheet.Range["A1", "G1"];
          range.Style = "Heading 1";
          Excel.Range range2 = sheet.Range["A2", "G2"];
          range2.Style = "Heading 2";
          origin.Activate();
    

     

    I hope it can help you.

     

    Have a good day,

     

    Tom

     

     

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by user4756 Thursday, August 25, 2011 3:05 AM
    Wednesday, August 24, 2011 9:01 AM
    Moderator

All replies

  • Hello,

    What Excel version do you use? Is the latest service pack installed?

    What code line moves the selection?


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Friday, August 19, 2011 5:40 AM
  • Office 2010, all latest updates installed

    This line: 

    range.Style = style;

    When I comment it out, the problem disappears.

    Friday, August 19, 2011 9:24 AM
  • Hello,

    I cannot reproduce the issue on my side. I use Office 2010 SP 1 32-bit. Can you please provide more details? Or, maybe, sampple code?


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Monday, August 22, 2011 8:22 AM
  • Try this code (given that Sheet1 is active):

     

     

    var sheet = (Worksheet)Application.ActiveWorkbook.Worksheets[2];

     

    var range = sheet.Range["A1", "G1"];

    range.Style = "Heading 1";

     

    var range2 = sheet.Range["A2", "G2"];

    range2.Style = "Heading 2";

     

    Monday, August 22, 2011 1:41 PM
  • So, any ideas? Do you have the same bug?
    Wednesday, August 24, 2011 8:11 AM
  • Hi user4756,

     

    Thanks for posting in the MSDN Forum.

     

    I think this is a work round:

     

          Excel.Worksheet origin = Globals.ThisWorkbook.ActiveSheet as Excel.Worksheet;
          Excel.Worksheet sheet = Globals.ThisWorkbook.Worksheets[2];
          sheet.Activate();
          Excel.Range range = sheet.Range["A1", "G1"];
          range.Style = "Heading 1";
          Excel.Range range2 = sheet.Range["A2", "G2"];
          range2.Style = "Heading 2";
          origin.Activate();
    

     

    I hope it can help you.

     

    Have a good day,

     

    Tom

     

     

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by user4756 Thursday, August 25, 2011 3:05 AM
    Wednesday, August 24, 2011 9:01 AM
    Moderator
  • Okay, making that worksheet active removed the problem. The problem happens only when worksheet is in background.
    Is this a bug in Excel?
    Thursday, August 25, 2011 4:36 AM