2012-11-01 88 views
1

我有一個sas代碼,在excel文件中寫入文本(讓我們調用是「sas文本」),然後VBA在excel中創建圖形並在標題中複製「sas文本」 。 當「sas text」長度很短時,VBA工作正常,但是每當「SAS text」長度較長時,VBA會給出運行時間錯誤 - 「VBA,方法」文本「的對象錯誤。VBA代碼錯誤:Excel中的圖表標題

我的代碼是:

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" 
" 

感謝很多

回答

4

的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)

+4

+1或'左$( 「你的內置字符串」,252) 「...」' –

2

在Excel中一些東西被限制爲255個字符,由於是(舊式)Pascal用第一個字節的長度計算字符串。

要解決此問題,您可以添加一個TextBox並使用您的標題填充該問題。請注意,交互時在時間限制內仍有255個字符,但您可以建立起來。

下將無法正常工作:

Sub WillNotWork() 
    Const LongString As String = _ 
     "Pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164..." 
    Dim title As TextFrame 

    Set title = ActiveSheet.Shapes("Text Box 1").TextFrame 
    title.Characters.Text = LongString 'fails silently 
End Sub 

但這會:

Sub Works() 
    Const LongString As String = _ 
     "Pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164..." 
    Dim title As TextFrame 

    Set title = ActiveSheet.Shapes("Text Box 1").TextFrame 
    title.Characters.Text = "" 
    For i = 0 To Len(LongString) \ 255 
     title.Characters(i * 255 + 1).Insert Mid$(LongString, 255 * i + 1, 255) 
    Next 
End Sub 
+0

+1,這是一個相當不錯的工作,如果擁有超長標題是必要的,但它可能需要大量的遊戲才能正確顯示。 –

+0

文本框也沒有工作。我相信我必須去限制角色的數量。感謝A.韋伯和丹尼爾的時間。 :) – Nupur

+0

@Nupur一個文本框可以工作,因爲它可以容納超過255個字符。但是,您仍然必須尊重與其交互的255個字符限制。查看我的編輯答案的代碼示例。 –