MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 3rd, 2002, 01:01 PM   #1
jamesgw
New Member
 
Join Date: May 2002
Posts: 10
Default

I am trying to create a bar chart by recording a macro. My source data is sent from an Access table to a sheet. The number of rows can vary widely. In recording, after I choose the type of chart I want, It asks me for the source data. I am using "CTRL-A" to choose all data. When I finish my chart, and run the macro, all goes according to plan. HOWEVER, when I use NEW data sent over from access, and it has MORE rows than the previous chart had, the macro will only choose the same RANGE OF DATA that I had previously used. When I go in to edit the macro, it show (for example):
ActiveChart.SetSourceData Source:=Sheets("NewChart").Range("A1:C21"), PlotBy _
:=xlColumns
My new data has 36 rows, but the macro has HARDCODED the previous old data rows. What am I doing wrong??? Thanks!!!!

[ This Message was edited by: jamesgw on 2002-05-03 12:02 ]
jamesgw is offline   Reply With Quote
Old May 3rd, 2002, 01:13 PM   #2
kkknie
Board Regular
 
Join Date: Apr 2002
Location: Greenwood, SC
Posts: 677
Default

When you record a macro, it is always hardcoded. To get around this you must dynamically define your range. Here is how I would do it.

dim iRow as Integer

iRow = Sheets("NewChart").Range("A1").CurrentRegion.Rows.Count

ActiveChart.SetSourceData Source:=Sheets("NewChart").Range(cells(1,1),cells(iRow,3), PlotBy _ :=xlColumns

This assumes that your data will always be in a sheet called "NewChart" and that you have three columns with data starting in cell A1. To make it dynamic for new columns change to:

dim iRow as Integer
dim iCol as Integer

iRow = Sheets("NewChart").Range("A1").CurrentRegion.Rows.Count

iCol = Sheets("NewChart").Range("A1").CurrentRegion.Columns.Count

ActiveChart.SetSourceData Source:=Sheets("NewChart").Range(cells(1,1),cells(iRow,iCol), PlotBy :=xlColumns

Good luck,

K
kkknie is offline   Reply With Quote
Old May 6th, 2002, 09:13 AM   #3
jamesgw
New Member
 
Join Date: May 2002
Posts: 10
Default

Thanks K, that seems to be a neat solution.


BUT,

When I tried it, I got a totally new error. This time it says:

Run-time error '1004';
Method 'cells' of object '_global' failed.

It stops on the "ActiveChart.SETSOURCE DATA Source" line.

Here is the first part of my macro.

Sub graph()
Dim iRow As Integer

Sheets("graph").Select
ActiveWindow.SelectedSheets.Delete 'delete old graph
Range("B1").Select
Selection.ClearContents 'remove title from 2nd column
Range("C1").Select
ActiveCell.FormulaR1C1 = "Length of Study" 'change 3rd column title
Cells.Select
Range("D2").Activate
iRow = Worksheets("NewChart").Range("A1").CurrentRegion.Rows.Count
Charts.Add 'add chart
ActiveChart.ChartType = xlBarStacked 'create stacked bar graph
ActiveChart.SetSourceData Source:=Sheets("NewChart").Range(Cells(1, 1), Cells(iRow, 3)), PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="GRAPH" ' change sheet name
With ActiveChart
.HasTitle = True

Could anyone tell me WHAT to look for here?
Thanks!!




[ This Message was edited by: jamesgw on 2002-05-06 09:53 ]
jamesgw is offline   Reply With Quote
Old May 7th, 2002, 01:19 PM   #4
jamesgw
New Member
 
Join Date: May 2002
Posts: 10
Default

jamesgw is offline   Reply With Quote
Old Jun 28th, 2002, 02:39 PM   #5
Old Man Winter
New Member
 
Join Date: Jun 2002
Posts: 1
Default

I am having the same problem trying to implement this code. I have another solution to selecting a dynamic area for a chart macro:
Range("A1:F1").Select
Range(Selection, Selection.End(xlDown)).Select

Charts.Add
ActiveChart.ChartType = xlLineMarkers
...

So I get the first 6 columns, as many rows are are present. Works great, except that occasionally it farts and plots data by row instead of by column. So I want to implement the PlotBy:=column, which is (cleverly!) embedded in the ActiveChart.SetSourceData portion of the code, and cannot be seperated. So I am trying to jump through these hoops to specify PlotBy:=Columns. I tried the iRow solution posted by kkknie and am getting the same Method 'cells' of object '_Global' failed error. No idea what it's talking about. Anyone else ever get a solution to this problem?
Old Man Winter is offline   Reply With Quote
Old Aug 16th, 2005, 11:59 PM   #6
striderkap
New Member
 
Join Date: Aug 2005
Posts: 1
Default

I was having the same problem until I saw your post and had the idea to try this

Range(Cells(StartRow, StartColPtr), Cells(EndRow, LastColPtr)).Select
Charts.Add
ActiveChart.ChartType = xlLine
ActiveChart.PlotBy = xlRows

Note that the ActiveChart object takes the PlotBy so that it will force plot method you want.
striderkap is offline   Reply With Quote
Old May 23rd, 2007, 11:39 AM   #7
riteshnarain
New Member
 
Join Date: May 2007
Posts: 7
Default Try the following

Set r1 = Range(Cells(1, 1), Cells(RowLimit, columnLimit))
Sheets("CHRT1 - Av Against Due by Date").Select
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=r1, PlotBy:=xlColumns

I have just posted snippet of my code but you should be able to figure out how to use it in your code

ritesh.narain@gmail.com

Ritesh
riteshnarain is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On

Forum Jump


All times are GMT -4. The time now is 04:53 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes