gpt4 book ai didi

VBA 代码错误 : Chart Title in Excel

转载 作者:行者123 更新时间:2023-12-04 21:17:17 24 4
gpt4 key购买 nike

我有一个 sas 代码,它在 excel 文件中写入文本(让调用是“sas 文本”),然后 VBA 在 excel 中创建图形并复制标题中的“sas 文本”。
当“sas 文本”长度较小时,VBA 工作正常,但只要“SAS 文本”长度较长,VBA 就会给出运行时错误 - “VBA,对象错误的方法“文本”。

我的代码是:

Sub FormatChart()
Let Title = Sheets("Sheet1").Cells(2, 1)
Let Title1 = Sheets("Sheet1").Cells(2, 2)
Let Title2 = Sheets("Sheet1").Cells(2, 3)
Let Title3 = Sheets("Sheet1").Cells(2, 4)
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(1).Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).ReversePlotOrder = True
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartTitle.Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartTitle.Text = Title & Title3 & Chr(10) & Title1 & "to " & Title2 & ": People with 25 or more visits" & Chr(10) & "Source: www.xxxxxxxxxxx.xxx.xxx"
With ActiveChart.ChartTitle.Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 8
End With
With ActiveChart.Axes(xlCategory).TickLabels.Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 7
End With
With ActiveChart.Axes(xlValue).TickLabels.Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 7
End With
With ActiveChart.PlotArea.Interior
.ColorIndex = 2
.PatternColorIndex = 1
.Pattern = xlSolid
End With
Range("S4").Select
ActiveWindow.SmallScroll Down:=48
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Legend.Select
Selection.delete
ActiveWindow.SmallScroll Down:=45
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.PlotArea.Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveWindow.SmallScroll Down:=-45
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlValue).MajorGridlines.Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveWindow.SmallScroll Down:=-54
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Select
ActiveChart.ChartArea.Width = 500
ActiveChart.ChartArea.Height = 1000
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlValue).MajorGridlines.Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlValue).Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlValue).MajorGridlines.Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MaximumScale = 100
ActiveChart.Axes(xlValue).MajorUnit = 20
ActiveChart.Axes(xlValue).MajorUnit = 10
With ActiveChart.SeriesCollection(1)
.Interior.Color = RGB(0, 51, 153)
End With
ActiveSheet.ChartObjects("Chart 1").Activate
Dim c As Chart
Dim s As Series
Dim iPoint As Long
Dim nPoint As Long

Set c = ActiveChart
Set s = c.SeriesCollection(1)

nPoint = s.Points.Count
For iPoint = 1 To nPoint
If s.XValues(iPoint) = "MINNESOTA STATE AVERAGE " Then
s.Points(iPoint).Interior.Color = RGB(80, 116, 77)
End If
If s.XValues(iPoint) = "NATIONAL AVERAGE " Then
s.Points(iPoint).Interior.Color = RGB(80, 116, 77)
End If
Next iPoint

ActiveSheet.ChartObjects("Chart 1").Activate
Selection.Cut
Sheets("Chart1").Select
ActiveChart.Paste
End Sub

在调试时,它会突出显示这行代码
"        ActiveChart.ChartTitle.Text = Title & Title3 & Chr(10) & Title1 & "to " & Title2 & ": People with 25 or more visits" & Chr(10) & "Source: www.xxxxxxxxxxx.xxx.xxx"
"

非常感谢!

最佳答案

ChartTitle.Text 限制为 255 个字符。

当 x = 256 时,通过实验确认以下失败。

Sub Macro2()
'
Dim x as long
With ActiveChart
.HasTitle = True
For x = 1 To 500
.ChartTitle.Text = Characters(x)
Next
End With
End Sub
Function Characters(x As long)
dim charCount as long
For charCount = 1 To x
Characters = Characters & "."
Next
End Function

我的建议是截断你的标题 ChartTitle.Text = Left("your built string",255)

关于VBA 代码错误 : Chart Title in Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13182413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com