好的,我要做的是拥有一个带有组合框的用户表单,该组合框显示每个工作表作为选项。我明白了。我希望能够在我的用户表单上选择一张工作表并从所述工作表上的某些范围绘制图表。我希望这是有道理的....这是我到目前为止所得到的。它适用于第一个组合框选择,但是一旦我更换了工作表并尝试从不同的工作表中绘制图表,我就会收到错误,"方法'值'对象'系列'失败。任何帮助将不胜感激。在此先感谢=)
Private Sub ComboBox1_Change()
End Sub
Private Sub CommandButton1_Click()
If ComboBox1.Text = "Select A Client" Then
MsgBox "You Must Select a Name to Continue.", , "ERROR:Select A Name"
ComboBox1.SetFocus
Exit Sub
End If
Dim MyChart As Chart
Dim ChartData As Range
Dim chartIndex As Integer
Dim ChartName As String
chartIndex = ComboBox1.ListIndex
Select Case chartIndex
Case 1
Set ChartData = ActiveSheet.Range("L4:L103")
ChartName = ActiveSheet.Range("A1")
End Select
Application.ScreenUpdating = False
Set MyChart = ActiveSheet.Shapes.AddChart(xlXYScatterLines).Chart
MyChart.SeriesCollection.NewSeries
MyChart.SeriesCollection(1).Name = ChartName
MyChart.SeriesCollection(1).Values = ChartData
MyChart.SeriesCollection(1).XValues = ActiveSheet.Range("J4:J103")
Dim imageName As String
imageName = Application.DefaultFilePath & Application.PathSeparator & "TempChart.gif"
MyChart.Export Filename:=imageName
ActiveSheet.ChartObjects(1).Delete
Application.ScreenUpdating = True
UserForm1.Image1.Picture = LoadPicture(imageName)
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
Dim sht As Worksheet, txt As String
For Each sht In ActiveWorkbook.Sheets
Me.ComboBox1.AddItem sht.Name
Next sht
End Sub
答案 0 :(得分:1)
您定义ChartData
的唯一时间是ComboBox
ListIndex
是否为1.任何其他选择和ChartData
对象将保持Nothing
。因此,当您尝试将图表值设置为ChartData
时,您将收到错误。
很难说出你要对Ranges
做些什么。如果您要在Range
中选择的任何工作表上定义ComboBox
,那么您的代码将需要如下所示:
Dim ws as Worksheet
Set ws = ThisWorkbook.Worksheets(ComboBox1.Text)
并删除Select
阻止。
您的所有代码都引用了ActiveSheet
,这可能不是您想要的,因此您需要使用此新变量ws
替换对该引用的所有引用。