VBA Personal Macro Contents Page

toffee_girl

New Member
Joined
Sep 16, 2016
Messages
3
Hi Everyone,

Hoping somebody can help me before I throw my lap top through the window...!

I attempted to write a macro which was supposed to add a contents page with links to all other worksheets and then on each of those worksheets insert a shape with a link back to the contents page. After about 100 attempts the macro worked fine - until I tried to save it as a personal macro. Now when I run it the initial part works and it creates the contents page and adds the shapes but when I try to click back on the shape I get an error message to say "Cannot run the Macro 'Module2.ContentsSelect'. The Macro may not be available in this workbook or all macros may be disabled.

Any help would be much appreciated !

Code:
 Sub Contents()

Dim wbBook As Workbook
Dim wsActive As Worksheet
Dim wsSheet As Worksheet


Dim lnRow As Long
Dim lnPages As Long
Dim lnCount As Long


Set wbBook = ActiveWorkbook


With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
End With


On Error Resume Next


With wbBook
    .Worksheets("Contents").Delete
    .Worksheets.Add Before:=.Worksheets(1)
End With
On Error GoTo 0


Set wsActive = wbBook.ActiveSheet
With wsActive
    .Name = "Contents"
    With .Range("B2")
        .Value = VBA.Array("Table of Contents")
        .Font.Bold = True
    End With
End With


lnRow = 3
lnCount = 2


For Each wsSheet In wbBook.Worksheets
    If wsSheet.Name <> wsActive.Name Then
        wsSheet.Activate
        With wsActive
            .Hyperlinks.Add .Cells(lnRow, 2), "", _
            SubAddress:="'" & wsSheet.Name & "'!A1", _
            TextToDisplay:=wsSheet.Name
            
        End With
        lnRow = lnRow + 1
        lnCount = lnCount + 1
    End If
Next wsSheet


wsActive.Activate
wsActive.Columns("B").EntireColumn.AutoFit


ActiveWindow.DisplayGridlines = False


Dim ws As Worksheet


For Each ws In ActiveWorkbook.Worksheets


Set s = ws.Shapes.AddShape(51, 300, 10, 100, 30)
With s
    .Fill.ForeColor.RGB = RGB(204, 193, 218)
    .TextFrame.Characters.Text = "Back to Contents"
    .OnAction = "Module2.ContentsSelect"
        
    
End With


Next


With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
End With
End Sub

Sub ContentsSelect()
        
        Worksheets("Contents").Activate
    
        Range("B2").Select
    
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Welcome to the board.

Where is the macro ContentsSelect? If it's in Personal, the line should be

Code:
.OnAction = "personal.xls!ContentsSelect"

or

Code:
.OnAction = "personal.xls[COLOR="#FF0000"]m[/COLOR]!ContentsSelect"

or

Code:
.OnAction = "personal.xls[COLOR="#FF0000"]b[/COLOR]!ContentsSelect"

..., depending.

That said, why not put the code in the workbook of interest, so it's not dependent on Personal?
 
Last edited:
Upvote 0
This worked perfectly, thank-you very much!! The reason its a personal macro is because I get a lot of system generated spreadsheets with multiple pages and I wanted to be able to add a contents page to each one to make improve ease of navigation. Have I misunderstood the use of a personal macro?

Thanks again! :)
 
Upvote 0

Forum statistics

Threads
1,215,968
Messages
6,127,983
Members
449,414
Latest member
sameri

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