none
Excel VSTO sheet codenames RRS feed

  • Question

  • When creating Excel programs with VBA, one of the key things for keeping the code robust is to use sheet codenames, which prevents the user from breaking the program the moment he/she decides to rename the sheet and move the order of sheets like when he/she has worksheets "a" and "b" and then rearranges and renames them to be "bananas" and "apples". Basically those codenames are like names of sheets while those users sees are captions.

    Now when using VSTO, I have understood you are not supposed to use sheet codenames. So in order to make the code robust like with VBA, you need to...?

    To give a code example, in VBA the wrong style (for setting the value of cell A1 to 1) is

    Worksheets("Sheet1").Range("A1").value = 1

    and the right style is

    Sheet1.Range("A1").value = 1

    In VSTO, the wrong style is

    Worksheets("Sheet1").Range("A1").value = 1

    and the right style is...?

    Monday, July 15, 2013 12:25 PM

Answers

  • hi kilpAr,

    You can write code like this.

     Globals.Sheet1.Range["A1"].Value = 1;

    • Marked as answer by KilpAr Monday, July 15, 2013 1:49 PM
    Monday, July 15, 2013 1:36 PM

All replies

  • hi kilpAr,

    You can write code like this.

     Globals.Sheet1.Range["A1"].Value = 1;

    • Marked as answer by KilpAr Monday, July 15, 2013 1:49 PM
    Monday, July 15, 2013 1:36 PM
  • hi kilpAr,

    You can write code like this.

     Globals.Sheet1.Range["A1"].Value = 1;

    Exactly what I was looking for, thanks!

    To see this in some action, try 

    Globals.Sheet1.Name = "the new sheet caption"

    Globals.Sheet1.Range["A1"].Value = "doesn't care about new sheet name"


    Monday, July 15, 2013 1:54 PM