Histogram with unknown column length data

scolty1985

Board Regular
Joined
Sep 16, 2009
Messages
88
Good afternoon all, I was hoping someone would be kind enough to help me with this wee problem.

Im trying to write a VBA program which will automatically create a histogram for me. I already have an example for plotting an XY scatter which im trying to modify but thus far with no success.

Issues:

1) Im not sure what the syntax is for creating a histogram

2) Im not sure how to use the existing example i have so it references an undisclosed number of rows in a column.

The existing code i have, written by someone here i believe, is as follows:

Code:
With Worksheets("Sheet3").ChartObject.Add _
    (Left:=200, Width:=400, Top:=250, Height:=225)
    .Chart.SetSourceData Source:=Worksheets("DataSheet").Range("H2:H6")
    .Chart.ChartType = xlXYScatterLines

I usually use the following:

Code:
LC = Range("A" & Rows.Count).End(xlUp).Row

To store the number of rows in a column. I dont know how i can use that in a range though, ie

...Range("H2: H(LC)").ch....

If someone could also comment on the correct syntax for a historgram so i can replace the line

Code:
Chart.ChartType = xlXYScatterLines

I would appreciate it.

Rgds

Scolty
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Ok im now just stuck on the data range for the chart, any ideas how i can automatically get excel to calculate the number of entries in a column and then assign it into the Range section??

Code:
Source:=Worksheets("DataSheet").[COLOR="Red"][B]Range("H2:H6")[/B][/COLOR]
    .Chart.ChartType = xlColumnClustered

so H6 is altered automatically??

Thanks in advance

Rgds

Scolty
 
Upvote 0
Try

Code:
LR = Worksheets("DataSheet").Range("H" & Rows.Count).End(xlUp).Row
Source:=Worksheets("DataSheet").Range("H2:H" & LR)
 
Upvote 0
Thanks again VoG and Rasm. I have one finaly queery, how do i state the name for the chart?

I tried adding in the following lines within the WITH block but none seemed to work.

ActiveChart.Name = "Positive Distribution"

and

Chart.Name = "Positive Distribution"

any ideas??

Thanks in advance.
 
Upvote 0
As you have just added the chart it should be the last one on the sheet so try

Code:
ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count).Name = "Positive Distribution"
 
Upvote 0
i tried putting it inside and outside the WITH block and nothing happened.

Code:
With Worksheets("Sheet3").ChartObjects.Add _
    (Left:=910, Width:=400, Top:=250, Height:=225)
    .Chart.SetSourceData Source:=Worksheets("Sheet3").Range("G2:G" & LS)
    .Chart.ChartType = xlColumnClustered
    ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count).Name = "Positive Distribution"
End With
 
Upvote 0
Try taking that out and add after End With

Code:
Worksheets("Sheet3").ChartObjects(Worksheets("Sheet3").ChartObjects.Count).Name = "Positive Distribution"
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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