Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA图表->;范围选择使用";“细胞”;在图表中_Excel_Charts_Runtime Error_Office 2007_Vba - Fatal编程技术网

Excel VBA图表->;范围选择使用";“细胞”;在图表中

Excel VBA图表->;范围选择使用";“细胞”;在图表中,excel,charts,runtime-error,office-2007,vba,Excel,Charts,Runtime Error,Office 2007,Vba,我正在尝试修改一个现有的VBA代码(Excel),它可以生成图表,并使其更加灵活 我知道以下代码的作用基本相同: Range(Cells(12, 2), Cells(15, 2)).Select 与以下内容大致相同: Range("B12:B15").Select 我的目标是创建一个图表,它表示灵活的行数 因此,我更改了现有代码: ActiveChart.SetSourceData Source:=Sheets("Log-Data").Range("B12:B200"), P

我正在尝试修改一个现有的VBA代码(Excel),它可以生成图表,并使其更加灵活

我知道以下代码的作用基本相同:

Range(Cells(12, 2), Cells(15, 2)).Select    
与以下内容大致相同:

Range("B12:B15").Select    
我的目标是创建一个图表,它表示灵活的行数

因此,我更改了现有代码:

ActiveChart.SetSourceData Source:=Sheets("Log-Data").Range("B12:B200"), PlotBy:=xlColumns    

现在,每当我执行代码时,我都会收到:

运行时错误“1004”:应用程序定义的错误或对象定义的错误

LastRow
变量不是问题所在:如果将其替换为200,则结果相同

我做错了什么

干杯


Peter

LastRow=ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count)。Row


LastRow=ActiveSheet.UsedRange.Rows.Count

都是一样的

就实际错误而言,代码运行时似乎没有活动的图表对象。所以
ActiveChart.
返回一个对象错误

试试这个:

Dim LastRow As Integer
LastRow = ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Sheets("Log-Data").Range(Cells(12, 2), Cells(LastRow, 2)), PlotBy:=xlColumns

试试下面的内容,让我知道

Dim LastRow As Integer
LastRow = ActiveSheet.UsedRange.Rows.Count

With Sheets("Log-Data")
Set Myrange = .Range(Cells(12, 2), Cells(LastRow, 2))
End With

ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Myrange, PlotBy:=xlColumns

我尝试了以下方法,效果很好(在单词前添加句点)


使用
sheetname.Range(单元格(a、b)、单元格(a2、b2))
时,应始终使用工作表引用限定每个
Cells()
:否则,
Cells()
将始终引用活动工作表,这可能不是您想要的。
Dim LastRow As Integer
LastRow = ActiveSheet.UsedRange.Rows.Count

With Sheets("Log-Data")
Set Myrange = .Range(Cells(12, 2), Cells(LastRow, 2))
End With

ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Myrange, PlotBy:=xlColumns
Dim LastRow As Integer
LastRow = ActiveSheet.UsedRange.Rows.Count

With Sheets("Log-Data")
Set Myrange = .Range(.Cells(12, 2), .Cells(LastRow, 2))
End With

ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Myrange, PlotBy:=xlColumns