Copy one sheet for each pivotfields if items have data

L

Lydya

Hello,

I have a pivot table showing this data:

rowfield1 : ValueDate
rowfield2: FundName
columnfield1 : Currency
columnfield2 : BuyOrSell (has 2 items, Buy and Sell)
data : Amount

I'm trying (with VBA) :
-->to copy one sheet per ValueDate, per FundName, per Currency if the item
has an amount data
-->to rename the sheet after the fields items (something like "NameA USD
28_07" for the first sheet, "NameA EUR 28_07" for the 2nd, "NameA EUR,
29_07" for the 3rd and so on).

I gave it many tries but all I get is the two first sheets created with the
right naming, then the other sheets are copied but not renamed.
In fact, I can't get the code to copy and name the sheet only if there is an
amount data in the datafield item.
For example, if there is no Buy for a fund, but only one USD - Sell, there
shouldn't be a sheet copied for the USD - Sell Item)

I hope I made myself clear.

below is my code.

I would be very greatful for your help. Thanks in advance.

Lydya
======================
Here is one of my unsuccessful codes:

Sub CreateSheets()

Dim FundName As PivotItem
Dim ValueDate As PivotItem
Dim Curr As PivotItem
Dim BuyOrSell As PivotItem

Sheets("TCD").Activate

BuyOrSell =
ActiveSheet.PivotTables("TCD").PivotFields("Buysell").PivotItems.PivotItem

For Each FundName In
ActiveSheet.PivotTables("TCD").PivotFields("FundName").PivotItems
For Each ValueDate In ActiveSheet.PivotTables("TCD").PivotFields("Value
date").PivotItems
For Each Curr In ActiveSheet.PivotTables("TCD").PivotFields("Nego
Curr").PivotItems
If Curr.Value <> "" Or Not Curr Is Nothing Then
If BuyOrSell.Value <> "" Or Not BuyOrSell Is Nothing Then
Sheets("RECAP TRESO").Copy After:=Sheets(3)
ActiveSheet.Name = FundName & " " & Curr &
Format(ValueDate, "dd_mm")
End If
End If
Next Curr
Next ValueDate
Next FundName

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top