+ Reply to Thread
Results 1 to 7 of 7

Referencing a variable in place of worksheet name

Hybrid View

marksince1984 Referencing a variable in... 10-13-2006, 10:00 PM
Mallycat Could the problem be this? ... 10-13-2006, 10:34 PM
marksince1984 This is not the problem,... 10-13-2006, 10:59 PM
Mallycat Looks OK to me. I have... 10-14-2006, 12:14 AM
marksince1984 'SECTION TWO - create new... 10-15-2006, 12:31 AM
  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??

        'Create sheet 1
        
        Set NewSheet1 = Worksheets.Add
        NewSheet1.Name = outputvar1
        Range("A1").Select
        ActiveCell.FormulaR1C1 = "This sheet will hold the pivot for:"
        Range("D1").Select
        ActiveCell.FormulaR1C1 = outputvar1
        
                'Creates the pivot table on sheet
                        Dim WSD As Worksheet
                        Dim PTCache As PivotCache
                        Dim PT As PivotTable
                        Dim PRange As Range
                        Dim FinalRow As Long
                        Set WSD = Worksheets(outputvar1)
    
                        
                        ' Delete any prior pivot tables
                        For Each PT In WSD.PivotTables
                            PT.TableRange2.Clear
                        Next PT
                        
                        ' Define input area and set up a Pivot Cache
                        FinalRow = WSD.Cells(65536, 1).End(xlUp).Row
                        Set PRange = WSD.Cells(1, 1).Resize(FinalRow, 8)
                        Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange.Address)
                            
                        Set PT = PTCache.CreatePivotTable(TableDestination:=WSD.Range("J2"), TableName:="PivotTable1")
                        PT.ManualUpdate = True
                        ' Set up the row  & column fields
                        PT.AddFields RowFields:=Array("Product", "Customer"), ColumnFields:="Region"
                            
                        ' Set up the data fields
                        With PT.PivotFields("Revenue")
                            .Orientation = xlDataField
                            .Function = xlSum
                            .Position = 1
                        End With
                        
                        ' Calc the pivot table
                        PT.ManualUpdate = False
                        PT.ManualUpdate = True

  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
    'SECTION TWO - create new worksheets for each individual cost centre based on variables
    
        'Create sheet 1
       
        Set Newsheet1 = Worksheets.Add
        Newsheet1.Name = outputvar1
        Range("A1").FormulaR1C1 = "This sheet will hold the pivot for:"
        Range("D1").FormulaR1C1 = outputvar1
    ^^ This works fine

    'Creates the pivot table on sheet        
      ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
      "Control!$A:$X").CreatePivotTable TableDestination:= _
      "'[Overheads.Engine.xls]100241'!R5C1", TableName:="PivotTable1",
     DefaultVersion:=xlPivotTableVersion10

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


    'Creates the pivot table on sheet        
      ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
      "Control!$A:$X").CreatePivotTable TableDestination:= _
      "'[Overheads.Engine.xls]outputvar1'!R5C1", TableName:="PivotTable1",
     DefaultVersion:=xlPivotTableVersion10

    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
    Win10/MSO2016
    Posts
    12,969
    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

+ 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