none
Using Excel.Range.get_Range() within a range, gives unexpected behavior RRS feed

All replies

  • Hi mauja,

    Thank you for posting here.

    According to your question is more related to Excel, I will move it to Excel for Developers forum for suitable support.

    This forum discuss and ask the C# programming language, IDE, libraries, samples and tools.

    Best Regards,

    Wendy


    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.

    Thursday, July 20, 2017 1:53 AM
  • Hi mauja,

    I try to reproduce issue on my side with code below.

     private void Form1_Load(object sender, EventArgs e)
            {
                Excel.Application xlApp;
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;
    
                xlApp = new Excel.Application();
                xlApp.Visible = true;
                xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\v-padee\Desktop\demo.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    
                //--------------------------------------------------------------------------------------------------
    
                Excel.Range data_range = xlWorkSheet.UsedRange.Cells;
                int rows = data_range.Rows.Count;
                int cols = data_range.Columns.Count;
    
                // Get the range containing rows 3 through N.
                Excel.Range c1 = xlWorkSheet.Cells[3, 1];
                Excel.Range c2 = xlWorkSheet.Cells[rows, cols];
                Excel.Range range1 = (Excel.Range)xlWorkSheet.get_Range(c1, c2);
    
                string value1 = System.Convert.ToString(range1.Cells[1, 1].value);
    
                Console.WriteLine(value1);
                textBox1.Text = value1;
    
                //--------------------------------------------------------------------------------------------------
    
                int rows1 = range1.Rows.Count;
                int cols1 = range1.Columns.Count;
    
                Excel.Range d1 = range1.Cells[1, 1]; //3
                Excel.Range d2 = range1.Cells[rows1, cols1]; //5
                string v1 = System.Convert.ToString(d1.Value); //3
                string v2 = System.Convert.ToString(d2.Value);  //5
                MessageBox.Show(v1);  //3
                MessageBox.Show(v2);   //5
                Excel.Range range2 = (Excel.Range)range1.get_Range(d1, d2);
                range2.Select();
                string value2 = System.Convert.ToString(range2.Cells[1, 1].Value);
                textBox2.Text = value2;
                Console.WriteLine(value2);
    
               // xlWorkBook.Close();
                //xlApp = null;
            }
    

    the code is same as yours with some modification for testing.

    you mentioned that both should return '3' as result.

    but if you try to debug the code you will find that your second range is starting from 5.

    so when you try to fetch the value of cell (1,1) it will return 5.

    so the result is correct.

    I also try to select the Range 2 to show you the result here.

    you can check.

    you can see the selected range in Excel sheet. it start from 5th row and contains 3 rows and 1 column.

    so I hope , this will clear all the things.

    Regards

    Deepak


    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.

    Thursday, July 20, 2017 8:31 AM
    Moderator