locked
Excel Column Name RRS feed

  • Question

  • I am assigning excel column name in sheet1 like this in my C# project:

    Excel.Worksheet worksheet = Application.ActiveSheet;

     worksheet.get_Range("A:A", missing).Name = "FirstName";

    If I name a column in sheet2 with the same name sheet one "FirstName" column disappears. Its seems as if the entire workbook requires unique column name. But if I name the column directly in excel it allows duplicate names in different worksheet. Is it possible to have duplicate column names in different sheet via C#?

    Wednesday, April 27, 2011 7:52 PM

Answers

  • In VB, I imagine it might look something like this.

     

            Dim aSheet As Worksheet = xl.ActiveSheet

            Dim sName As String = aSheet.Name

            Dim colRng As String = "=" & sName & "!C1"

            wrkSheet.Names.Add(Name:="ColmA", RefersToR1C1:=colRng)

     


    Regards, Rich
    • Marked as answer by Firehead25 Thursday, April 28, 2011 6:43 PM
    Thursday, April 28, 2011 3:58 PM

All replies

  • Hi Firehead25,

     

    Unless the “refers to” property points to a worksheet I believe your Name will default to the entire workbook, which would then not allow a duplicate name across sheets.

     

    In VB the setting of the name looks like this below.

     

    wrkSheet.Columns("A:A").Select

    wrkSheet.Names.Add Name:="ColmA", RefersToR1C1:= "=Sheet1!C1"

     

    There must be a C# equivalent.

     

    Hope this helps


    Regards, Rich
    Thursday, April 28, 2011 1:38 AM
  • Rich,

    What if I don't know the sheet name?

    Thursday, April 28, 2011 2:50 PM
  • In VB, I imagine it might look something like this.

     

            Dim aSheet As Worksheet = xl.ActiveSheet

            Dim sName As String = aSheet.Name

            Dim colRng As String = "=" & sName & "!C1"

            wrkSheet.Names.Add(Name:="ColmA", RefersToR1C1:=colRng)

     


    Regards, Rich
    • Marked as answer by Firehead25 Thursday, April 28, 2011 6:43 PM
    Thursday, April 28, 2011 3:58 PM