+ Reply to Thread
Results 1 to 2 of 2

When I run my code through my userform, the chart moves to a different workbook

  1. #1
    Registered User
    Join Date
    02-26-2023
    Location
    Baltimore,Maryland
    MS-Off Ver
    Microsoft 365
    Posts
    2

    Unhappy When I run my code through my userform, the chart moves to a different workbook

    Hi,

    I made a code that creates a new workbook, moves the data to a new tab in the new workbook, as well as creates a PivotTable and chart from the raw data.
    The code works well if ran directly as a macro.
    However, I made a userform to run three different versions of the code depending on which button you push.
    If I run the code by selecting a button on the userform, it creates everything perfectly. Yet, if I click off of the chart (to the pivot table or the new raw data tab) then click back to the chart tab,
    it moves the chart back to the original workbook...
    Something must be wrong with my UserForm since it works fine when I run the macro directly, but I have no idea what is wrong. Please assist.
    Here is the workbook as well as the code:
    Raw Data.xlsm

    My Chart code for one of my buttons is:

    Private Sub CommandButton1_Click()
    Call PivotTable_1
    UserForm1.Hide
    End Sub

    My
    Module code is:
    Sub Load_PivotSheet_Options()
    Load UserForm1
    UserForm1.Show
    End Sub

    Sub PivotTable_1()
    Dim PTCache As PivotCache
    Dim PT As PivotTable
    'select current region
    ActiveSheet.Activate
    ActiveCell.CurrentRegion.Copy
    'Make new workbook
    Workbooks.Add
    Application.ScreenUpdating = False
    ActiveCell.CurrentRegion.PasteSpecial
    Sheets("Sheet1").Name = "Raw_Data"
    'Delete PivotSheet if it exists
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("PivotSheet").Delete
    On Error GoTo 0

    'Create a Pivot Cache
    Set PTCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=Range("A1").CurrentRegion.Address)

    'Add new Worksheet
    Worksheets.Add
    ActiveSheet.Name = "Financials"
    ActiveWindow.DisplayGridlines = False

    'Create the Pivot Table from the Cache
    Set PT = ActiveSheet.PivotTables.Add( _
    PivotCache:=PTCache, _
    TableDestination:=Range("A1"), _
    TableName:="BudgetPivot")

    With PT
    'Add Fields
    'xlPageField is for filter
    .PivotFields("Account").Orientation = xlPageField
    .PivotFields("Business Unit").Orientation = xlRowField
    .PivotFields("Scenario").Orientation = xlRowField
    .PivotFields("Jan").Orientation = xlDataField
    .PivotFields("Feb").Orientation = xlDataField
    .PivotFields("Mar").Orientation = xlDataField
    .PivotFields("Apr").Orientation = xlDataField
    .PivotFields("May").Orientation = xlDataField
    .PivotFields("Jun").Orientation = xlDataField
    .PivotFields("Year").Orientation = xlRowField
    'Specify a number format
    .DataBodyRange.NumberFormat = "0,000"

    'Apply a Style
    .TableStyle2 = "PivotStyleMedium3"

    'Change the captions
    .PivotFields("Values").Caption = " Budget & Actual"
    End With

    ActiveSheet.PivotTables("BudgetPivot").PivotSelect "", xlDataAndLabel, True
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Financials_Chart"

    End Sub

  2. #2
    Registered User
    Join Date
    02-26-2023
    Location
    Baltimore,Maryland
    MS-Off Ver
    Microsoft 365
    Posts
    2

    Lightbulb Re: When I run my code through my userform, the chart moves to a different workbook

    Update: I am not sure what was causing that issue. However, I managed to fix it by turning off the pivot table editing prompt at the end of my pivot table code.

+ 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. Excel Userform Text Boxes moves in different screens
    By e129657 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-28-2021, 02:10 AM
  2. [SOLVED] UserForm's Command button moves when using scrollbar
    By zsylvainz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-15-2020, 01:32 AM
  3. [SOLVED] Run VB chart countdown everytime code moves to sheet.
    By saudi_red_neck in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-08-2019, 10:52 AM
  4. UserForm help - display list of open workbooks except workbook userform code is in
    By kensweep in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-09-2014, 12:26 PM
  5. Userform window moves slow
    By pezalmendra in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-01-2012, 02:23 PM
  6. [SOLVED] Legend moves across chart
    By Sinus Log in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-12-2006, 12:20 AM
  7. chart legen moves!
    By [email protected] in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 04-28-2006, 03:20 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