Execute string as vba code using vba

Gideon1995

New Member
Joined
May 8, 2018
Messages
3
I am using code to generate code. The generated code is stored within a string, but i can't execute the string. I have been searching for hours but still find nothing. The goal is to update the filters within a pivot table to only show certain months between a start and finishing date, without having to go and select them.

Code:
Dim field As String

field = "ActiveSheet.PivotTables(" & """FinanceTable""" & ").PivotFields( _" & Chr(13) & """[Transaction Date].[Date YQMD].[Month]""" & ").VisibleItemsList = Array( _" & Chr(13)
Range("F1").Value = field
'Add necessary month fields
For i = Starting_Month To Current_Month
    If i < 10 Then
        field = field + """[Transaction Date].[Date YQMD].[Month].&[20180" & i & "]""" & ", _" & Chr(13)
        Range("F1").Value = field
        
        
    Else
        field = field + "[Transaction Date].[Date YQMD].[Month].&[2018" & i & "]"""
    End If
Next i

field = field & "ActiveSheet.PivotTables(" & "FinanceTable" & ").PivotFields( _" & Chr(13) & """[Transaction Date].[Date YQMD].[Day]""" & ").VisibleItemsList = Array("""")"
Range("F1").Value = field

*Execute String as Function Code*
 
Last edited by a moderator:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Why can't you run the code in the usual way?
 
Upvote 0
Why can't you run the code in the usual way?

I tried using the for loop to create each filter field individually but it doesn't save the previous ones.

Code:
For i = Starting_Month To Current_Month
    ActiveSheet.PivotTables("FinanceTable").PivotFields( _
        "[Transaction Date].[Date YQMD].[Month]").VisibleItemsList = Array( _
        "[Transaction Date].[Date YQMD].[Month].&[20180" & "i" & "]")
next i

I need to keep the previous filtered months *** well. I want to see various columns.
 
Upvote 0
I think what you need to do is create the array of items/months within the loop and then, outside the loop, set the VisibleItemsList.
Code:
Dim arrItems()

    ReDim arrItems(Starting_Month To Current_Month

    For i = Starting_Month To Current_Month
        arrItems(i) =  "[Transaction Date].[Date YQMD].[Month].&[20180" & "i" & "]"
    Next I
        
    ActiveSheet.PivotTables("FinanceTable").PivotFields("[Transaction Date].[Date YQMD].[Month]").VisibleItemsList = arrItems
 
Upvote 0
I think what you need to do is create the array of items/months within the loop and then, outside the loop, set the VisibleItemsList.
Code:
Dim arrItems()

    ReDim arrItems(Starting_Month To Current_Month

    For i = Starting_Month To Current_Month
        arrItems(i) =  "[Transaction Date].[Date YQMD].[Month].&[20180" & "i" & "]"
    Next I
        
    ActiveSheet.PivotTables("FinanceTable").PivotFields("[Transaction Date].[Date YQMD].[Month]").VisibleItemsList = arrItems

This works perfectly Thank You so much! Been sitting from this morning at 9.
 
Upvote 0

Forum statistics

Threads
1,214,858
Messages
6,121,960
Members
449,057
Latest member
FreeCricketId

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top