Vba 宏在我调用它时工作正常,但在作为按钮的.OnAction时不工作
有谁能帮我解决将宏正确分配给按钮的问题吗 这是我要指定的宏:Vba 宏在我调用它时工作正常,但在作为按钮的.OnAction时不工作,vba,excel,Vba,Excel,有谁能帮我解决将宏正确分配给按钮的问题吗 这是我要指定的宏: 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 =
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)
那么
将此宏分配给具有以下内容的同一按钮:
.OnAction = "'testMacro""arg1"",""arg2"", arg3'"
工作非常好,所以我认为这是可能的。顶部的
程序在哪里?在模块中?@Pᴇʜ,是的,在同一模块中,您确定单击了正确的形状吗?在测试添加新形状之前删除旧形状,或在新工作簿中测试此代码。我无法重现那个问题。我复制了你的.OnAction
,它按预期工作。@Pᴇʜ它确实起作用了,当我将代码复制到新工作簿时,现在它工作得很好。谢谢。
.OnAction = "'testMacro""arg1"",""arg2"", arg3'"