VBA图表中的动态行和列

时间:2015-04-17 05:56:42

标签: vba dynamic charts

所以我试着写一些可以查看“sheet2”的VBA,并确定它需要在折线图中制作多少行和列

我很难根据变量声明.SetSourceData的范围我需要它从单元格(1,4)开始并转到Cell(LastRow,LastColumn)。当我尝试我得到一个错误

'方法2 昏暗的LastRow一样长 Dim LastColumn As Long Dim rng2 As Range Dim ShName As String 使用ActiveSheet

LastRow = ThisWorkbook.Sheets("Sheet2").Range("G" & .Rows.Count).End(xlUp).Row
LastColumn = ThisWorkbook.Sheets("Sheet2").Cells(1, Columns.Count).End(xlToLeft).Column
Set rng2 = ThisWorkbook.Sheets("Sheet2").Range(.Sheets("Sheet2").Cells(2, 4), .Sheets("Sheet2").Cells(LastRow, LastColumn))

'ThisWorkbook.Sheets(“Sheet2”)。范围(“R2”)。值= rng2

ShName = .Name
End With

Charts.Add

With ActiveChart
    .ChartType = xlLine
    .SetSourceData Source:=rng2
End With

1 个答案:

答案 0 :(得分:0)

我尝试使用你的代码并在行中获得错误:

Set rng2 = ThisWorkbook.Sheets("Sheet2").Range(.Sheets("Sheet2").Cells(2, 4), .Sheets("Sheet2").Cells(LastRow, LastColumn))

尝试在ThisWorkbook之前添加.Sheets

Set rng2 = ThisWorkbook.Sheets("Sheet2").Range(ThisWorkbook.Sheets("Sheet2").Cells(2, 4), ThisWorkbook.Sheets("Sheet2").Cells(LastRow, LastColumn))

或删除Sheets之前的点:

Set rng2 = ThisWorkbook.Sheets("Sheet2").Range(Sheets("Sheet2").Cells(2, 4), Sheets("Sheet2").Cells(LastRow, LastColumn))