Excel 宏仅在逐步执行或代码中有一条特征线时起作用,但在正常运行时不起作用
我的宏创建图表时出现问题,当我逐步执行时,它工作正常,但当我正常运行Excel时,它会重新启动。试过各种各样的东西,什么都不管用。它最初是其他宏的一部分,但我将它隔离到另一个子宏,认为它可能会有所帮助,但隔离后它仍然崩溃 你们知道这是什么原因吗 EDIT1:没有错误信息,excel只是重新启动 EDIT2:问题似乎是由这部分代码引起的:Excel 宏仅在逐步执行或代码中有一条特征线时起作用,但在正常运行时不起作用,excel,vba,excel-charts,Excel,Vba,Excel Charts,我的宏创建图表时出现问题,当我逐步执行时,它工作正常,但当我正常运行Excel时,它会重新启动。试过各种各样的东西,什么都不管用。它最初是其他宏的一部分,但我将它隔离到另一个子宏,认为它可能会有所帮助,但隔离后它仍然崩溃 你们知道这是什么原因吗 EDIT1:没有错误信息,excel只是重新启动 EDIT2:问题似乎是由这部分代码引起的: .SetElement (msoElementChartTitleAboveChart) .ChartTitle.Text = "Liczba
.SetElement (msoElementChartTitleAboveChart)
.ChartTitle.Text = "Liczba Dni Promocji - Wykres"
.ChartTitle.Font.Bold = True
.ChartTitle.Font.Size = 16
代码如下:
Sub StworzWykres()
Application.ScreenUpdating = False
'Application.PrintCommunication = True
Dim ws As Worksheet
Dim pt As PivotTable
Dim chrt As Chart
Dim myRng As Range
Dim i As Integer
Dim j As Integer
Set ws = ThisWorkbook.Sheets("Raport_LiczbaDniPromocji")
Set pt = ws.PivotTables("LDP_Tab1")
'delete existing charts
Dim shp As Shape
For Each shp In ws.Shapes
shp.Delete
Next shp
Set shp = Nothing
'ask if make a chart
Application.ScreenUpdating = True
If MsgBox("Czy chcesz utworzyć wykres Liczby Dni Promocji?", vbYesNo, "Wykres") = vbNo Then
Exit Sub
End If
Application.ScreenUpdating = False
'adding the chart
'Set chrt = ws.Shapes.AddChart.Chart
Set myRng = ws.Range(Cells(19 + pt.RowRange.Rows.Count, 4), Cells(18 + 2 * (pt.RowRange.Rows.Count), 6))
myRng.Select
Set chrt = ws.Shapes.AddChart.Chart
'Set chrt = ws.ChartObjects.Add.Chart
'chrt.Activate
With chrt
'For j = .SeriesCollection.Count To 1 Step -1
' .SeriesCollection(j).Delete
'Next j
'.SetSourceData Source:=myRng, PlotBy:=xlColumns
.ChartType = xl3DColumnClustered
'.SetSourceData Source:=myRng, PlotBy:=xlColumns
.Parent.Name = "Wykres_LDP"
.DepthPercent = 400
.PlotArea.Format.ThreeD.RotationX = 0
.PlotArea.Format.ThreeD.RotationY = 110
.RightAngleAxes = True
.ChartArea.Left = ws.Range(Cells(1, 1), Cells(1, 6)).Width + 1 'ws.Cells(20 + pt.RowRange.Rows.Count, 8).Left - (ws.Columns(7).ColumnWidth / 1.25)
.ChartArea.Top = ws.Cells(18 + pt.RowRange.Rows.Count, 8).Top
.ChartArea.Height = ws.Range(Cells(19 + pt.RowRange.Rows.Count, 8), Cells(47 + pt.RowRange.Rows.Count, 8)).Height
.ChartArea.Width = 1000
.Parent.Placement = xlMove
.ChartColor = 10
.ChartGroups(1).GapWidth = 150
.SetElement (msoElementLegendBottom)
.Legend.Font.Size = 12
.Legend.Font.Bold = True
.SetElement (msoElementChartTitleAboveChart)
.ChartTitle.Text = "Liczba Dni Promocji - Wykres"
.ChartTitle.Font.Bold = True
.ChartTitle.Font.Size = 16
With .Axes(xlCategory, xlPrimary)
.TickLabels.Orientation = 60
'.TickLabels.Font.Bold = True
'.TickLabels.Font.Size = 11
End With
For j = .SeriesCollection.Count To 1 Step -1
.SeriesCollection(j).HasDataLabels = True
.SeriesCollection(j).DataLabels.Orientation = xlUpward
.SeriesCollection(j).DataLabels.Font.Bold = True
.SeriesCollection(j).DataLabels.Font.Size = 10
If j = 1 Then
.SeriesCollection(j).DataLabels.Font.ColorIndex = 32
Else
.SeriesCollection(j).DataLabels.Font.ColorIndex = 46
End If
Next j
'.SetSourceData Source:=myRng, PlotBy:=xlColumns
End With
'clear variables
Set ws = Nothing
Set pt = Nothing
Set myRng = Nothing
i = Empty
j = Empty
'Application.PrintCommunication = False
Application.ScreenUpdating = True
End Sub
除去
并插入
.HasTitle = True
在同一位置。移除
.SetElement (msoElementChartTitleAboveChart)
并插入
.HasTitle = True
在同一个地方。嗨,怎么了?那条线。。。你必须提供更多的细节…但没有错误,Excel只是重新启动。经过进一步挖掘,发现是
.SetElement(msoElementChartTitleAboveChart).ChartTitle.Text=“Liczba Dni Promocji-Wykres.ChartTitle.Font.Bold=True.ChartTitle.Font.Size=16
导致了问题。您好,错误是什么?那条线。。。你必须提供更多的细节…但没有错误,Excel只是重新启动。经过进一步挖掘,结果证明是.SetElement(msoElementCharttTitleAbovechart).ChartTitle.Text=“Liczba Dni Promocji-Wykres.ChartTitle.Font.Bold=True.ChartTitle.Font.Size=16
导致了问题。是的,它成功了!非常感谢你。你能告诉我为什么.SetElement
失败吗?我几乎可以肯定我在其他图表中使用过它,而且效果很好。谁知道,Excel的工作方式很神秘。我所知道的是,.SetElement
语法是在我学会用老方法做事(比如.hasttle=True
或.Visible=True
等)多年后添加的,而且旧方法似乎比.SetElement
更可靠,实际上更容易解释。是的,它奏效了!非常感谢你。你能告诉我为什么.SetElement
失败吗?我几乎可以肯定我在其他图表中使用过它,而且效果很好。谁知道,Excel的工作方式很神秘。我所知道的是,.SetElement
语法是在我学会用老方法做事(比如.hasttle=True
或.Visible=True
等)多年后添加的,而且旧方法似乎比.SetElement
更可靠,实际上更容易解释。