Putting graph in user named sheet

LukeMorris

New Member
Joined
Jan 23, 2006
Messages
13
I want a macro that inserts a graph into a sheet that the user defines the name of.

So far I have this code:

Code:
Sub GraphIt()
'
' GraphIt Macro
' Macro recorded 26/01/2006 by Luke Morris
'

'
'Add New Sheet
    Sheets.Add

'Make sure the name is valid
    On Error Resume Next

'Get the new name
     ActiveSheet.Name = InputBox("Name for graph?")

'Keep asking for name if name is invalid
    Do Until Err.Number = 0
        Err.Clear
        ActiveSheet.Name = InputBox("Try Again!" _
          & vbCrLf & "Invalid Name or Name Already Exists" _
          & vbCrLf & "Please name the New Sheet")
    Loop
    On Error GoTo 0

'Remember Sheet
    Dim CurrentSheetName As String
    CurrentSheetName = ActiveSheet.Name

'Make Graph
    Sheets("Log Sheet").Select
    Range("B10:B39,J10:J39").Select
    Range("J10").Activate
    Charts.Add
    ActiveChart.ChartType = xlLine
    ActiveChart.SetSourceData Source:=Sheets("Log Sheet").Range("B10:B39,J10:J39" _
        ), PlotBy:=xlColumns
    ActiveChart.Location Where:=xlLocationAsNewSheet, Name:=("CurrentSheetName")
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "Cash Flow"
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Balance"
    End With
   
    Sheets("Log Sheet").Select
    Range("A1").Activate


End Sub

It works okay, up to the point of the graph being put in and then it makes a sheet called "CurrentSheetName"

Because this line is incorrect I guess:
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:=("CurrentSheetName")

But I'm not sure of the correct code, or whether this is even the right way to go about this.

Can anyone help me with this?

Thanks!

Luke
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You seem to be using "CurrentSheetName" as a string instead of a variable ie. don't use quote marks. Also I don't think you need this line at all..
Code:
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:=("CurrentSheetName")
omit these lines...
Code:
Sheets("Log Sheet").Select
    Range("B10:B39,J10:J39").Select
    Range("J10").Activate
and insert this in its' place...
Code:
Sheets("CurrentSheetName").Select
Untested but seems reasonable. Dave
 
Upvote 0
Thanks for the help

I deleted the
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:=("CurrentSheetName") line, and put Sheets("CurrentSheetName").Select inplace of the lines you said.

Run time error '9'

Subscript out of range

When debugged it highlights

"Sheets("CurrentSheetName").Select"

:( Any ideas?
 
Upvote 0
Luke

Why are you using quotes around CurrentSheetName?

If you are then VBA assumes you are referring to a sheet with that
name rather than the sheet that the user has entered.
 
Upvote 0
I just realised that and took it out!
Thank you!

Onwards to next problem!
It's making the sheet....and then a chart (for the graph)

So my next change to code is this.....I don't want the user to make a sheet, I want them to make a CHART.....'cause then hopefully the graph will actually be inserted into it!

What changes to my code do I have to make to make this work?

Code at current is:

Code:
Sub GraphIt()
'
' GraphIt Macro
' Macro recorded 26/01/2006 by Luke Morris
'

'
'Add New Sheet
    Sheets.Add

'Make sure the name is valid
    On Error Resume Next

'Get the new name
     ActiveSheet.Name = InputBox("Name for graph?")

'Keep asking for name if name is invalid
    Do Until Err.Number = 0
        Err.Clear
        ActiveSheet.Name = InputBox("Try Again!" _
          & vbCrLf & "Invalid Name or Name Already Exists" _
          & vbCrLf & "Please name the New Sheet")
    Loop
    On Error GoTo 0

'Remember Sheet
    Dim CurrentSheetName As String
    CurrentSheetName = ActiveSheet.Name

'Make Graph
    Sheets(CurrentSheetName).Select
    Charts.Add
    ActiveChart.ChartType = xlLine
    ActiveChart.SetSourceData Source:=Sheets("Log Sheet").Range("B10:B39,J10:J39" _
        ), PlotBy:=xlColumns
        
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "Cash Flow"
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Balance"
    End With
   
    Sheets("Log Sheet").Select
    Range("A1").Activate


End Sub
 
Upvote 0
No line just a chart?

Sorry, I don't understand what you mean.

Let me try to confirm.

I run the macro, it prompts me for a sheet name. It makes the sheet "test1" and also makes the correct graph in a sheet called "Chart23"
 
Upvote 0
That's interesting... Chart23 is probably the 23 XL chart object that you have made. If CurrentSheetName is a string it should have the quotes but by placing the quotes XL thinks the sheet name is CurrentSheetName which doesn't exist hence the error... what a conundrum. Perhaps Norie has an answer. Dave
 
Upvote 0

Forum statistics

Threads
1,215,048
Messages
6,122,862
Members
449,097
Latest member
dbomb1414

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