+ Reply to Thread
Results 1 to 7 of 7

Referencing a variable in place of worksheet name

  1. #1
    Registered User
    Join Date
    04-13-2006
    Posts
    26

    Referencing a variable in place of worksheet name

    G'Day everyone

    Trying to create a Pivot on a worksheet that was created for a variable entered by user. But i am having trouble getting Excel to accept the variable as the worksheet name. (below in Red) Any ideas??

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394
    Could the problem be this?

    You have this line of code
    NewSheet1.Name = outputvar1

    but I think you need this instead

    outputvar1 = NewSheet1.Name

  3. #3
    Registered User
    Join Date
    04-13-2006
    Posts
    26
    Quote Originally Posted by Mallycat
    Could the problem be this?

    You have this line of code
    NewSheet1.Name = outputvar1

    but I think you need this instead

    outputvar1 = NewSheet1.Name
    This is not the problem, "outputvar1" has been given a value earlier in the code.

    using "NewSheet1.Name = outputvar1" sets the new sheets name to be that of the value set for "outputvar1"

    I can reference the variable "outputvar1" successfully when naming the newly created worksheet and setting the value of cell D1 - But i cannot use it to identify the worksheet in VBA code again. Why is this??

  4. #4
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394
    Looks OK to me. I have tested it and it works find.

    Try putting a break in the code just before this line, and seeh what the variable evalutes to

  5. #5
    Registered User
    Join Date
    04-13-2006
    Posts
    26
    Please Login or Register  to view this content.
    ^^ This works fine

    Please Login or Register  to view this content.

    ^^ so does this (where the sheet is referenced as a fixed name (100241)


    Please Login or Register  to view this content.

    BUT it is really important that I am able to reference the sheet as a variable as i am dynamically creating up to 10 sheets which could have any different cost centre number (which means i cant hard code the sheet number as above)

    Since i create the pivots on each sheet as i go, is there any way to replace referncing the sheet by it name by using some other type such as "active sheet??"

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612
    I think that the line:
    "'[Overheads.Engine.xls]outputvar1'!R5C1", TableName:="PivotTable1",...
    where you have outputvar1 embedded in double quotes as a text string, should be:
    "'[Overheads.Engine.xls]" & outputvar1 & "'!R5C1", TableName:="PivotTable1",...
    Ben Van Johnson

  7. #7
    Registered User
    Join Date
    04-13-2006
    Posts
    26
    Quote Originally Posted by protonLeah
    I think that the line:
    "'[Overheads.Engine.xls]outputvar1'!R5C1", TableName:="PivotTable1",...
    where you have outputvar1 embedded in double quotes as a text string, should be:
    "'[Overheads.Engine.xls]" & outputvar1 & "'!R5C1", TableName:="PivotTable1",...
    Mate your a bloody legend!!! That does the trick!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1