none
How to Populate a ListBox with Excel Range RRS feed

  • Question

  • Hello,

    I am developing a Document-Level customization for Excel using VS 2010. It is not clear to me how to populate a listbox on a Windows Form with the values from an Excel Range. So for example if I want my listbox to have the values in range "A1:A7" from Sheet2.

    Thanks!

    Monday, June 23, 2014 6:21 PM

Answers

  • Hi,

    We need to pass the value to the Windows Form which we want to populate to the list box. Here is a sample for your reference:

    using Microsoft.Office.Interop.Excel;
    
      public partial class Form1 : Form
    
        {
    
            Range range;
    
            public Form1(Range range)
    
            {
    
                this.range = range;
    
                InitializeComponent();
    
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
    
            {
    
                foreach(Microsoft.Office.Interop.Excel.Range cell in range.Cells)
    
                {
    
                    listBox1.Items.Add(cell.Value);
    
                }
    
                
    
            }
    
        }
    

    To initialize the form you can refer to code below:

      new Form1(Globals.Sheet2.Range["A1:A7"]).Show();
    

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 24, 2014 7:51 AM
    Moderator