初始化和调用变量

Initialize and call variables

我正在尝试使用 Visual Basic 在 excel 中创建一个宏。当我试图调用我的变量时,我 运行 遇到了问题。我以前从未用 Visual Basic 编写过代码,所以我不确定我是否正确地初始化和调用了所有变量。

这是我正在使用的代码:

Sub Graphing2()
'
' Graphing2 Macro
'

'
    Dim a, b, y, x As Long
    a = 176
    b = 126
    y = 3
    x = 0
    Do While x < 225
    ActiveSheet.ChartObjects("Chart 1").Activate
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.FullSeriesCollection(2).Name = "=""Unit y"""
    ActiveChart.FullSeriesCollection(2).XValues = "=Sheet1!$B$a:$B$b"
    ActiveChart.FullSeriesCollection(2).Values = "=Sheet1!$E$a:$E$b"
    x = x + 1
    y = y + 1
    a = a + 67
    b = b + 67
    Loop
End Sub

任何有关如何初始化然后调用变量的帮助都会有所帮助。 谢谢

未经测试,但这里有一些提示:

Sub Graphing2()
    'must specify type for each variable....
    Dim a As Long, b As Long, y As Long, x As Long
    Dim cht As Chart 'declare a chart variable
    
    a = 176
    b = 126
    y = 3
    
    Set cht = ActiveSheet.ChartObjects("Chart 1").Chart
    
    'user a For Next loop for fixed sequences
    For x = 0 To 244
        
        With cht.SeriesCollection.NewSerie 'NewSeries returns the added series
            'concatenate your variables
            .Name = "Unit " & y
            .XValues = "=Sheet1!$B$" & a & ":$B$" & b
            .Values = "=Sheet1!$E$" & a & ":$E$" & b
        End With
        
        y = y + 1
        a = a + 67
        b = b + 67
    Next x
End Sub