bar chart one row with varying colums excel 2003

cometorta

New Member
Joined
Nov 4, 2009
Messages
25
Hi,

Can someone help me with the following.

I need a macro to create a bar chart:

the data to be charted is in one row (let's row 2); however the number of columns varies between 1 and 10. I want to a flexible macro that counts the number of columns with data, and then used that number to do the bar chart. The label for the data points are in row 1.


Thanks in advance,

Francisco
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
cometora,
remember that excel has an excellent macro recording tool that will give you most of the information you need.

I recorded creating a graph and need to change only one thing. The code you need (assuming that C2 is in your range to be charted:
Code:
Sub CreateGraph()
'
'
    Range("C2").Select
    Charts.Add
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("C2").CurrentRegion
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
End Sub

where the recorded macro read
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("C2:E2")
I changed this to the line above, with .currentregion
range("c2").currentregion will expand the address to all the cells with values attached to C2. So that will inlcude all your columns with a value
 
Upvote 0
sijpie,

Awesome! thank you very much.

One more questions can i change

ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("C2").CurrentRegion

to

ActiveChart.SetSourceData Source:=ActiveSheet.Range("C2").CurrentRegion

I want to replicate the same macro across different sheets and workbooks. So i need a macro that is so restrictive

Thanks

Francisco
 
Upvote 0
Sijpie,

I have run your code. However the CurrentRegion command graphs everything around the range "C2". I have data in the next 2 rows which I don't want to graphs. Is there such a thing as CurrentRow ?

Thanks,

Francisco
 
Upvote 0
no there isn't such a thing as currentrow. What you then need to do is set up the range properly.
so you want to count the number of columns first and then use this to define the range. Then you set the chart:
Code:
Sub CreateGraph()
'
Dim CRows As Long
Dim chRange As String
Dim ActSheet As String
    CRows = Range("C2").CurrentRegion.Columns.Count
    chRange = "C2:C" & CRows
    ActSheet = ActiveSheet.Name
    Charts.Add
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Sheets(ActSheet).Range(chRange)
    ActiveChart.Location Where:=xlLocationAsObject, Name:=ActSheet
End Sub

You need to store the name of the sheet in the variable (ActSheet I called it) as while the chart is being created, Excel will first create a chart sheet, so the active sheet has suddenly changed during the macro!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top