宏在我调用时可以正常工作,但在作为按钮的.OnAction时不起作用

时间:2018-08-20 12:08:34

标签: excel vba excel-vba

有人可以帮助我解决为按钮正确分配宏的问题吗?

这是我要分配的宏:

Sub TOP(table_sheet_name As String, sheet_name As String, chart_name As String, number As Integer)

Dim s As Series
Dim i As Integer
Dim sheetChart As Worksheet
Dim sheetTable As Worksheet

Application.ScreenUpdating = False

Set sheetTable = ThisWorkbook.Sheets(table_sheet_name)
Set sheetChart = ThisWorkbook.Sheets(sheet_name)

'delete existing series

'For Each s In sheetChart.ChartObjects(chart_name).SeriesCollection
sheetChart.ChartObjects(chart_name).Activate
For Each s In ActiveChart.SeriesCollection
    s.Delete
Next s

'add proper number of new series
'With sheetChart.ChartObjects.Chart(chart_name)
sheetChart.ChartObjects(chart_name).Activate
With ActiveChart
For i = 1 To number
    .SeriesCollection.NewSeries
    sheetTable.Select
    With .FullSeriesCollection(i)
        .Name = sheetTable.Cells(11 + i, 1).Value
        .XValues = sheetTable.Range(Cells(11, 2), Cells(11, 25))
        .Values = sheetTable.Range(Cells(11 + i, 2), Cells(11 + i, 25))
        .MarkerStyle = 8
        .MarkerSize = 5
        .Format.Line.Visible = msoTrue
        .Format.Line.Weight = 2.25
        .Format.Line.Style = msoLineSingle
        .Format.Shadow.Type = msoShadow21
    End With
    sheetChart.Select
Next i
End With

Application.ScreenUpdating = True
End Sub

这是我的一部分代码,我尝试制作一个形状按钮:

Set rngB = ws.Cells(1, 5)
Set R1 = ws.Shapes.AddShape(msoShapeRectangle, rngB.Left, rngB.TOP, rngB.Width, rngB.Height)
With R1
    .Line.Weight = 0.5
    .Line.Style = msoLineSingle
    .Line.ForeColor.RGB = RGB(0, 102, 204)
    .Name = "TOP5"
    .TextFrame.Characters.Text = "TOP 5"
    '.OnAction = "'" & ThisWorkbook.Name & "'!'TOP""Liczba_promocji"",""Wykres sezonowości"",""Wykres Sezonowości"",5'"
    .OnAction = "'TOP""Liczba_promocji"",""Wykres sezonowości"",""Wykres Sezonowości"",5'"
    .TextFrame.HorizontalAlignment = xlHAlignCenter
    .TextFrame.VerticalAlignment = xlVAlignCenter
    .TextFrame.Characters.Font.Size = 12
    .TextFrame.Characters.Font.Bold = True
    .TextFrame.Characters.Font.Color = 32
    .Fill.ForeColor.RGB = RGB(0, 102, 204)
    .Fill.BackColor.RGB = RGB(0, 0, 255)
    .Fill.TwoColorGradient msoGradientHorizontal, 1
    .Locked = True
End With

我不知道可能是什么问题,我已经寻找解决方案已有一段时间了。 我收到的错误是:“ ...该宏可能在此工作簿(工作表)中不可用,或者可能禁用了所有宏...”

我还需要注意,当我在代码中调用过程时,它运行良好:

Call TOP("Liczba_promocji", "Wykres sezonowości", "Wykres Sezonowości", 5)

1 个答案:

答案 0 :(得分:0)

好吧

TextBlock

此宏通过以下方式分配给了相同的按钮:

Sub testMacro(arg1 As String, arg2 As String, arg3 As Integer)
MsgBox arg1 & ", " & arg2 & ", " & arg3
End Sub

做的很好,所以我认为有可能。