PC Review


Reply
Thread Tools Rate Thread

Chart Position in VB6

 
 
=?Utf-8?B?amR1Yjc2NQ==?=
Guest
Posts: n/a
 
      1st Mar 2007
I'm working in TestPartner, an automation tool that uses VB6, and I either
need to reposition a chart, or change the default position. I've been
searching to find a solution, but I can't seem to get it to work...

I found this link
http://msdn.microsoft.com/newsgroups...=en-us&m=1&p=1
and I think I need to use this code:

With ActiveSheet.Shapes("Chart 1")
.Left = ActiveCell.Left
.Top = ActiveCell.Top
End With

I don't know how to use the code above in my code below...

Private Sub GenerateGraph(objChart As Excel.Chart)
objChart.ChartType = xlPie
objChart.SetSourceData source:=objSheet.Range("B4:C5"), PlotBy _
:=xlColumns
objChart.location Where:=xlLocationAsObject, name:="Graph"
End Sub

Any help is greatly appreciated!
 
Reply With Quote
 
 
 
 
Jon Peltier
Guest
Posts: n/a
 
      1st Mar 2007
You need to change the position of the chart object, which is the shape that
contains the chart:

Private Sub GenerateGraph(objChart As Excel.Chart)
objChart.ChartType = xlPie
objChart.SetSourceData source:=objSheet.Range("B4:C5"), PlotBy _
:=xlColumns
objChart.location Where:=xlLocationAsObject, name:="Graph"
With ActiveCell ' or With Worksheets("Graph").Range("C12")
objChart.Parent.Left = .Left
objChart.Parent.Top = .Top
End With
End Sub

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"jdub765" <(E-Mail Removed)> wrote in message
news:E8AFBD78-54CB-4A8C-949B-(E-Mail Removed)...
> I'm working in TestPartner, an automation tool that uses VB6, and I either
> need to reposition a chart, or change the default position. I've been
> searching to find a solution, but I can't seem to get it to work...
>
> I found this link:
> http://msdn.microsoft.com/newsgroups...=en-us&m=1&p=1
> and I think I need to use this code:
>
> With ActiveSheet.Shapes("Chart 1")
> .Left = ActiveCell.Left
> .Top = ActiveCell.Top
> End With
>
> I don't know how to use the code above in my code below...
>
> Private Sub GenerateGraph(objChart As Excel.Chart)
> objChart.ChartType = xlPie
> objChart.SetSourceData source:=objSheet.Range("B4:C5"), PlotBy _
> :=xlColumns
> objChart.location Where:=xlLocationAsObject, name:="Graph"
> End Sub
>
> Any help is greatly appreciated!



 
Reply With Quote
 
=?Utf-8?B?amR1Yjc2NQ==?=
Guest
Posts: n/a
 
      1st Mar 2007
Thanks for the reply!
I tried the code you provided:
With ActiveCell
objChart.Parent.Left = .Left
objChart.Parent.Top = .Top
End With
and while there were no compiling errors, I get runtime errors.

When I use ActiveCell, I get Error 91 - Object variable or With block
variable not set, and when I use Worksheets("Graph").Range("C12"), I get
Error -2147221080 - Method Parent of object _Chart failed.
Both fail on objChart.Parent.Left = .Left.

I don't really understand how VB handles excel objects so I'm not sure how
to fix it. I've been guess & checking for about an hour. Do you have any
links that I can read so I can get a better understanding of what's going on?
All the searches I've done come up with links that don't really apply to my
problem...

Thanks a million for the help!


"Jon Peltier" wrote:

> You need to change the position of the chart object, which is the shape that
> contains the chart:
>
> Private Sub GenerateGraph(objChart As Excel.Chart)
> objChart.ChartType = xlPie
> objChart.SetSourceData source:=objSheet.Range("B4:C5"), PlotBy _
> :=xlColumns
> objChart.location Where:=xlLocationAsObject, name:="Graph"
> With ActiveCell ' or With Worksheets("Graph").Range("C12")
> objChart.Parent.Left = .Left
> objChart.Parent.Top = .Top
> End With
> End Sub
>
> - Jon
> -------
> Jon Peltier, Microsoft Excel MVP
> Tutorials and Custom Solutions
> http://PeltierTech.com
> _______
>
>
> "jdub765" <(E-Mail Removed)> wrote in message
> news:E8AFBD78-54CB-4A8C-949B-(E-Mail Removed)...
> > I'm working in TestPartner, an automation tool that uses VB6, and I either
> > need to reposition a chart, or change the default position. I've been
> > searching to find a solution, but I can't seem to get it to work...
> >
> > I found this link:
> > http://msdn.microsoft.com/newsgroups...=en-us&m=1&p=1
> > and I think I need to use this code:
> >
> > With ActiveSheet.Shapes("Chart 1")
> > .Left = ActiveCell.Left
> > .Top = ActiveCell.Top
> > End With
> >
> > I don't know how to use the code above in my code below...
> >
> > Private Sub GenerateGraph(objChart As Excel.Chart)
> > objChart.ChartType = xlPie
> > objChart.SetSourceData source:=objSheet.Range("B4:C5"), PlotBy _
> > :=xlColumns
> > objChart.location Where:=xlLocationAsObject, name:="Graph"
> > End Sub
> >
> > Any help is greatly appreciated!

>
>
>

 
Reply With Quote
 
=?Utf-8?B?amR1Yjc2NQ==?=
Guest
Posts: n/a
 
      1st Mar 2007
