How to call variables in excel macro?

doingcho2

New Member
Joined
Jul 21, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have never coded in visual basic before. I am trying to create a macro that will graph a ton of data. I made a quick program using the "record" function, then tried to mess with it to get the rest of the data graphed.
This is what I have come up with so far:
VBA Code:
Sub Graphing2()
'
' Graphing2 Macro
'

'
    Let a = 176
    Let b = 126
    Let y = 3
    Let 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

I'm getting an error where I try and move the X and Y values down a couple of lines to the next set of data. Am I not calling the variables correctly? Im not sure whats wrong.
Any information is appreciated.

Thanks.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Remove the word "Let" from the lines....should simply be
a = 176
b = 126
y = 3
x = 0
And you need to declare ALL of your variables.
Put this
Dim a as long, b as long, x as long, y as long
after the
' Graphing2 Macro line
 
Upvote 0
Remove the word "Let" from the lines....should simply be
a = 176
b = 126
y = 3
x = 0
And you need to declare ALL of your variables.
Put this
Dim a as long, b as long, x as long, y as long
after the
' Graphing2 Macro line

Hello,

Thank you for the reply, I declared them all with Dim and set them as "Long" variables.
But I am still getting the error on line 17 when I am trying to move the X and Y values to graph different data.
How do I initialize the variables? Is this correct? And I am calling them correctly on lines 17 and 18?
VBA Code:
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
 
Upvote 0
I don't think that's caused by a problem with the variables, which line of code is triggering the error?
 
Upvote 0
I don't think that's caused by a problem with the variables, which line of code is triggering the error?

ActiveChart.FullSeriesCollection(2).XValues = "=Sheet1!$B&a:$B&b"
I am getting the error on this line. I am guessing its from the "a" and "b". Am I calling them properly?
 
Upvote 0
The way you declared the variables is incorrect
Dim a, b, y, x As Long
Note the difference in the way I posted the line....
Dim a as long, b as long, x as long, y as long
 
Upvote 0
Ah, it is connected with the variables.

This shouldn't error but I'm not sure it'll do what you want.
[code-vba]
ActiveChart.FullSeriesCollection(2).Name = "=""Unit " & y & """"
ActiveChart.FullSeriesCollection(2).XValues = "=Sheet1!$B$" & a & ":$B$" & b
ActiveChart.FullSeriesCollection(2).Values = "=Sheet1!$E$" & a & ":$E$" & b
[/code]
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top