VBA code n ot running properly in context of macro

lipanook

New Member
Joined
Nov 29, 2012
Messages
23
I have a big Excel add-in that I've made using VBA. It has lots of different subroutines and functions.

Part of it creates a set of charts, formats them and then positions them in a new worksheet so that they can be exported as a PDF.

When I run this subroutine, the charts do not position themselves correctly. But if I create a new macro exactly the same code in it that is used in the big Add-in, it works perfectly. So the code isn't working in the context of my add-in for some reason.

Has anyone got any ideas as to the kind of thing that could cause this problem, i.e. what I should look out for when debugging?

Thanks in advance.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Code:
Sub positionCharts()
'This sub-routine positions the polar plots for exporting as PDFs


    Dim numberOfCharts As Integer
    Dim chartLeft As Integer
    Dim pageNumber As Integer
    Dim chartTop As Integer
    Dim Sheet As Object
    Dim i As Integer
    Dim multipleCheck As Integer
    
    
    For Each Sheet In ThisWorkbook.Sheets
    
        If Right(Sheet.Name, 5) = "plots" Then
            numberOfCharts = ActiveSheet.ChartObjects.Count
            
            For i = 1 To numberOfCharts
                
                ''''''''This is a pretty dirty way of selecting the chart
                ActiveSheet.ChartObjects("Chart " & i).Activate
                
                'calculate left position (0 from left if odd number, 222 from left if even)
                If i Mod 2 = 1 Then
                    chartLeft = 0
                Else
                    chartLeft = 222
                End If
                        
                'determine verticle position depending on page number and number that chart is on that page (6 charts per page)
                
                'pageNumber
                pageNumber = Int((i - 1) / 6)
                
                'determine number that chart is on that page
                multipleCheck = i - 1 Mod 6
            
                If multipleCheck = 0 Then
                    chartTop = 750 * pageNumber
                End If
            
                If multipleCheck = 1 Then
                    chartTop = 750 * pageNumber
                End If
            
                If multipleCheck = 2 Then
                    chartTop = 750 * pageNumber + 222
                End If
            
                If multipleCheck = 3 Then
                    chartTop = 750 * pageNumber + 222
                End If
            
                If multipleCheck = 4 Then
                    chartTop = 750 * pageNumber + 444
                End If
            
                If multipleCheck = 5 Then
                    chartTop = 750 * pageNumber + 444
                End If
            
                'Apply the chart position
                With ActiveChart
                    .Parent.Top = chartTop
                    .Parent.Left = chartLeft
                    
                End With
            
            Next i
        
        End If
        
    Next Sheet




End Sub
 
Upvote 0
In an Add-In you need to use ActiveWorkbook rather than ThisWorkbook, which refers to the workbook that contains the running code.

As an aside you shouldn't use the names of VBA objects as variables, ie Sheet.
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,962
Members
449,200
Latest member
indiansth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top