?
Solved

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

Posted on 2015-02-03
15
Medium Priority
?
134 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

864 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