我正在向图表添加趋势线,并希望在将数据标签捕获为字符串之前更改数据标签中的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保护时,有没有办法更改Trendline.DataLabel的NumberFormat:= True?