+ Reply to Thread
Results 1 to 11 of 11

Help editing MACRO to run on any active sheet

  1. #1
    Registered User
    Join Date
    06-21-2019
    Location
    USA
    MS-Off Ver
    Windows 10
    Posts
    10

    Unhappy Help editing MACRO to run on any active sheet

    Hi,

    I created the macro below which inserts a pivot table. I would like it to run on any active sheet and can't figure out how. It is saved in the Personal category so it is not attached to any workbook which is what I want, but the code shows the name of the original sheet (Employee Total Hours Report) that was open when I recorded the macro. I've tried replacing with active.sheet but can't get it to work. Any help would be appreciated.



    HTML Code: 
    Last edited by Peter_Excel; 06-21-2019 at 11:53 AM. Reason: Tags

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Help editing MACRO to run on any active sheet

    Welcome to the board.

    Please take a few minutes to read the Forum Rules, and then edit your post to wrap your code with CODE tags.

    Thanks.

    EDIT: Thank you.
    Last edited by shg; 06-21-2019 at 12:04 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Help editing MACRO to run on any active sheet

    I do no pivot table automation at all, but perhaps

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    06-21-2019
    Location
    USA
    MS-Off Ver
    Windows 10
    Posts
    10

    Re: Help editing MACRO to run on any active sheet

    I'll give it a try, thanks!

  5. #5
    Registered User
    Join Date
    06-21-2019
    Location
    USA
    MS-Off Ver
    Windows 10
    Posts
    10

    Re: Help editing MACRO to run on any active sheet

    I was unable to get this to work, I appreciate your response anyway.

  6. #6
    Registered User
    Join Date
    06-21-2019
    Location
    USA
    MS-Off Ver
    Windows 10
    Posts
    10

    Re: Help editing MACRO to run on any active sheet

    As I continue to look for an answer it seems this thread has become rather famous, showing up high on search results. Whoever provides a working solution will become famous.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Help editing MACRO to run on any active sheet

    Quote Originally Posted by Peter_Excel View Post
    Whoever provides a working solution will become famous.
    In that case, you have the opportunity to share in the glory by giving a better response than "doesn't work", like what happened when you ran it, what happened when you stepped through it, what line errored, what the error message was, ...

  8. #8
    Registered User
    Join Date
    06-21-2019
    Location
    USA
    MS-Off Ver
    Windows 10
    Posts
    10

    Re: Help editing MACRO to run on any active sheet

    I would, except I was unable to be very specific. The debugger opened up and highlighted the entire 1st paragraph or so. I wasn't able to determine which part exactly was causing the problem.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Help editing MACRO to run on any active sheet

    Then you could post a workbook with sensitive data removed that illustrates.

  10. #10
    Registered User
    Join Date
    06-21-2019
    Location
    USA
    MS-Off Ver
    Windows 10
    Posts
    10

    Re: Help editing MACRO to run on any active sheet

    Alright shg, you asked and you have received, but I will take none of the glory. Included is the VBA code and the error I got. I will attatch a dummy sheet if I can get the button to work. The first big paragraph of the code was highlighted yellow.


    PHP Code: 
    Sub Hours_Report_PivotTable()
    '
    Hours_Report_PivotTable Macro
    ' Creates pivot table for weekly hours report
    '

    '
        Dim wks As Worksheet
        
        Set wks = ActiveSheet
        
        With wks
        Columns("A:J").Select
        .Parent.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=.Range("E:E"), _
            Version:=xlPivotTableVersion14 _
            ).CreatePivotTable TableDestination:=.Range("M4"), _
            TableName:="PivotTable3", DefaultVersion:=xlPivotTableVersion14
        
        With .PivotTables("PivotTable3")
          With .PivotFields("File Number")
            .Orientation = xlRowField
            .Position = 1
          End With

          With .PivotFields("Pay Code")
            .Orientation = xlColumnField
            .Position = 1
          End With

          With .PivotFields("Hours")
            .Caption = "Count of Hours"
            .Function = xlCount
          End With

          With .PivotFields("Count of Hours")
            .Caption = "Sum of Hours"
            .Function = xlSum
          End With
        End With

        .Parent.ShowPivotTableFieldList = False
      End With
    End Sub 

    Attachment 630179

    Attachment 630180
    Attached Files Attached Files
    Last edited by Peter_Excel; 06-28-2019 at 04:26 PM.

  11. #11
    Registered User
    Join Date
    06-21-2019
    Location
    USA
    MS-Off Ver
    Windows 10
    Posts
    10

    Re: Help editing MACRO to run on any active sheet


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to go back to the last active sheet and hide the active sheet
    By yatahaze in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-20-2014, 08:18 AM
  2. Macro with reference to active chart on active sheet
    By bhavikpatel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-14-2014, 10:43 AM
  3. VBA - Locking sheet for editing with macro
    By djsouljah in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-02-2014, 11:08 PM
  4. [SOLVED] Macro to look up and down from the active cell, copying the active "section" of the sheet
    By mikkola in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-12-2013, 07:21 PM
  5. [SOLVED] Macro running on active sheet instead of sheet defined in macro
    By davegscott in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-19-2013, 02:04 PM
  6. Macro to Print active sheet as PDF to Active workbook and customize name
    By Scott Taylor in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-17-2012, 08:52 PM
  7. How to keep an cell active for editing.
    By DTK in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-30-2010, 01:28 PM

Tags for this Thread

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