473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Excel help... Automate Charts

135 100+
Hi all. I am asking this question here because I have no idea where else to go with it. It involves VBA... but not Access. You guys have always been spot on with Access help so i thought maybe you could give me some direction or maybe tell me where to go with my question. I have an Excel question.

I have a list of data that is updated every day and exported from another program into an excel file. This data is in long columns of some 2500+ rows. The column headings are date, type, status, field name. Every block of 50 rows is various data for the same field name. I would like to automate chart making for each block of 50 rows. The chart will be exactly the same format with exactly the same amount of data. Here is what the macro creates for one chart:
Expand|Select|Wrap|Line Numbers
  1. Sub Macro12()
  2.     ActiveSheet.Shapes.AddChart.Select
  3.     ActiveChart.ChartType = xlLine
  4.     ActiveChart.SeriesCollection.NewSeries
  5.     ActiveChart.SeriesCollection(1).Values = "='Sheet1'!$D$2:$D$50"
  6.     ActiveChart.SeriesCollection(1).XValues = "='Sheet1'!$F$2:$F$50"
  7. End Sub
How to have a chart created to represent every 50 rows of data? I suppose i could copy the code 50 times and find out what cells would be included in each section... but that seems so archaic. Ideas? Or where I should go post this question? Thanks all.
Mar 12 '11 #1
9 3155
ADezii
8,834 Expert 8TB
Just subscribing for now, will see if any ideas pop up in my head...
Mar 12 '11 #2
MyWaterloo
135 100+
Ya... It seems like it shouldn't be too difficult: Code for chart creation; Code to move 50+ rows and create a new chart using next 50 rows; and so on...but I can't get my brain around it... or find any similar posts through an internet search that could help.
Mar 12 '11 #3
ADezii
8,834 Expert 8TB
This Code has not been tested, so I really do not know how well, or if at all, it will work. You may have to tweak it a little, but hopefully it will point you in the right direction. Good Luck.
Expand|Select|Wrap|Line Numbers
  1. Dim intCtr As Integer
  2. Const conNUM_OF_ROWS As Integer = 2500
  3.  
  4. For intCtr = 2 To conNUM_OF_ROWS Step 50
  5.   ActiveSheet.Shapes.AddChart.Select
  6.     With ActiveChart
  7.       .ChartType = xlLine
  8.       .SeriesCollection.NewSeries
  9.       .SeriesCollection(1).Values = "='Sheet1'!$D$" & CStr(intCtr) & ":$D$" & CStr(intCtr + 49)
  10.       .SeriesCollection(1).XValues = "='Sheet1'!$F$" & CStr(intCtr) & ":$F$" & CStr(intCtr + 49)
  11.     End With
  12. Next
Mar 12 '11 #4
MyWaterloo
135 100+
Wow! It worked perfect! I plugged in the code and boom! More charts than you can shake a stick at! If I could trouble you once more... How can I select the name for the charts? The Names reside in column B. I tried:
Expand|Select|Wrap|Line Numbers
  1.     Dim intCtr As Integer
  2.     Const conNUM_OF_ROWS As Integer = 2646
  3.  
  4.     For intCtr = 2 To conNUM_OF_ROWS Step 49
  5.       ActiveSheet.Shapes.AddChart.Select
  6.         With ActiveChart
  7.           ActiveChart.Name = "='Sheet1'!$B$" & CStr(intCtr) & ":$B$" & CStr(intCtr + 48)
  8.           .ChartType = xlLine
  9.           .SeriesCollection.NewSeries
  10.           .SeriesCollection(1).Values = "='Sheet1'!$D$" & CStr(intCtr) & ":$D$" & CStr(intCtr + 48)
  11.          .SeriesCollection(1).XValues = "='Sheet1'!$F$" & CStr(intCtr) & ":$F$" & CStr(intCtr + 48)
  12.        End With
  13.    Next
...but it doesn't work to try and name the charts like that. (I also discovered it is every 49 cells for chart data, hence the change in cells to count.) Also, all the charts are created as objects piled one on top of another, is there a way to tell them to spread out or align to a grid maybe? Like 5 across and 10 down? Thanks again, I will keep playing with this and keep checking back here also.
Mar 13 '11 #5
MyWaterloo
135 100+
I figured out the naming issue:
Expand|Select|Wrap|Line Numbers
  1.         Dim intCtr As Integer
  2.         Const conNUM_OF_ROWS As Integer = 2646
  3.  
  4.         For intCtr = 2 To conNUM_OF_ROWS Step 49
  5.           ActiveSheet.Shapes.AddChart.Select
  6.             With ActiveChart
  7.               .ChartType = xlLine
  8.               .SeriesCollection.NewSeries
  9.               .SeriesCollection(1).Values = "='Sheet1'!$D$" & CStr(intCtr) & ":$D$" & CStr(intCtr + 48)
  10.               .SeriesCollection(1).XValues = "='Sheet1'!$F$" & CStr(intCtr) & ":$F$" & CStr(intCtr + 48)
  11.               .SeriesCollection(1).Name = "='Sheet1'!$B$" & CStr(intCtr + 48)
  12.         End With
  13.       Next
This works great to creat the charts and give them the appropriate names. There is one strange anomaly, the charts are all created with a phantom 2nd series called "Series2". I can't figure out why unless there is a default series when a chart is created. Se attached image. "Series2" is also empty of data.
Attached Images
File Type: jpg ScreenShot008.jpg (35.8 KB, 215 views)
Mar 13 '11 #6
MyWaterloo
135 100+
Ok, the extra series is from the:
Expand|Select|Wrap|Line Numbers
  1. .SeriesCollection.NewSeries
