+ Reply to Thread
Results 1 to 2 of 2

Trying to insert a private sub into new workbook that is created via macro

Hybrid View

  1. #1
    Registered User
    Join Date
    03-06-2017
    Location
    Utah, USA
    MS-Off Ver
    Windows 2016
    Posts
    2

    Question Trying to insert a private sub into new workbook that is created via macro

    I am working on automating a report that pulls credit card spend transaction data, organizes transaction data by approving manager, makes pivot tables from that data, copies the pivot table and underlying data to a new sheet, resets the pivot table source to the data on the new sheet, then saves and closes the new sheet with the pivot table and data that was copied from the source sheet.

    I am running into a problem where when the new workbook is opened when the user is trying to change a filter Excel shows an error that the table was saved without the underlying data.

    I have tried to have the script open the new sheet (after previously closing it), refreshing the data, then closing and saving it, but that doesn't seem to solve the problem.

    Is there a way that I can insert a private sub into the new workbook that runs when the workbook is opened that refreshes the pivot table data to avoid the error?

    Thanks in advance for any help.

  2. #2
    Registered User
    Join Date
    03-06-2017
    Location
    Utah, USA
    MS-Off Ver
    Windows 2016
    Posts
    2

    Lightbulb Re: Trying to insert a private sub into new workbook that is created via macro

    I was able to find a problem to my own problem, I am not sure if there is a better way, but what I did was to make a mastersheet in my data workbook that has this code on the sheet,

    Public sheetopened As Boolean

    Private Sub worksheet_activate()
    If sheetopened = False Then
    ThisWorkbook.RefreshAll
    sheetopened = True
    End If
    End Sub


    I then put code on the "ThisWorkbook" sheet in the VBA editor so that the master sheet was copied every time that a new sheet is created so that the new sheet has the update code. Here is that code:

    Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Dim tmpName As String

    tmpName = Sh.Name
    Sheets("MasterSheet").Copy Before:=Sheets(Sh.Name)
    Application.DisplayAlerts = False
    Sheets(Sh.Name).Delete
    Application.DisplayAlerts = True
    Sheets("MasterSheet (2)").Name = tmpName
    Sheets(tmpName).Visible = True
    Sheets(tmpName).Activate
    End Sub

    This then made it so that when my normal macro runs to make the pivot tables and copy them to new workbooks the update code is copied with it. The only thing that I had to do was to make it so that the user has to change sheets to select the sheet with the pivot table in order for the update code able to work as it will not run when the workbook is opened unless the user leaves and returns to the sheet with the pivot table. I hope this helps anyone who is having the same problem that I was having.

+ 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. HELP!! Can not run macro from a workbook created from a macro enabled template.
    By wacky_diva in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-08-2014, 01:10 AM
  2. Using a macro to add code to a newly created workbook.
    By Alagard in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-16-2010, 06:08 PM
  3. Open newly created macro from new workbook
    By stubarso in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-06-2010, 02:30 PM
  4. Hiding New Workbook created using Macro Recorder
    By deelee in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-25-2006, 09:55 AM
  5. Macro run each time a new workbook created
    By cmorton89 in forum Excel General
    Replies: 0
    Last Post: 03-08-2006, 03:58 PM
  6. [SOLVED] How can I edit/delet a macro that has been created as a private ma
    By smmischief in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-08-2006, 12:20 PM
  7. [SOLVED] How do I copy macro into new workbook that was created automatica.
    By Darshan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-10-2005, 10:06 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