2016-09-30 77 views
0

問:使用輸入框價值VBA創建圖表標題

您將需要創建三個變量如下:

「rangeData」:這是存儲當前選定的範圍。 「titleChart」:這是存儲圖表標題 「nameSheet」:這是存儲表名稱

您將使用InputBox函數兩次以圖表工作表一個標題和一個名稱,並使用授予稱號爲圖表和工作表的名稱。

到目前爲止,我一直在堅持充分利用InputBox值到新的圖表標題:

代碼我到目前爲止有:

Dim RangeData As Integer 
Dim rng As Range 

Dim ctInputbox As String 
Dim CT As ChartTitle 

Dim nsnInputbox As String 
Dim nsn As Worksheet 

'Change Data source to currentley selected cells 
Set rng = Selection 

'Add a chart onto the active sheet and select the chart 
ActiveSheet.Shapes.AddChart.Select 

'Chart type is Clustered Column chart 
ActiveChart.ChartType = xlColumnClustered 

'Assign a chart title: 

'instead of using the content of cell H13 as the chart title, ask 
'the user for the title of the chart and set the title 

'Add user data to create a chart title  
ctInputbox = InputBox("Please enter a chart title", "Chart Title Name") 
ActiveChart.HasTitle = True 

With ActiveChart.ChartObjects 
    Set CT = ctInputbox 
End With 

End Sub 

回答

1

Set CT = ...需要在右側而ChartTitle對象InputBox()函數返回String

另外還有ActiveChartChartObjects「返回表示單個嵌入式圖表(ChartObject對象)或表單中所有嵌入式圖表(ChartObjects對象)的集合的對象。」,這是不是你想要設置圖表標題是什麼?

相反,你要設置有關Chart對象的ChartTitle屬性檢索ChartTitle對象的Text財產。

,我寧願Application.InputBox()方法,而不是VBA InputBox()功能,前者可讓您通過其Type參數強制用戶輸入類型: 將其設置爲2將迫使一個字符串輸入。

最後分配SelectionRange對象之前,總是檢查前真的一個Range對象

所以在這裏你可以去用什麼:

'Change Data source to currentley selected cells 
If TypeName(Selection) <> "Range" Then Exit Sub '<--| exit if selection is not a 'Range (i.e. it might be a 'Chart') 
Set rng = Selection '<--| now you can do that safely 

'Add a chart onto the active sheet and select the chart 
ActiveSheet.Shapes.AddChart.Select 

With ActiveChart 
    'Chart type is Clustered Column chart 
    .ChartType = xlColumnClustered 

    'Add user data to create a chart title 
    .HasTitle = True 
    .ChartTitle.Text = Application.InputBox("Please enter a chart title", "Chart Title Name", Type:=2) 
End With 
0

與yourobject.chart

.SetElement msoElementChartTitleAboveChart 

    .ChartTitle.Caption = Title$ 

結尾