It happens that Cindy Wang formulated :
is there a way to control the sheet name you add? It could be called
sheet 1, sheet 2 or sheet3, etc. Is there a way to keep it same when
you add a new one? Or when you create a pivot, is there a way you
can always create from sheet1 when using a new sheet? I try to create
a macro, but could not control the sheet name and vloop would not be
able to find the sheet. Thanks,
This appears to be a continuation of the same problem you posted for on
May 11th. IMO, you're trying to get a fully qualified reference for a
newly added sheet that eventually contains a PivotTable. Here's an
example of one way to do this so your code uses object refs rather than
hard-code sheetnames...
Dim wksNew As Worksheet '//use object variable to ref the new sheet
Set wksNew = Worksheets.Add
Use the new sheet's name in a formula...
ActiveCell.Formula = "=VLOOKUP(RC[-2],'" & wksNew.Name _
& "'!R5C1:R3000C3,2,FALSE)"
...though, I suggest you assign defined names to the PivotTable and
LookupValueColumn, and use those instead of R1C1 notation...
ActiveCell.Formula = "=VLOOKUP(LookupValueColName,'" & wksNew.Name _
& "'!<PivotTableName>,2,FALSE)"
...where the defined name for the PivotTable is fully absolute. Make the
defined name for the LookupValue column-absolute/row-relative.
*Note* that after adding the new sheet it becomes the active sheet and
so ActiveCell refers to that sheet. To keep a ref to the sheet where
you want the formula put you need to set a ref to it *before* adding
the new sheet...
Dim wksTarget As Worksheet, wksNew As Worksheet
Set wksTarget = ActiveSheet
Set wksNew = Worksheets.Add
With wksNew
'code to build PivotTable goes here...
End With 'wksNew
wksTarget.ActiveCell.Formula = "=VLOOKUP(LookupValueColName,'" _
& wksNew.Name _
& "'!<PivotTableName>,2,FALSE)"
--
Garry
Free usenet access at
http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion