none
Why does this excel interop code only work when its in its own class? RRS feed

  • Question

  • Excel Project Source

                    //loop merge objective cell label ranges
                    Microsoft.Office.Interop.Excel.Range range1450;
                    for (int i = 2; i <= 240; i++)
                    {
                        range1450 = oSheet.Range[oSheet.Cells[7, i], oSheet.Cells[20, i]];
                        range1450.Cells.Merge(System.Type.Missing);
                        i = i + 6;
                    }

    This code works if I create a new button and run it from there while there is no other code.

    Once I insert it at line 1641 in my project, it does not work, it does nothing at all and there are no error messages. If I insert the code at some earlier point in the project, it does not work and does not produce an error message.

    Does anyone have any idea why this is happening??

    Friday, August 3, 2018 2:42 PM

Answers

  • Hello _sniffles_,

    In my test, it does works for me. I could see the range B7:B20 is merged and I7:I20 is also merged and so on....

    What's your test result? Have you tried to set a break point to check if the code is executed? Could you share the workbook you generated for comparing?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by _Sniffles_ Saturday, August 11, 2018 2:05 PM
    Monday, August 6, 2018 5:34 AM

All replies

  • Hello _sniffles_,

    In my test, it does works for me. I could see the range B7:B20 is merged and I7:I20 is also merged and so on....

    What's your test result? Have you tried to set a break point to check if the code is executed? Could you share the workbook you generated for comparing?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by _Sniffles_ Saturday, August 11, 2018 2:05 PM
    Monday, August 6, 2018 5:34 AM
  • No I have not tried that. I am not familiar with break points but I will read up on it and figure out how to use them and see if I can figure that out.

    Sorry for the late reply, for some reason I am not getting notified when there is a reply even though I have it set to notify me and alert me.

    All of the code was made available through the link above. I am not using an existing workbook, just the one generated by running the code.

    I will try your suggestion to troubleshoot and see what I come up with. Thank you for that tip.


    • Edited by _Sniffles_ Saturday, August 11, 2018 1:15 PM
    Saturday, August 11, 2018 1:15 PM
  • I read up on breakpoints and such then tried to implement some to figure out what was going on. The code is running and working properly now! I made excel visible and ran it with breakpoints. Doing that allowed me to see cells merging but then it seemed to revert back to an un-merged state.

    I was running that merge test code on the first sheet which was was deleted later on in the code because it was only used to set everything up, it was not a permanent part of the final spreadsheet. I just didn't realize I had some code after that which deleted the sheet.

    Since I had excel hidden I did not know and when it was visible it only looked like it worked then reverted back but that's because the test sheet was deleted and the first usable sheet looked exactly the same but did not have merged cells, I only had that code running on oSheet and had not made it run on all sheets yet.

    Thanks again Terry, I really appreciate your help and your comments. I've learned a great deal and this will make many lives much easier.

    Saturday, August 11, 2018 2:05 PM