我正在向图表添加趋势线,并希望在将数据标签捕获为字符串之前更改数据标签中的 NumberFormat。最初我是这样做的:
Sub AddLine()
ActiveChart.SeriesCollection(1).Trendlines.Add Type:=xlLinear, _
intercept:=0, DisplayEquation:=1, DisplayRSquared:=1, Name:="Linear"
End Sub
Sub Eqn()
Dim t As Trendline
Dim eqn As String
Set t = ActiveChart.SeriesCollection(1).Trendlines(1)
t.DataLabel.NumberFormat = "0.0000E+00"
Application.Wait (Now + TimeValue("0:00:01"))
eqn = ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.text
'other stuff
End Sub
Sub Run()
Call AddLine
Call Eqn
End Sub
在我开始保护我的床单之前,这一切都很好。我在 VBA 中专门保护了我的工作表,因此我可以使用 UserInterFaceOnly:=True。尽管如此,当我尝试更改 NumberFormat 时,VBA 会引发错误。我试图通过将代码更改为以下内容来解决此问题:
Sub AddLine()
With ActiveChart.SeriesCollection(1)
.Trendlines.Add Type:=xlExponential, Forward:=0, Backward:=0, _
DisplayEquation:=1, DisplayRSquared:=1, Name:="Exponential"
.Trendlines(1).DataLabel.NumberFormat = "0.0000E+00"
End With
End Sub
Sub Eqn()
Dim eqn As String
Application.Wait (Now + TimeValue("0:00:01"))
eqn = ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.text
'other stuff
End Sub
Sub Run()
Call AddLine
Call Eqn
End Sub
当它在没有保护的情况下运行时,这也可以正常工作,但是当我在它受到保护时设置 NumberFormat 时再次引发错误。
当工作表受 UserInterFaceOnly:=True 保护时,有没有办法更改 Trendline.DataLabel 的 NumberFormat?