vba中图形绘制的范围选择

时间:2014-10-08 10:13:44

标签: excel vba excel-vba

您好我正在尝试使用vba宏自动生成图形的过程。下面的代码给出了错误的Object变量或With block变量未设置。我已经在下面显示了我的代码,它在xrange线上打破了。我很感激人们的帮助。

' Chart update
Dim xrange As Range, yrange As Range
Dim co As Object
Left = 1500: Top = 250: Width = 280: Height = 210

'Draw the Charts
' Set range
xrange = ActiveSheet.Range(Cells(2, 3), Cells(17, 3))
yrange = ActiveSheet.Range(Cells(2, 4), Cells(17, 4))
'
Set co = Sheets("Sheet1").ChartObjects.Add(Left, Top, Width, Height)
co.Name = "Chart1"
co.Chart.ChartWizard Source:=yrange, _
Gallery:=xlXYScatterSmooth, PlotBy:=xlColumns, Format:=1, _
CategoryLabels:=0, SeriesLabels:=0, HasLegend:=False, Title:="Chart Title", _
CategoryTitle:=xlabel, ValueTitle:=ylabel
co.Chart.SeriesCollection(1).XValues = xrange

1 个答案:

答案 0 :(得分:0)

我看到了几个错误。

  1. LeftWidth是保留字。您不应该将它们用作变量。您可以使用LftWdt代替它们。

  2. 您的Cells对象不是完全限定的。我建议使用对象而不是ActiveSheet

  3. 例如(注意细胞前的DOT)

    Sub Sample()
        Dim xrange As Range, yrange As Range
        Dim co As Object
        Dim ws As Worksheet
    
        '~~> Change this to the relevant sheet
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
        lft = 1500: Top = 250: wdt = 280: Height = 210
    
        With ws
            xrange = .Range(.Cells(2, 3), .Cells(17, 3))
            yrange = .Range(.Cells(2, 4), .Cells(17, 4))
    
            '
            '~~> Rest of the code
            '
        End With
    End Sub