2012-10-03 88 views
1

我試圖從5個ActiveX文本框中創建一個圖表,並將其放入我的工作表中。首先用戶輸入值,然後按下生成圖表的按鈕。Excel中的VBA:從ActiveX文本框創建圖表

這是我的方法,但它不起作用。它說,像「對象需要」

Private Sub CommandButton1_Click() 

Dim arKurse(4) As Double 

arKurse(0) = Conver.ToDouble(TextBox1.Text) 
arKurse(1) = Conver.ToDouble(TextBox2.Text) 
arKurse(2) = Conver.ToDouble(TextBox3.Text) 
arKurse(3) = Conver.ToDouble(TextBox4.Text) 
arKurse(4) = Conver.ToDouble(TextBox5.Text) 


Dim oChtObj As ChartObject 
Set oChtObj = ActiveSheet.ChartObjects.Add(Left:=445, Width:=385, Top:=10, Height:=245) 

With oChtObj.Chart 
    .SeriesCollection.NewSeries 
    .SeriesCollection.Values = arKurse 
    .SeriesCollection.XValues = Array("1", "2", "3", "4", "5") 

    .ChartType = xlLine 
    .HasLegend = False 
    .HasTitle = True 
    .ChartTitle.Caption = "Chart" 

End With 

End Sub 
+0

它在哪裏說的錯誤?什麼是Conver.toDouble?你是在什麼地方定義的?爲什麼不使用'CDbl()'函數? – enderland

+0

是你的文本框實際命名爲'TextBox1','TextBox2'等? – enderland

回答

2

有不工作的幾件事情

  1. Conver.ToDouble應該CDbl,除非你有一個很好的理由使用自定義的方法?
  2. TextBox.Text應該是一個用戶窗體上的Textbox.value,也最好限定該窗體的父窗體的位置。 (在這種情況下Userform1但可能是工作表Sheet1如果文本框是工作表)
  3. 需要定義的SeriesCollection

下面的更新工作有望

Option Explicit 

Sub SetupUserformToTest() 
UserForm1.Show 
UserForm1.TextBox1.Value = 1 
UserForm1.TextBox2.Value = 2 
UserForm1.TextBox3.Value = 3 
UserForm1.TextBox4.Value = 4 
UserForm1.TextBox5.Value = 1 
CommandButton1_Click 
End Sub 

Private Sub CommandButton1_Click() 

Dim arKurse(4) As Double 

arKurse(0) = CDbl(UserForm1.TextBox1.Value) 
arKurse(1) = CDbl(UserForm1.TextBox2.Value) 
arKurse(2) = CDbl(UserForm1.TextBox3.Value) 
arKurse(3) = CDbl(UserForm1.TextBox4.Value) 
arKurse(4) = CDbl(UserForm1.TextBox5.Value) 

Dim oChtObj As ChartObject 
Set oChtObj = ActiveSheet.ChartObjects.Add(Left:=445, Width:=385, Top:=10, Height:=245) 

With oChtObj.Chart 
    .SeriesCollection.NewSeries 
    'Assumed series 1 
    .SeriesCollection(1).Values = arKurse 
    .SeriesCollection(1).XValues = Array("1", "2", "3", "4", "5") 

    .ChartType = xlLine 
    .HasLegend = False 
    .HasTitle = True 
    .ChartTitle.Caption = "Chart" 
End With 

End Sub 
+0

謝謝,幫助我...但現在它說「類型不匹配」...我認爲該系列不能採取任何方式? – Mexxer

+0

等待不知道...得到它......忘了切換UserForm1到Sheet1 – Mexxer

+2

只是稍作修改 - 您應該可以使用TextBox.Text或TextBox.Value。它們是同一件事,但在VBA中,價值在控件中更爲常見。 – user3357963