omg. I found an easy solution.

I found the "Record Macro" function in Excel. lol
I clicked on "Relative Reference", moved the chart, and then looked at the
resulting code.
Then I changed the code a little to fit what I needed and I came up with this:

With ActiveSheet.Shapes("Chart 1")
.IncrementLeft -25.5
.IncrementTop 50.25
End With

And it works!! WOOHOO! Thanks for your help Jon Peltier! It helped me
understand what was going on a little more so that I could edit the code
 
Reply With Quote
 
Jon Peltier
Guest
Posts: n/a
 
      2nd Mar 2007
You need to prefix ActiveCell with the object variable representing the
Excel application, or:

With objChart.Application.ActiveCell

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"jdub765" <(E-Mail Removed)> wrote in message
news:08A2E9C1-AF92-4DE1-BA9A-(E-Mail Removed)...
> Thanks for the reply!
> I tried the code you provided:
> With ActiveCell
> objChart.Parent.Left = .Left
> objChart.Parent.Top = .Top
> End With
> and while there were no compiling errors, I get runtime errors.
>
> When I use ActiveCell, I get Error 91 - Object variable or With block
> variable not set, and when I use Worksheets("Graph").Range("C12"), I get
> Error -2147221080 - Method Parent of object _Chart failed.
> Both fail on objChart.Parent.Left = .Left.
>
> I don't really understand how VB handles excel objects so I'm not sure how
> to fix it. I've been guess & checking for about an hour. Do you have any
> links that I can read so I can get a better understanding of what's going
> on?
> All the searches I've done come up with links that don't really apply to
> my
> problem...
>
> Thanks a million for the help!
>
>
> "Jon Peltier" wrote:
>
>> You need to change the position of the chart object, which is the shape
>> that
>> contains the chart:
>>
>> Private Sub GenerateGraph(objChart As Excel.Chart)
>> objChart.ChartType = xlPie
>> objChart.SetSourceData source:=objSheet.Range("B4:C5"), PlotBy _
>> :=xlColumns
>> objChart.location Where:=xlLocationAsObject, name:="Graph"
>> With ActiveCell ' or With Worksheets("Graph").Range("C12")
>> objChart.Parent.Left = .Left
>> objChart.Parent.Top = .Top
>> End With
>> End Sub
>>
>> - Jon
>> -------
>> Jon Peltier, Microsoft Excel MVP
>> Tutorials and Custom Solutions
>> http://PeltierTech.com
>> _______
>>
>>
>> "jdub765" <(E-Mail Removed)> wrote in message
>> news:E8AFBD78-54CB-4A8C-949B-(E-Mail Removed)...
>> > I'm working in TestPartner, an automation tool that uses VB6, and I
>> > either
>> > need to reposition a chart, or change the default position. I've been
>> > searching to find a solution, but I can't seem to get it to work...
>> >
>> > I found this link:
>> > http://msdn.microsoft.com/newsgroups...=en-us&m=1&p=1
>> > and I think I need to use this code:
>> >
>> > With ActiveSheet.Shapes("Chart 1")
>> > .Left = ActiveCell.Left
>> > .Top = ActiveCell.Top
>> > End With
>> >
>> > I don't know how to use the code above in my code below...
>> >
>> > Private Sub GenerateGraph(objChart As Excel.Chart)
>> > objChart.ChartType = xlPie
>> > objChart.SetSourceData source:=objSheet.Range("B4:C5"), PlotBy _
>> > :=xlColumns
>> > objChart.location Where:=xlLocationAsObject, name:="Graph"
>> > End Sub
>> >
>> > Any help is greatly appreciated!

>>
>>
>>



 
Reply With Quote
 
Jon Peltier
Guest
Posts: n/a
 
      5th Mar 2007
IncrementTop and IncrementLeft can have unexpected results, like if the
chart is created in a sheet with a different freeze panes configuration or a
window of a different size. Instead of these with relative distances, use
..Top and .Left with absolute references.

More info:
http://peltiertech.com/Excel/ChartsH...oveAChart.html

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"jdub765" <(E-Mail Removed)> wrote in message
news:A8B32B25-86DD-4988-B7EF-(E-Mail Removed)...
> omg. I found an easy solution.
>
> I found the "Record Macro" function in Excel. lol
> I clicked on "Relative Reference", moved the chart, and then looked at the
> resulting code.
> Then I changed the code a little to fit what I needed and I came up with
> this:
>
> With ActiveSheet.Shapes("Chart 1")
> .IncrementLeft -25.5
> .IncrementTop 50.25
> End With
>
> And it works!! WOOHOO! Thanks for your help Jon Peltier! It helped me
> understand what was going on a little more so that I could edit the code
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to change position of chart labels on line chart Shane Henderson Microsoft Excel Charting 1 27th May 2011 09:31 AM
Position a chart according to the position of the userform Gaetan Microsoft Excel Discussion 2 3rd Mar 2010 04:28 PM
Chart position changes =?Utf-8?B?bmFybmltYXI=?= Microsoft Excel Misc 0 5th Aug 2007 12:46 PM
how to draw crosshairs on a chart that go to the height and width of the chart and move with the cursor position? Steve Microsoft C# .NET 1 11th Sep 2006 02:41 PM
Changing chart title changes position of chart =?Utf-8?B?QmluZw==?= Microsoft Excel Programming 3 9th Mar 2005 08:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:38 PM.