graham.reeds
graham.reeds

Reputation: 16486

Running a macro in excel

I don't think this is technically a macro but I don't know what else to call it:

Users want to print individual sections from a report on a sheet. These sections are just named ranges.

Some points:

I created a series of buttons down the edge of the sheet:

        ' in a loop    
        With ActiveSheet.Buttons.Add(rngCurrent.Left + 2, rngCurrent.Top + 1, rngCurrent.Width - 2, rngCurrent.Height - 1)
            .Caption = "Print"
            .OnAction = "PrintRange"
            .Font.Size = 7
            .Name = CStr(oSite.SiteID)
        End With

However when I click on the button it gives "Cannot run the macro 'filename.xls!PrintRange".

The PrintRange function is in the sheet shtPage while the loop is in a module called modPage.

Why can't I call the function I need and how can I make it work?

Upvotes: 1

Views: 1003

Answers (2)

Stewbob
Stewbob

Reputation: 16899

Even with your PrintRange sub declared as Public you still need to refer to it more specifically so that Excel can find it, because you have put it in the code section of a worksheet instead of a module.

Change:

.OnAction = "PrintRange"

to

.OnAction = "shtPage.PrintRange"

and it will work just fine.

A caution: If you have renamed your worksheet on the page tab to 'shtPage', but in the VBA project explorer, Excel still refers to the worksheet as 'Sheet1(shtPage)', you will need to use the 'Sheet1' name that Excel recognizes, not the 'shtPage' name that appears on the page tab.

Upvotes: 5

James Eichele
James Eichele

Reputation: 119164

Did you make the PrintRange macro public? It needs to be defined as follows:

Public Sub PrintRange
    '// ...'
End Sub

Upvotes: 1

Related Questions