+ Reply to Thread
Results 1 to 10 of 10

Help converting a workbook function into a Macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    522

    Help converting a workbook function into a Macro

    Dear Experts
    I found the following workbook function on the web and i dont know how to convert it into a macro. Being a new user of VBA i have tried and failed in last cpl of days
    Option Explicit
    Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    Dim ptTable As PivotTable, strField As String
    strField = "FIELD 1"' I WOULD WANT TO RENAME THIS TO ROUTE instead of FIELD 1
    On Error GoTo ExitPoint
    Application.EnableEvents = False
    For Each ptTable In ActiveSheet.PivotTables
        If ptTable <> Target Then
            ptTable.PageFields(strField).CurrentPage = Target.PageFields(strField).CurrentPage.Value
        End If
    Next ptTable
    ExitPoint:
    Application.EnableEvents = True
    End Sub
    Last edited by VKS; 02-11-2013 at 01:36 PM. Reason: Thanks Norie

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Help converting a workbook function into a Macro

    Why do you want to convert it to a macro?

    As it is this code, in a worksheet module, will be triggered whenever a pivot table on the worksheet is updated.

    Do you want to do something different?

    Also, how would you want to call the code?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    522

    Re: Help converting a workbook function into a Macro

    Hello Norie, Worksheet thing does not work when i use it in my workbook with a same pivot table pasted 6 times giving 6 diff views on the same dashboard (same pivot table but with data from external source i.e. access)
    Best Regards/VKS

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Help converting a workbook function into a Macro

    Do you mean the code is running but not doing what you expect it to?

    Can you attach a sample workbook?

  5. #5
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    522

    Re: Help converting a workbook function into a Macro

    No it is not running at all even after i am pasting it in my current workbook(Unless i am not pasting it correctly)

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Help converting a workbook function into a Macro

    The code should go in a worksheet module.

    To access a worksheet's module right click it's tab and select code.

    The window that opens is where the code needs to go.

  7. #7
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    522

    Re: Help converting a workbook function into a Macro

    Sorry Norie.......It didnt work even after right clicking and pasting the code. Do u think the culprit could be the source data which is not in the worksheet but in access?

  8. #8
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    522

    Re: Help converting a workbook function into a Macro

    By the way you do have my star now ;-)

  9. #9
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    522

    Re: Help converting a workbook function into a Macro

    My sincere apologies Norie,
    It actually worked after I unchecked select multiple items.
    I am sorry. You have been a great help cant thank you enough on this….
    Best Regards/VKS
    Marking it as closed Thanks to Norie for all the help

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Help converting a workbook function into a Macro

    I think you need to check if the code is being triggered.

    To do that put a breakpoint on the first line of the code with F9.

    Then return to the worksheet and make a change on one of the pivot tables.

    If the code is being triggered you should be transferred to the line of code you set the breakpoint on.

    If that doesn't happen then the code isn't being triggered.

    By the way, I checked with an external data source, Access, and the code is triggered when I make a change, eg filter, the pivot table.

+ 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