MS Office Forum / Excel / Charting / September 2007
how to create a chart from a dynamic table of data?
|
|
Thread rating:  |
linda - 30 Jul 2007 07:50 GMT hai!! i need to create a chart from a dynamic table,meaning that the table is auto-generated from database after clicking a button.This is my first time using macro in excel, so i'm not really well in it. my idea right now is using Chart.Add,but then i'm having problem on how to define the cells selection(i must do "the do..loop statement" rite?) ur help is very appreciated,tq!
Jon Peltier - 30 Jul 2007 15:18 GMT Use the macro recorder as a guide. When I record a macro to create a simple chart it looks like:
Sub Macro1() ' Macro recorded 7/30/2007 by Jon Peltier ' Charts.Add ActiveChart.ChartType = xlColumnClustered ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:C6") ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1" End Sub
Assuming your DB dump is on a new sheet, and applying some cleanup, this is a workable macro:
Sub DB_Dump_to_Chart() Dim rChartData As Range Dim wsData As Worksheet
Set wsData = ActiveSheet Set rChartData = wsData.UsedRange
Charts.Add ActiveChart.ChartType = xlColumnClustered ActiveChart.SetSourceData Source:=rChartData ActiveChart.Location Where:=xlLocationAsObject, Name:=wsData.Name End Sub
Naturally your recorded macro will be different, since you will record your actions while creating the type of chart you want from the data. But make the same kind of adjustments as I have made, and you should get a reasonable macro.
For more on fixing up chart macros:
http://peltiertech.com/Excel/ChartsHowTo/QuickChartVBA.html
- Jon ------- Jon Peltier, Microsoft Excel MVP Tutorials and Custom Solutions Peltier Technical Services, Inc. - http://PeltierTech.com _______
> hai!! > i need to create a chart from a dynamic table,meaning that the table is [quoted text clipped - 3 lines] > define the cells selection(i must do "the do..loop statement" rite?) > ur help is very appreciated,tq! linda - 31 Jul 2007 02:22 GMT meaning that doesn't need the looping statement?so,how to i check the size of table that used to generate the chart?
> Use the macro recorder as a guide. When I record a macro to create a simple > chart it looks like: [quoted text clipped - 47 lines] > > define the cells selection(i must do "the do..loop statement" rite?) > > ur help is very appreciated,tq! linda - 31 Jul 2007 08:44 GMT thank you for your reply..its works,but then..my range for SourceData is dynamic,not static..so,i think i need to do 'the do while loop statement' but i dont have any idea about the coding.
ur help is very appreciated,tq!
regards, linda
> Use the macro recorder as a guide. When I record a macro to create a simple > chart it looks like: [quoted text clipped - 47 lines] > > define the cells selection(i must do "the do..loop statement" rite?) > > ur help is very appreciated,tq! Jon Peltier - 31 Jul 2007 16:10 GMT How does the data get updated? Isn't it some piece of code? What is the sheet like after the update? Is the data table the only contents, or is there more stuff? Is the address of the output range known to the update code? The answer to your question is dependent on the answers to these (and probably more).
- Jon ------- Jon Peltier, Microsoft Excel MVP Tutorials and Custom Solutions Peltier Technical Services, Inc. - http://PeltierTech.com _______
> thank you for your reply..its works,but then..my range for SourceData is > dynamic,not static..so,i think i need to do 'the do while loop statement' [quoted text clipped - 63 lines] >> > define the cells selection(i must do "the do..loop statement" rite?) >> > ur help is very appreciated,tq! linda - 01 Aug 2007 03:56 GMT My SourceData should be expand as i add new rows or new columns or both and it will update the chart.i've tried to use the offset function but its just work for adding new row.Adding a new column does not effect the chart created.i use OFFSET with a defined name.
date-refers to:=OFFSET($A$2,0,0,COUNTA($A$2:$A$200),1) sales-refers to:=OFFSET($B$2,0,0,COUNT($B$2:$B$200),1)
i've try define another offset but it doesnt work.Got error 'the references is not valid.References for titles,values,or size must be in a single cell,row or column.
acData-refers to:=OFFSET($A$1,0,0,COUNTA($A:$A),6)
can these offset define in macro?if yes,where should i placed the code?
regards, linda
> How does the data get updated? Isn't it some piece of code? What is the > sheet like after the update? Is the data table the only contents, or is [quoted text clipped - 76 lines] > >> > define the cells selection(i must do "the do..loop statement" rite?) > >> > ur help is very appreciated,tq! Jon Peltier - 01 Aug 2007 05:12 GMT You're solving the wrong problem. This approach might be part of the solution, but it's not clear yet. It would be easiest to incorporate a few lines of code into the routine that updates the database. That's why I asked those questions in my last post.
- Jon ------- Jon Peltier, Microsoft Excel MVP Tutorials and Custom Solutions Peltier Technical Services, Inc. - http://PeltierTech.com _______
> My SourceData should be expand as i add new rows or new columns or both > and [quoted text clipped - 106 lines] >> >> > define the cells selection(i must do "the do..loop statement" rite?) >> >> > ur help is very appreciated,tq! linda - 01 Aug 2007 07:14 GMT Sorry cause i didnt get what you meant..can you explain me more clear?
> You're solving the wrong problem. This approach might be part of the > solution, but it's not clear yet. It would be easiest to incorporate a few [quoted text clipped - 118 lines] > >> >> > define the cells selection(i must do "the do..loop statement" rite?) > >> >> > ur help is very appreciated,tq! Jon Peltier - 01 Aug 2007 12:17 GMT How do you update the dynamic data range?
- Jon ------- Jon Peltier, Microsoft Excel MVP Tutorials and Custom Solutions Peltier Technical Services, Inc. - http://PeltierTech.com _______
> Sorry cause i didnt get what you meant..can you explain me more clear? > [quoted text clipped - 134 lines] >> >> >> > rite?) >> >> >> > ur help is very appreciated,tq! linda - 02 Aug 2007 01:42 GMT I just select all from database..so,by time to time,there's may be a new variable added..but i dont want to create a new chart..just update it.The no of row alwalys increase cause new rows of data will be add everyday. So,i will create an 'update' button on excel,and when i click it,it will get the latest data from database,put it in a table in excel and automatically create graph from the data in the table.
Regards, linda
> How do you update the dynamic data range? > [quoted text clipped - 143 lines] > >> >> >> > rite?) > >> >> >> > ur help is very appreciated,tq! Jon Peltier - 02 Aug 2007 16:17 GMT I see, no macro, it's a manual update.
Make sure the data range is clean, only data plus a header row with field names, the categories (X-axis labels or values) in the first column, the top left cell blank. Any other information in the sheet should be removed, or at the very least separated from the data by a blank row or column.
Assuming you have only one chart on the worksheet, select a cell in the data and run this macro:
Sub UpdateChartOnActiveSheet() ActiveSheet.ChartObjects(1).Chart.SetSourceData _ Source:=ActiveCell.CurrentRegion, PlotBy:=xlColumns End Sub
- Jon ------- Jon Peltier, Microsoft Excel MVP Tutorials and Custom Solutions Peltier Technical Services, Inc. - http://PeltierTech.com _______
>I just select all from database..so,by time to time,there's may be a new > variable added..but i dont want to create a new chart..just update it.The [quoted text clipped - 168 lines] >> >> >> >> > rite?) >> >> >> >> > ur help is very appreciated,tq! linda - 03 Aug 2007 04:32 GMT Jon, i've tried to run the macro that you gave but i got error. Run-time error '91':Object variable or With block varible not set. this is my current coding in macro.
Sub CreateChart() Charts.Add ActiveChart.ChartType = xlColumnClustered ** ActiveChart.SetSourceData Source:=ActiveCell.CurrentRegion, PlotBy:=xlColumns ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1" End Sub
the error is on the line that i put **.
am i missing something that make it error?sorry cause i've no experience in macro. thanks~
 Signature Regards, Linda
> I see, no macro, it's a manual update. > [quoted text clipped - 190 lines] > >> >> >> >> > rite?) > >> >> >> >> > ur help is very appreciated,tq! linda - 03 Aug 2007 05:18 GMT Jon, i already fixed the error..thank you very much..=)
 Signature Regards, Linda
> Jon, > i've tried to run the macro that you gave but i got error. [quoted text clipped - 209 lines] > > >> >> >> >> > rite?) > > >> >> >> >> > ur help is very appreciated,tq! linda - 12 Sep 2007 05:06 GMT Jon, where to change the code if my chart and the source data is not in the same worksheet?
 Signature Regards, Linda
> I see, no macro, it's a manual update. > [quoted text clipped - 190 lines] > >> >> >> >> > rite?) > >> >> >> >> > ur help is very appreciated,tq!
|
|
|