This is not needed as the chart is created with a default series that the rest of the code then defines with .Values and the .XValues.


I would like to add another cell address to the naming code so the name is derived from column B & E.

Current Code:
Expand|Select|Wrap|Line Numbers
  1.  .SeriesCollection(1).Name = "='Sheet1'!$B$" & CStr(intCtr + 48)
Change to something like:
Expand|Select|Wrap|Line Numbers
  1. .SeriesCollection(1).Name = "='Sheet1'!$B$" & "='Sheet1'!$E$" & CStr(intCtr + 48)
, but obviously this doesn't work.


So I am still attempting to find a way to align the charts to a grid, and trying to pull the name from two different cells.
Mar 13 '11 #7
ADezii
8,834 Expert 8TB
I would like to add another cell address to the naming code so the name is derived from column B & E.
Expand|Select|Wrap|Line Numbers
  1. Dim intCtr As Integer
  2.  
  3. intCtr = 2
  4.  
  5. MsgBox Worksheets("Sheet1").Range("B" & CStr(intCtr)) & _
  6.        Worksheets("Sheet1").Range("C" & CStr(intCtr + 48))
Mar 13 '11 #8
MyWaterloo
135 100+
Thanks! That works... I also found code for aligning the charts:
Expand|Select|Wrap|Line Numbers
  1. Sub AlignCharts()
  2.   ' Jon Peltier (3/19/2008)
  3.   ' http://peltiertech.com/WordPress/
  4.   ' Changes MyWaterloo (3/13/2011)
  5.  
  6.   ' chart size - adjust as desired
  7.   Const nRowsTall As Long = 13
  8.   Const nColsWide As Long = 9
  9.  
  10.   ' chart layout - adjust as desired
  11.   Const nChartsPerRow As Long = 3
  12.   Const nSkipRows As Long = 2
  13.   Const nSkipCols As Long = 1
  14.   Const nFirstRow As Long = 3
  15.   Const nFirstCol As Long = 1
  16.  
  17.   Dim iChart As Long
  18.   Dim chtob As ChartObject
  19.   Dim dWidth As Double
  20.   Dim dHeight As Double
  21.   Dim rData As Range
  22.   Dim dFirstChartTop As Double
  23.   Dim dFirstChartLeft As Double
  24.   Dim dRowsBetweenChart As Double
  25.   Dim dColsBetweenChart As Double
  26.  
  27.   With Worksheets("Sheet2").Cells(1, 1)
  28.     dWidth = nColsWide * .Width
  29.     dHeight = nRowsTall * .Height
  30.     dFirstChartLeft = (nFirstCol - 1) * .Width
  31.     dFirstChartTop = (nFirstRow - 1) * .Height
  32.     dRowsBetweenChart = nSkipRows * .Height
  33.     dColsBetweenChart = nSkipCols * .Width
  34.   End With
  35.  
  36.   For iChart = 1 To Worksheets("Sheet2").ChartObjects.Count
  37.  
  38.     Set chtob = Worksheets("Sheet2").ChartObjects(iChart)
  39.  
  40.     With chtob
  41.       .Left = ((iChart - 1) Mod nChartsPerRow) * (dWidth + dColsBetweenChart) + dFirstChartLeft
  42.       .Top = Int((iChart - 1) / nChartsPerRow) * (dHeight + dRowsBetweenChart) + dFirstChartTop
  43.       .Width = dWidth
  44.       .Height = dHeight
  45.     End With
  46.  
  47.   Next
  48.  
  49.  
  50.  
  51.  
  52.  
  53. End Sub
Thanks for all your help ADezii, I really appreciate it.

God Bless,
MyWaterloo
Mar 13 '11 #9
ADezii
8,834 Expert 8TB
@MyWaterloo - You really didn't need us at all! (LOL)
Mar 13 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: steve | last post by:
Hi, I have generated a datagrid with the following columns: | Date-Time | Station ID | Parameter1 | .... | Parameter 2| I would like to: Create an Excel file and preferably give the user...
0
by: hemanth | last post by:
Hi, I have a problem at hand. There is a link http://alpha2.iimb.ernet.in/pgsem2006/interviewcall.html. Which displays the result of an exam. I want to check the result of all the 2000...
6
by: jtswim01 | last post by:
I have a very basic issue in excel and I can't for the life of me find the solution. I have a dense worksheet of data and I want to distill some of the information into a much shorter list. The...
0
by: johnlim20088 | last post by:
Hi, Hi someone can help me on this? Currently I have a Listdata.aspx page with datagrid, I wish to export my datagrid content to excel with following code ( with button export click event):- ...
5
by: Louis | last post by:
I am trying to use php to update a spreadsheet (MS Excel or OpenOffice Calc) with data from MySQL. I looked into PEAR::Spreadsheet_Excel_Writer if it will do the job. If I understand it...
2
budigila
by: budigila | last post by:
Hiya peeps, Okies, I have been trying to work this out for a while now to no avail... I am a beginner to this whole coding thing but have made great strides in my project. Basically what I am...
10
KodeKrazy
by: KodeKrazy | last post by:
I'm trying to read an Excel worksheet and do a find/replace for all of the commas "," in any of the cells in the sheet and replace them with a dash "-" I can get as far as getting the workbook...
0
by: arunbalait | last post by:
Hi How can I display charts in VB.Net with multiple Y-Axes.? I need to display chart with multiple Y-Axis, but with Ms.Chart control there are options for 2 Y-Axis. but i want more than that. ...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
7
Catalyst159
by: Catalyst159 | last post by:
I want to automate the process of listing items for selling on ebay. Any help would be greatly appreciated. I would like to try and use Microsoft Access 2007 with VBA to accomplish this, but I am...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.