Search : in
By :

Macro code for making a graph in excel

Last answer on Oct 15, 2008 2:19:31 pm BST Shandor, on Oct 8, 2008 9:18:32 pm BST 
 Report this message to moderators

Hello,

I would like to create a graph from a macro. Here is what I have got so far.

Charts.Add
ActiveChart.ChartType = xlXYScatterLinesNoMarkers
ActiveChart.SetSourceData Source:=Sheets(sheetname).Range("AF1:" & lastcell2, "BN1:" & lastcell), PlotBy:=xlColumns ****<------ something is wrong here

ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Pressures"
Sheets(1).Move after:=Sheets(2)

ActiveChart.SeriesCollection(1).XValues = Sheets(sheetname).Range(("AL2"), lastcell3)
ActiveChart.SeriesCollection(2).XValues = Sheets(sheetname).Range(("AL2"), lastcell3)
ActiveChart.SeriesCollection(3).XValues = Sheets(sheetname).Range(("AL2"), lastcell3)
ActiveChart.SeriesCollection(4).XValues = Sheets(sheetname).Range(("AL2"), lastcell3)
ActiveChart.SeriesCollection(5).XValues = Sheets(sheetname).Range(("AL2"), lastcell3)

With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Pressure During Test"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Hours"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Pressure (psi)"
End With
ActiveChart.HasLegend = True
ActiveChart.Legend.Select
Selection.Position = xlBottom



I am managining wth the rest of this stuff but I marked a line of code with a ***. This line is giving me trouble. I only want to graph the data in column AF-AH and in BN and BO. But it seems to be selecting all the columns in between also.

What is the correct syntax to do this?

One more question, is there a way to get the x axis to be the data in column AL without it being part of the original selection and hwo do I just right the xvalues line only once instead of for each and every column of data?

Configuration: Windows XP
Firefox 3.0.3

Best answers for « macro code for making a graph in excel » in :
[Excel] – Running Macro Automatically Show[Excel] – Running Macro Automatically Issue Solution Issue I have a macro in excel which needs to be run twice a day and I don’t even want to open the excel sheet. How can I make this process automatic? If I can convert the macro...
How to convert Excel into PDF? ShowHow to convert Excel into PDF? Here is a small tips about how to convert your excel files into PDF for your presentation. Step 1 PDF995 is software that gets installed on your computer which enables you to print any sources of document to...
Adding a VBA CommandButton with its respective the code ShowAdding a VBA CommandButton with its respective the code Paste these two sub in a general module (Module1 for example). Sub CreateButton() Dim Obj As Object Dim Code As String Sheets("Sheet1").Select 'create button ...

1

Ivan-hoe, on Oct 9, 2008 6:54:53 am BST
  • +2

Hello Shandor,
what is the type of your variables LastCell and LastCell2 ?
If they are set as range, you should write

ActiveChart.SetSourceData Source:= _ 
Sheets(sheetname).Range("AF1:" & LastCell2.Address, "BN1:" & LastCell.Address)

Ivan

Reply to Ivan-hoe

2

afrog4u2, on Oct 9, 2008 11:09:05 am BST

I agree that knowing the various variable types will help, but without knowing that and assuming that they are "STRING", that part of the code worked for me. What failed was when assigning the XValues.

ActiveChart.SeriesCollection(1).XValues = Sheets(sheetname).Range(("AL2"), lastcell3)

The error was "Run-time error '1004': Unable to set the XValues property of the Series Class.

How did you create this script? Did you record it when creating the chart originally?

Do you have any sample data you can give?

Reply to afrog4u2

3

Shandor, on Oct 9, 2008 1:43:03 pm BST

I'm sorry here is the code of a working graphing subroutine, not the one I am trying to get to work.
Private Sub GraphTempButton_Click()
' tempgraph2 Macro
' Macro recorded 9/29/2008 by Robert
'

Application.ScreenUpdating = False
FinalRow = Range("A65536").End(xlUp).Row
lastcell = Cells(FinalRow, "G").Address
lastcell2 = Cells(FinalRow, "AL").Address
sheetname = ActiveSheet.Name

Charts.Add
ActiveChart.ChartType = xlXYScatterLinesNoMarkers
ActiveChart.SetSourceData Source:=Sheets(sheetname).Range(("A1"), lastcell), PlotBy:=xlColumns

ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Temperatures"
Sheets(1).Move after:=Sheets(2)

ActiveChart.SeriesCollection(1).XValues = Sheets(sheetname).Range(("AL2"), lastcell2)
ActiveChart.SeriesCollection(2).XValues = Sheets(sheetname).Range(("AL2"), lastcell2)
ActiveChart.SeriesCollection(3).XValues = Sheets(sheetname).Range(("AL2"), lastcell2)
ActiveChart.SeriesCollection(4).XValues = Sheets(sheetname).Range(("AL2"), lastcell2)
ActiveChart.SeriesCollection(5).XValues = Sheets(sheetname).Range(("AL2"), lastcell2)
ActiveChart.SeriesCollection(6).XValues = Sheets(sheetname).Range(("AL2"), lastcell2)

With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Temperature During Test"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Hours"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Temp (C)"
End With
ActiveChart.HasLegend = True
ActiveChart.Legend.Select
Selection.Position = xlBottom

'now some text
Call PlaceGraphText
Application.ScreenUpdating = True
End Sub


Now you can see how I defined lastcell.

This works when all the data is in adjacent columns. I am trying to get something to work that does not have data in adjacent columns.

Further, I was hoping there was a single command that applies x axis data to all the y axis data. You can see in this example that I assign it 6 times.

Very little of this was from recording macros. I am brute forcing my way through macro programming by dong google searches for example macro code and trying to apply it. Some times I get stuck (like now). Ivan hoe usually shows me how simple it is and that I am being a dumbass. :)

Reply to Shandor

4

Shandor, on Oct 9, 2008 1:47:35 pm BST

Ivan hoe,

looks like that didnt work. Probably because lastcell is not defined as a range, but is instead a cell.

I get a 'missing object' error. Any ideas?

Reply to Shandor

5

Shandor, on Oct 9, 2008 4:25:33 pm BST

I should also add that I dont want the first column in my selection to appear as the x-axis. However if I can get the syntax of what I want to be working then I can just add the correct column as the first column and thos becomes less of a problem.

Thanks

Reply to Shandor

6

Shandor, on Oct 10, 2008 7:31:52 pm BST

Sorry about the bump. But I am still stuck. Does anyone at least have a good on-line resource I can use to figure out my graph issue?

Reply to Shandor

7

 shandor, on Oct 15, 2008 2:19:31 pm BST
  • +1

Yeesh, sorry had to bump again, but I am still not finding this answer around on my own. If no one can answer this, does anyone have a good online resource for details on building graphs in excel with macros?

Reply to shandor