+ Reply to Thread
Results 1 to 6 of 6

Macros in custom menu not working properly

Hybrid View

fecurtis Macros in custom menu not... 05-12-2008, 02:48 PM
fecurtis Here are the macros that it... 05-12-2008, 02:53 PM
fecurtis Sub TSACreateChart() Dim... 05-12-2008, 02:53 PM
fecurtis Sub TotalCreateChart() ... 05-12-2008, 02:54 PM
fecurtis I realized that if I try... 05-12-2008, 03:54 PM
fecurtis I figured it out. Nevermind. 05-12-2008, 05:27 PM
  1. #1
    Forum Contributor
    Join Date
    04-30-2008
    Posts
    105

    Macros in custom menu not working properly

    I wrote this code that is supposed to create graphs and automatically format them. I then made this custom menu that was to be used as an Add-In but whenever I try to run the macro from the custom menu, it won't run properly. Heres what I have as my code:

    Sub DeleteMenu()
        On Error Resume Next
        CommandBars(1).Controls("Variance Analysis").Delete
        On Error GoTo 0
    End Sub
    Sub CreateMenu()
    
        Dim HelpMenu As CommandBarControl
        Dim NewMenu As CommandBarPopup
        Dim MenuItem As CommandBarControl
        Dim SubMenuItem As CommandBarButton
        
        Call DeleteMenu
        
        Set HelpMenu = CommandBars(1).FindControl(ID:=30010)
        
        If HelpMenu Is Nothing Then
            Set NewMenu = CommandBars(1).Controls _
                .Add(Type:=msoControlPopup, Temporary:=True)
        Else
            Set NewMenu = CommandBars(1).Controls _
                .Add(Type:=msoControlPopup, Before:=HelpMenu.Index, _
                Temporary:=True)
        End If
        
        NewMenu.Caption = ("&Variance Analysis")
        
        'FIRST MENU ITEM
        Set MenuItem = NewMenu.Controls.Add _
            (Type:=msoControlPopup)
        With MenuItem
            .Caption = ("Populate Variance Table")
        End With
            
        'FIRST SUBMENU ITEM
        Set SubMenuItem = MenuItem.Controls.Add _
            (Type:=msoControlButton)
        With SubMenuItem
            .Caption = "TSA Var Table"
            .OnAction = "FormatTSAVariance"
            .FaceId = 162
        End With
        
        'SECOND SUBMENU ITEM
        Set SubMenuItem = MenuItem.Controls.Add _
            (Type:=msoControlButton)
        With SubMenuItem
            .Caption = "DHS Var Table"
            .OnAction = "FormatDHSVariance"
            .FaceId = 162
        End With
            
        'SECOND MENU ITEM
        Set MenuItem = NewMenu.Controls.Add _
            (Type:=msoControlPopup)
        With MenuItem
            .Caption = ("Create Variance Graphs")
            .BeginGroup = True
        End With
        
        'FIRST SUBMENU UNDER SECOND MENU ITEM
        Set SubMenuItem = MenuItem.Controls.Add _
            (Type:=msoControlButton)
        With SubMenuItem
            .Caption = ("TSA Var Graphs")
            .OnAction = "TSACreateChart"
            .FaceId = 65
        End With
        
        'SECOND SUBMENU UNDER SECOND MENU ITEM
        Set SubMenuItem = MenuItem.Controls.Add _
            (Type:=msoControlButton)
        With SubMenuItem
            .Caption = ("DHS Var Graphs")
            .OnAction = "DHSCreateChart"
            .FaceId = 65
        End With
        
        'THIRD SUBMENU UNDER THRID MENU ITEM
        Set SubMenuItem = MenuItem.Controls.Add _
            (Type:=msoControlButton)
        With SubMenuItem
            .Caption = ("Total Var Graphs")
            .OnAction = "TotalCreateChart"
            .FaceId = 65
        End With
    End Sub

  2. #2
    Forum Contributor
    Join Date
    04-30-2008
    Posts
    105
    Here are the macros that it uses:

    Sub DHSCreateChart()
    
        Dim UserRangeOrders As Range
        Dim UserRangeRevenue As Range
        Dim UserRangeProfit As Range
        
        Dim W As Long, H As Long
        Dim TopPosition As Long, LeftPosition As Long, RightPosition As Long
        Dim ChtObj As ChartObject
        
        Set UserRangeOrders = Range("$A$17:$C$22")
        Set UserRangeRevenue = Range("$A$17:$A$22, $E$17:$F$22")
        Set UserRangeProfit = Range("$A$17:$A$22, $H$17:$I$22")
        
        Application.ScreenUpdating = False
            
        Sheets("DHS vs Op Plan").Activate
        
            Charts.Add
            ActiveChart.Location Where:=xlLocationAsObject, Name:="DHS Var Graphs"
            With ActiveChart
                .SetSourceData UserRangeOrders
                .HasTitle = True
                .ChartType = xlColumnClustered
                .ChartTitle.Characters.Text = "DHS Order Variance ($M)"
                .Axes(xlValue).HasMajorGridlines = False
                .Legend.Position = xlLegendPositionBottom
                .Legend.Border.LineStyle = x1None
                .Axes(xlValue).TickLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .ApplyDataLabels xlDataLabelsShowValue
                .SeriesCollection(1).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .SeriesCollection(2).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .PlotArea.Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, _
                    Degree:=0.331364919508659
                .PlotArea.Fill.Visible = True
                .PlotArea.Fill.ForeColor.SchemeColor = 40
                .Axes(xlCategory).TickLabelPosition = xlLow
                .Deselect
            End With
        
        Sheets("DHS vs Op Plan").Activate
        
            Charts.Add
            ActiveChart.Location Where:=xlLocationAsObject, Name:="DHS Var Graphs"
            With ActiveChart
                .SetSourceData UserRangeRevenue
                .HasTitle = True
                .ChartType = xlColumnClustered
                .ChartTitle.Characters.Text = "DHS Revenue Variance ($M)"
                .Axes(xlValue).HasMajorGridlines = False
                .Legend.Position = xlLegendPositionBottom
                .Legend.Border.LineStyle = x1None
                .Axes(xlValue).TickLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .ApplyDataLabels xlDataLabelsShowValue
                .SeriesCollection(1).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .SeriesCollection(2).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .PlotArea.Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, _
                    Degree:=0.331364919508659
                .PlotArea.Fill.Visible = True
                .PlotArea.Fill.ForeColor.SchemeColor = 40
                .Axes(xlCategory).TickLabelPosition = xlLow
                .Deselect
            End With
        
        Sheets("DHS vs Op Plan").Activate
    
            Charts.Add
            ActiveChart.Location Where:=xlLocationAsObject, Name:="DHS Var Graphs"
            With ActiveChart
                .SetSourceData UserRangeProfit
                .HasTitle = True
                .ChartType = xlColumnClustered
                .ChartTitle.Characters.Text = "DHS Profit Variance ($M)"
                .Axes(xlValue).HasMajorGridlines = False
                .Legend.Position = xlLegendPositionBottom
                .Legend.Border.LineStyle = x1None
                .Axes(xlValue).TickLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .ApplyDataLabels xlDataLabelsShowValue
                .SeriesCollection(1).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .SeriesCollection(2).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .PlotArea.Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, _
                    Degree:=0.331364919508659
                .PlotArea.Fill.Visible = True
                .PlotArea.Fill.ForeColor.SchemeColor = 40
                .Axes(xlCategory).TickLabelPosition = xlLow
                .Deselect
            End With
        
            W = ActiveChart.Parent.Width - 5
            H = ActiveChart.Parent.Height - 5
            TopPosition = 1
            LeftPosition = 10
            For Each ChtObj In ActiveSheet.ChartObjects
                ChtObj.Width = W
                ChtObj.Height = H
                ChtObj.Top = TopPosition
                ChtObj.Left = LeftPosition
                TopPosition = TopPosition + ChtObj.Height
            Next ChtObj
        Application.ScreenUpdating = True
            
    End Sub

  3. #3
    Forum Contributor
    Join Date
    04-30-2008
    Posts
    105
    Sub TSACreateChart()
        Dim UserRangeOrders As Range
        Dim UserRangeRevenue As Range
        Dim UserRangeProfit As Range
        
        Dim W As Long, H As Long
        Dim TopPosition As Long, LeftPosition As Long, RightPosition As Long
        Dim ChtObj As ChartObject
        
        Set UserRangeOrders = Range("$A$17:$C$22")
        Set UserRangeRevenue = Range("$A$17:$A$22, $E$17:$F$22")
        Set UserRangeProfit = Range("$A$17:$A$22, $H$17:$I$22")
        
        Application.ScreenUpdating = False
            
        Sheets("TSA vs Op Plan").Activate
        
            Charts.Add
            ActiveChart.Location Where:=xlLocationAsObject, Name:="TSA Var Graphs"
            With ActiveChart
                .SetSourceData UserRangeOrders
                .HasTitle = True
                .ChartType = xlColumnClustered
                .ChartTitle.Characters.Text = "TSA Order Variance ($M)"
                .Axes(xlValue).HasMajorGridlines = False
                .Legend.Position = xlLegendPositionBottom
                .Legend.Border.LineStyle = x1None
                .Axes(xlValue).TickLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .ApplyDataLabels xlDataLabelsShowValue
                .SeriesCollection(1).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .SeriesCollection(2).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .PlotArea.Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, _
                    Degree:=0.331364919508659
                .PlotArea.Fill.Visible = True
                .PlotArea.Fill.ForeColor.SchemeColor = 40
                .Axes(xlCategory).TickLabelPosition = xlLow
                .Deselect
            End With
        
        Sheets("TSA vs Op Plan").Activate
        
            Charts.Add
            ActiveChart.Location Where:=xlLocationAsObject, Name:="TSA Var Graphs"
            With ActiveChart
                .SetSourceData UserRangeRevenue
                .HasTitle = True
                .ChartType = xlColumnClustered
                .ChartTitle.Characters.Text = "TSA Revenue Variance ($M)"
                .Axes(xlValue).HasMajorGridlines = False
                .Legend.Position = xlLegendPositionBottom
                .Legend.Border.LineStyle = x1None
                .Axes(xlValue).TickLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .ApplyDataLabels xlDataLabelsShowValue
                .SeriesCollection(1).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .SeriesCollection(2).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .PlotArea.Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, _
                    Degree:=0.331364919508659
                .PlotArea.Fill.Visible = True
                .PlotArea.Fill.ForeColor.SchemeColor = 40
                .Axes(xlCategory).TickLabelPosition = xlLow
                .Deselect
            End With
        
        Sheets("TSA vs Op Plan").Activate
    
            Charts.Add
            ActiveChart.Location Where:=xlLocationAsObject, Name:="TSA Var Graphs"
            With ActiveChart
                .SetSourceData UserRangeProfit
                .HasTitle = True
                .ChartType = xlColumnClustered
                .ChartTitle.Characters.Text = "TSA Profit Variance ($M)"
                .Axes(xlValue).HasMajorGridlines = False
                .Legend.Position = xlLegendPositionBottom
                .Legend.Border.LineStyle = x1None
                .Axes(xlValue).TickLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .ApplyDataLabels xlDataLabelsShowValue
                .SeriesCollection(1).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .SeriesCollection(2).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .PlotArea.Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, _
                    Degree:=0.331364919508659
                .PlotArea.Fill.Visible = True
                .PlotArea.Fill.ForeColor.SchemeColor = 40
                .Axes(xlCategory).TickLabelPosition = xlLow
                .Deselect
            End With
        
            W = ActiveChart.Parent.Width - 5
            H = ActiveChart.Parent.Height - 5
            TopPosition = 1
            LeftPosition = 10
            For Each ChtObj In ActiveSheet.ChartObjects
                ChtObj.Width = W
                ChtObj.Height = H
                ChtObj.Top = TopPosition
                ChtObj.Left = LeftPosition
                TopPosition = TopPosition + ChtObj.Height
            Next ChtObj
        Application.ScreenUpdating = True
            
    End Sub

  4. #4
    Forum Contributor
    Join Date
    04-30-2008
    Posts
    105
    Sub TotalCreateChart()
        Dim UserRangeOrders As Range
        Dim UserRangeRevenue As Range
        Dim UserRangeProfit As Range
        
        Dim W As Long, H As Long
        Dim TopPosition As Long, LeftPosition As Long, RightPosition As Long
        Dim ChtObj As ChartObject
        
        Set UserRangeOrders = Range("$A$2:$C$7")
        Set UserRangeRevenue = Range("$A2:$A$7, $E$2:$F7")
        Set UserRangeProfit = Range("$A$2:$A$7, $H$2:$I$7")
        
        Application.ScreenUpdating = False
            
        Sheets("Total vs Op Plan").Activate
        
            Charts.Add
            ActiveChart.Location Where:=xlLocationAsObject, Name:="Total Var Graphs"
            With ActiveChart
                .SetSourceData UserRangeOrders
                .HasTitle = True
                .ChartType = xlColumnClustered
                .ChartTitle.Characters.Text = "Total Order Variance ($M)"
                .Axes(xlValue).HasMajorGridlines = False
                .Legend.Position = xlLegendPositionBottom
                .Legend.Border.LineStyle = x1None
                .Axes(xlValue).TickLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .ApplyDataLabels xlDataLabelsShowValue
                .SeriesCollection(1).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .SeriesCollection(2).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .PlotArea.Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, _
                    Degree:=0.331364919508659
                .PlotArea.Fill.Visible = True
                .PlotArea.Fill.ForeColor.SchemeColor = 40
                .Axes(xlCategory).TickLabelPosition = xlLow
                .Deselect
            End With
        
        Sheets("Total vs Op Plan").Activate
        
            Charts.Add
            ActiveChart.Location Where:=xlLocationAsObject, Name:="Total Var Graphs"
            With ActiveChart
                .SetSourceData UserRangeRevenue
                .HasTitle = True
                .ChartType = xlColumnClustered
                .ChartTitle.Characters.Text = "Total Revenue Variance ($M)"
                .Axes(xlValue).HasMajorGridlines = False
                .Legend.Position = xlLegendPositionBottom
                .Legend.Border.LineStyle = x1None
                .Axes(xlValue).TickLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .ApplyDataLabels xlDataLabelsShowValue
                .SeriesCollection(1).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .SeriesCollection(2).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .PlotArea.Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, _
                    Degree:=0.331364919508659
                .PlotArea.Fill.Visible = True
                .PlotArea.Fill.ForeColor.SchemeColor = 40
                .Axes(xlCategory).TickLabelPosition = xlLow
                .Deselect
            End With
        
        Sheets("Total vs Op Plan").Activate
    
            Charts.Add
            ActiveChart.Location Where:=xlLocationAsObject, Name:="Total Var Graphs"
            With ActiveChart
                .SetSourceData UserRangeProfit
                .HasTitle = True
                .ChartType = xlColumnClustered
                .ChartTitle.Characters.Text = "Total Profit Variance ($M)"
                .Axes(xlValue).HasMajorGridlines = False
                .Legend.Position = xlLegendPositionBottom
                .Legend.Border.LineStyle = x1None
                .Axes(xlValue).TickLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .ApplyDataLabels xlDataLabelsShowValue
                .SeriesCollection(1).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .SeriesCollection(2).DataLabels.NumberFormat = "#,##0.0,,_);[Red](#,##0.0,,)"
                .PlotArea.Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, _
                    Degree:=0.331364919508659
                .PlotArea.Fill.Visible = True
                .PlotArea.Fill.ForeColor.SchemeColor = 40
                .Axes(xlCategory).TickLabelPosition = xlLow
                .Deselect
            End With
        
            W = ActiveChart.Parent.Width - 5
            H = ActiveChart.Parent.Height - 5
            TopPosition = 1
            LeftPosition = 10
            For Each ChtObj In ActiveSheet.ChartObjects
                ChtObj.Width = W
                ChtObj.Height = H
                ChtObj.Top = TopPosition
                ChtObj.Left = LeftPosition
                TopPosition = TopPosition + ChtObj.Height
            Next ChtObj
        Application.ScreenUpdating = True
            
    End Sub
    Thanks in advance for the help.
    Last edited by fecurtis; 05-12-2008 at 03:00 PM.

  5. #5
    Forum Contributor
    Join Date
    04-30-2008
    Posts
    105
    I realized that if I try running the macro on a sheet that does not contain the data it uses, that the macro will return a blank graph or a strange graph.

    I'm not quite sure how to fix it. I've tried changing the macros so that they first go to the spreadsheet that contains the data before it does anything else but it still doesn't work. I'm completely stumped.

  6. #6
    Forum Contributor
    Join Date
    04-30-2008
    Posts
    105
    I figured it out. Nevermind.

+ 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