saltire1963
Board Regular
- Joined
- Aug 11, 2014
- Messages
- 69
I am trying to create a chart from a 2 column range of data using VBA (XL2010), one where the x-axis values are not in numerical order.
For example, in range A2:B6, if I have no's 1 to 5 down column A and the no's 3, 2, 6, 1, 8 down column B, I would like to create a bar style chart that shows the values (column B) listed in order from high to low, but also show the x-axis with the corresponding number from column A. For example, the highest no. in column B is 8 and is the 5th no. in column A. I would like the chart to put the no's in correct order 8, 6, 3, 2, 1 but also the corresponding no's as the x-axis i.e. 5, 3, 1, 2, 4
I've tried copying the range A1:B6 to G1:H6 and sorting this new range in a macro which works fine, but when I try to use the same code in my own module it sorts Column H, but does not bring the corresponding no's in column G along with it. See code below.
Range("A1").Select
Sheet1.Sort.SortFields.Add Key:=ActiveCell.Offset(0, 1).Range("A2:A6"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With Sheet1.Sort
.SetRange ActiveCell.Offset(0, 1).Range("A1:B6")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The other problem is when I create the chart using the macro values, it still makes the x-axis no's 1 to 5. I've seen that I need to create an additional dummy series of values that are all 0's and then link the make the correctly ordered numbers in Column G into the labels for the zero values, which works fine when creating the chart in excel, but I need to be able to do this in vba. So if I add 2 more columns of data, say column E2:E6 is no's 1 to 5 and Column F2:F5 are all zero's I have tried the code:
Set Cht = Sheet1.Shapes.AddChart(XlChartType:=xlColumnClustered, Left:=[B46].Left, Top:=[B46].Top, Width:=[B46:I46].Width, Height:=[B46:B64].Height).Chart
With Cht
.SetSourceData Source:=Range("E2:H6")
.SetElement (msoElementLegendNone)
.ChartGroups(1).GapWidth = 100
.SetElement (msoElementDataLabelOutSideEnd)
End With
Sorry, its a bit long winded and would like to send spread sheet as example, but not sure how to do this. Appreciate any help.
For example, in range A2:B6, if I have no's 1 to 5 down column A and the no's 3, 2, 6, 1, 8 down column B, I would like to create a bar style chart that shows the values (column B) listed in order from high to low, but also show the x-axis with the corresponding number from column A. For example, the highest no. in column B is 8 and is the 5th no. in column A. I would like the chart to put the no's in correct order 8, 6, 3, 2, 1 but also the corresponding no's as the x-axis i.e. 5, 3, 1, 2, 4
I've tried copying the range A1:B6 to G1:H6 and sorting this new range in a macro which works fine, but when I try to use the same code in my own module it sorts Column H, but does not bring the corresponding no's in column G along with it. See code below.
Range("A1").Select
Sheet1.Sort.SortFields.Add Key:=ActiveCell.Offset(0, 1).Range("A2:A6"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With Sheet1.Sort
.SetRange ActiveCell.Offset(0, 1).Range("A1:B6")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The other problem is when I create the chart using the macro values, it still makes the x-axis no's 1 to 5. I've seen that I need to create an additional dummy series of values that are all 0's and then link the make the correctly ordered numbers in Column G into the labels for the zero values, which works fine when creating the chart in excel, but I need to be able to do this in vba. So if I add 2 more columns of data, say column E2:E6 is no's 1 to 5 and Column F2:F5 are all zero's I have tried the code:
Set Cht = Sheet1.Shapes.AddChart(XlChartType:=xlColumnClustered, Left:=[B46].Left, Top:=[B46].Top, Width:=[B46:I46].Width, Height:=[B46:B64].Height).Chart
With Cht
.SetSourceData Source:=Range("E2:H6")
.SetElement (msoElementLegendNone)
.ChartGroups(1).GapWidth = 100
.SetElement (msoElementDataLabelOutSideEnd)
End With
Sorry, its a bit long winded and would like to send spread sheet as example, but not sure how to do this. Appreciate any help.