Solved

How to set the top argument of the ChartObjects.Add method dynamically

Posted on 2015-02-03
15
101 Views
Last Modified: 2016-02-11
Hello,

I have a worksheet that consists of a datatable and five charts.  The table is dynamic in size.  I'lm trying to modify my code so that the charts will move up or down so that the distance between the charts and the datatable remains constant.  I am creating the charts and in fact via automation from Access.  Everything populates fine.  I think I may have to use the current.region property in the top argument but am not sure how.  In addition, I'm not sure how to convert the currentregion to points.

Set objXlChart = objXLSheet.ChartObjects.Add(Left:=75, Width:=750, Top:=150, Height:=300).Chart


Private Sub CreateNE_TotalPercentItemsAvailableChart(objWS As Object)
Dim objXLSheet As Object
Dim objXlChart As Object
Dim objXlDataSheet As Object
Dim rng As Object
Dim rngChart As Object

Set objXLSheet = objWS
Set objXlDataSheet = objXlBook.Worksheets("Summary")
Set rng = objXlDataSheet.Range("A:A,D:D")

Set objXlChart = objXLSheet.ChartObjects.Add(Left:=75, Width:=750, Top:=150, Height:=300).Chart



    With objXlChart
        .ChartType = 4  'xlLine
        .SetSourceData Source:=rng
        .PlotBy = 2
        .HasTitle = True
        .ChartTitle.Text = "Norgren Express" & vbCrLf & "Total Items % Available"
        .HasLegend = False
        .Axes(2).TickLabels.NumberFormat = "0%"
        .Axes(2).TickLabels.Orientation = 0
        .Axes(1).TickLabels.Orientation = 60
        .ChartStyle = 31
    End With

End Sub

Open in new window

0
Comment
Question by:chtullu135
  • 10
  • 4
15 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40586711
I'lm trying to modify my code so that the charts will move up or down so that the distance between the charts and the datatable remains constant.
Is this in a Report r a form?
What about page breaks?
Move the chart?, ...or do you mean shrink/grow?

This is all difficult to visualize...
Can you post a screenshot of what you have now, ...then post a clear graphical example of what you want it to be...?

I have done something like this for Images,...so the same  technique might be useful here.

JeffCoachman
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40586782
convert the value of the Top parameter to a variable.

dim xTop as long
xTop=150 ' start of the chart vertical location

then just add the height of the chart + space to xTop variable as you are plotting the succeeding charts
0
 

Author Comment

by:chtullu135
ID: 40587117
convert the value of the Top parameter to a variable.

dim xTop as long
xTop=150 ' start of the chart vertical location

then just add the height of the chart + space to xTop variable as you are plotting the succeeding charts

I want to be able to set the value of xtop for the first chart equal to the height of the currentregion of the datatable   (which is the datasource for the graphs and which may expand according to the underlying recordset" plus a space of 50 points
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40587143
how do you populate the datatable?
0
 

Author Comment

by:chtullu135
ID: 40587266
I populate the datatable from a query.  It's jus a dataset that is used to populate a chart.  Depending on the data, it can be more or less rows.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40587283
<I populate the datatable from a query>

are you using vba to do this or copy and paste
0
 

Author Comment

by:chtullu135
ID: 40587307
I am using vba. I have a query and I am using the copy recordset method to put the results of the query on the worksheet.  I am then generating several charts from that data.  I want to ensure that no matter how many rows of data are returned that the charts do not end up overlapping one another or the dataset at the top of the worksheet.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40587363
after the copyfromrecordset line,

you can get the number of rows that was used with

objXlDataSheet.UsedRange.rows.count

from here you can based your starting xTop variable
0
 

Author Comment

by:chtullu135
ID: 40587365
I've attached a screenshot of the worksheet.  I want to dynamically position the top position of each chart so that they will never overlap
Worksheet.jpg
0
 

Author Comment

by:chtullu135
ID: 40587372
objXlDataSheet.UsedRange.rows.count
From reading about the top argument, it uses points.  Is there a way to convert the rows count to point?  It seems like they are apples and oranges
0
 

Author Comment

by:chtullu135
ID: 40587392

after the copyfromrecordset line,

you can get the number of rows that was used with

objXlDataSheet.UsedRange.rows.count

from here you can based your starting xTop variable



I see what you mean.  I'll give it a try.
0
 

Author Comment

by:chtullu135
ID: 40618128
I've requested that this question be closed as follows:

Accepted answer: 0 points for chtullu135's comment #a40587392

for the following reason:

Thank you very much for your help.
0
 

Author Comment

by:chtullu135
ID: 40618127
I'd like to cancel my request to close the question.  I meant to give the point to Rey
0
 

Author Comment

by:chtullu135
ID: 40618129
I'd like to cancel my request to close the question.  I meant to award the points to rey.
0
 

Author Closing Comment

by:chtullu135
ID: 40618130
Thanks Rey
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question