•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 425,986 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,659 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Views: 9242 | Replies: 4
![]() |
•
•
Join Date: Nov 2004
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
I'm creating a simple little macro that draws a chart from some data in Excel. The frustration comes because every time my macro draws a chart, it sequences the number. The first chart is "chart 1" the second is "chart 2" even if "chart 1" no longer exists.
I know there's a simple answer to this problem but I can't find it, hence the frustration. Please could someone tell me how to take charge of the chart identification before I chew another corner off my keyboard!
Steve
I know there's a simple answer to this problem but I can't find it, hence the frustration. Please could someone tell me how to take charge of the chart identification before I chew another corner off my keyboard!
Steve
•
•
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 191
Reputation:
Rep Power: 5
Solved Threads: 1
How are you adding the chart? If you are using the Charts.Add method then you can use this to get a reference to the new chart object.
And so on.
By the way your CD's were put in the mail today.
dim chartNew as Chart set chartNew=ActiveWorkBook.Charts.Add chartNew.ChartTitle.Characters.Text = "This is a New Chart"
And so on.
By the way your CD's were put in the mail today.
Mark Nemtsas
Time and Billing Software - Time Tracking Software - Roller Shutters - Roller Blinds -
Baby Books
Time and Billing Software - Time Tracking Software - Roller Shutters - Roller Blinds -
Baby Books
•
•
Join Date: Nov 2004
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Hello again Mark, I'm adding my chart as per below. This is a simplified version of what I'm doing (with your code added). The point is to graph a couple of columns and only select the cells with data in. This works fine but each time it runs it creates a new chart on top of the previous whereas I want to replace the previous chart with the new one. I thought I'd simply be able to put a line of code at the start to delete "chart 1" but that doesn't work as the next time around it's called "chart 2"!
I've added your code as per below but there's something wrong with how I've done it as it won't add the title.
Assuming I can get it to add the title will I now be able to add a line to delete "This is a new chart" at the start of the sub?
Cheers steve
Sub graphtest()
Dim chartNew As Chart
Do
rowpointer = rowpointer + 1
locate = Cells(rowpointer, 1).Value
Loop Until locate = 0
c = rowpointer - 1
Set myrange = Worksheets("Sheet1").Range(Cells(1, 1), Cells(c, 2))
Set chartNew = ActiveWorkbook.Charts.Add
chartNew.ChartType = xlXYScatterSmooth
chartNew.SetSourceData Source:=myrange, PlotBy:=xlColumns
chartNew.Location Where:=xlLocationAsObject, Name:="Sheet1"
With chartNew
.HasTitle = True
.ChartTitle.Characters.Text = "This is a New Chart"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Shrinkage %"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "M/C %"
End With
End Sub
I've added your code as per below but there's something wrong with how I've done it as it won't add the title.
Assuming I can get it to add the title will I now be able to add a line to delete "This is a new chart" at the start of the sub?
Cheers steve
Sub graphtest()
Dim chartNew As Chart
Do
rowpointer = rowpointer + 1
locate = Cells(rowpointer, 1).Value
Loop Until locate = 0
c = rowpointer - 1
Set myrange = Worksheets("Sheet1").Range(Cells(1, 1), Cells(c, 2))
Set chartNew = ActiveWorkbook.Charts.Add
chartNew.ChartType = xlXYScatterSmooth
chartNew.SetSourceData Source:=myrange, PlotBy:=xlColumns
chartNew.Location Where:=xlLocationAsObject, Name:="Sheet1"
With chartNew
.HasTitle = True
.ChartTitle.Characters.Text = "This is a New Chart"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Shrinkage %"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "M/C %"
End With
End Sub
•
•
•
•
Originally Posted by mnemtsas
How are you adding the chart? If you are using the Charts.Add method then you can use this to get a reference to the new chart object.
dim chartNew as Chart set chartNew=ActiveWorkBook.Charts.Add chartNew.ChartTitle.Characters.Text = "This is a New Chart"
And so on.
By the way your CD's were put in the mail today.
•
•
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 191
Reputation:
Rep Power: 5
Solved Threads: 1
Hi Steve,
Try:
Try:
Sub graphtest()
Dim chartNew As Chart
Dim chartsTemp As ChartObjects
On Error GoTo errorHandler 'put in error handling to handle if no charts exist
'
'Deletes the last created chart in the worksheet
'
Set chartsTemp = ActiveSheet.ChartObjects
If chartsTemp.Count > 0 Then
chartsTemp(chartsTemp.Count).Delete
End If
Do
rowpointer = rowpointer + 1
locate = Cells(rowpointer, 1).Value
Loop Until locate = 0
c = rowpointer - 1
Set myrange = Worksheets("Sheet1").Range(Cells(1, 1), Cells(c, 2))
Set chartNew = ActiveWorkbook.Charts.Add
chartNew.ChartType = xlXYScatterSmooth
chartNew.SetSourceData Source:=myrange, PlotBy:=xlColumns
chartNew.Location Where:=xlLocationAsObject, Name:="Sheet1"
'
'FOr some reason VBA loses the handle on the chart when you change the location so to workaround this
'just re-reference it here
'
Set chartNew = Application.ActiveChart
With Application.ActiveChart
.HasTitle = True
'
'Sorry I got the chart title stuff wrong :-(
'
.ChartTitle.Text = "This is a New Chart"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Shrinkage %"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "M/C %"
End With
Exit Sub
errorHandler:
If Err.Number = 91 Then 'handles the case when there is no chart
Err.Clear
Resume Next
Else
MsgBox "Unhandled Error" & vbCrLf & Err.Number & " " & Err.Description
End If
End Sub
Mark Nemtsas
Time and Billing Software - Time Tracking Software - Roller Shutters - Roller Blinds -
Baby Books
Time and Billing Software - Time Tracking Software - Roller Shutters - Roller Blinds -
Baby Books
![]() |
•
•
•
•
•
•
•
•
DaniWeb Visual Basic 4 / 5 / 6 Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Using VBA with Access (was: Please Help!!!) (Visual Basic 4 / 5 / 6)
- Microsoft OLEDB Chart Control Version 6 question (Visual Basic 4 / 5 / 6)
- generating excel chart in VBA (Visual Basic 4 / 5 / 6)
- I need help using VBA for Word Macros (Windows NT / 2000 / XP / 2003)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Real Estate Program
- Next Thread: How to write a program to register a chart object in excel?


Linear Mode