Excel 解决运行时424错误;ActiveChart.SetSourceData
下午好 初学者使用2002年编写的遗留宏。宏只需编译来自不同工作簿的数据,并使用图表创建一个新文件。将电脑从Excel2003升级到现在导致了一些bug,这些bug大部分已经解决。最后剩下的问题之一是构建图表的子系统。初始错误(1004)发生在线路上Excel 解决运行时424错误;ActiveChart.SetSourceData,excel,vba,runtime,Excel,Vba,Runtime,下午好 初学者使用2002年编写的遗留宏。宏只需编译来自不同工作簿的数据,并使用图表创建一个新文件。将电脑从Excel2003升级到现在导致了一些bug,这些bug大部分已经解决。最后剩下的问题之一是构建图表的子系统。初始错误(1004)发生在线路上 ActiveChart.SetSourceData Source:=Sheets("Data").Range(_ PlotCells), PlotBy:=XlColumns 经过一些研究,我在这里找到了一些信息。。。https
ActiveChart.SetSourceData Source:=Sheets("Data").Range(_
PlotCells), PlotBy:=XlColumns
经过一些研究,我在这里找到了一些信息。。。https://social.msdn.microsoft.com/Forums/en-US/ce290e29-634b-4383-9bb5-fddda8db3974/excel-macro-not-able-to-setsourcedata-for-chart?forum=isvvba
这让我把那句话改成了
ActiveChart.SetSourceData Source:=PlotCells, PlotBy:=xlColumns
1004错误不再发生,但现在已被424错误取代。不确定原因是什么,因为我认为我正确地标注了所有部件的尺寸,并将数据从一个子部件传递到下一个子部件。调试时,PlotCells包含一个值。全文如下
Sub Build_Chart(ByVal File_Path As String, ByVal NumFreq As String, ByVal NumTemp As String, ByRef FreqV() As String)
'
' Macro6 Macro
' Macro recorded 1/11/02 by Edward P. Smith
'
' Making the Plot
Dim X_Column, FCell, PlotCells, ii, tt, Freq, Cell, W_Book
X_Column = "J"
FCell = NumFreq + 3
PlotCells = X_Column & FCell & ","
Workbooks.Open Filename:=File_Path
W_Book = ActiveWorkbook.Name
'Add the Chart
Charts.Add
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
For ii = 1 To NumFreq
'Get the values to Plot
For tt = 1 To NumTemp - 2
If Not tt = NumTemp - 2 Then
PlotCells = PlotCells & X_Column & (FCell + tt * (NumFreq + 1)) & ","
Else
PlotCells = PlotCells & X_Column & (FCell + tt * (NumFreq + 1))
End If
Next tt
If ii = 1 Then
ActiveChart.SetSourceData Source:=PlotCells, PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
Else
Sheets("Data").Select
Range(PlotCells).Select
Selection.Copy
Sheets("Chart1").Select
ActiveChart.PlotArea.Select
ActiveChart.SeriesCollection.Paste Rowcol:=xlColumns, SeriesLabels:=False, _
CategoryLabels:=False, Replace:=False, NewSeries:=True
End If
FCell = FCell + 1
PlotCells = X_Column & FCell & ","
Next ii
' Title the Chart and Axis
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "PPM Change"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Temperature"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "PPM"
End With
' Name the plot series
For ii = 1 To NumFreq
ActiveChart.SeriesCollection(ii).Name = "=""" & MyRound(FreqV(ii - 1), 2) & " GHz"""
Next ii
' Set the X-Axis values to the temperatures
For ii = 1 To NumFreq
ActiveChart.SeriesCollection(ii).XValues = _
"=Data!R" & ((NumFreq + 1) * NumTemp) + 4 & "C1:R" & (((NumFreq + 1) * NumTemp) + 3) + (NumTemp - 1) & "C1"
Next ii
' Set the Scale to our default values
ActiveChart.Axes(xlValue).Select
With ActiveChart.Axes(xlValue)
.MinimumScale = -1000
.MaximumScale = 2000
.MinorUnitIsAuto = True
.MajorUnitIsAuto = True
.Crosses = xlCustom
.CrossesAt = -1000
.ReversePlotOrder = False
.ScaleType = xlLinear
End With
ActiveChart.Axes(xlCategory).Select
With ActiveChart.Axes(xlCategory)
.MinimumScaleIsAuto = True
.MaximumScaleIsAuto = True
.MinorUnitIsAuto = True
.MajorUnitIsAuto = True
.Crosses = xlCustom
.CrossesAt = -1000
.ReversePlotOrder = False
.ScaleType = xlLinear
End With
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
Selection.Interior.ColorIndex = xlNone
Windows(W_Book).Activate
ActiveWorkbook.Save
'MyDelay 20
ActiveWindow.Close
End Sub
谢谢你的帮助。谢谢。对象“\u工作表”失败”的初始1004“方法“范围”错误可能是由于PlotCells
的长度超过了范围可接受的最大字符数(255)造成的
正在抛出424“Object required”错误,因为Source
需要的是范围对象而不是字符串
如果
Len(
PlotCells)
>255,则必须使用Union()
来构建范围。PlotCells
看起来像一个字符串,而不是范围。您应该Debug.Print
查看它是什么。顺便说一句,该代码可能需要重写,它有一些不好的做法。也许开始吧,我不怀疑!目前只是想在没有完全重写的情况下过日子。谢谢你提供的信息!