Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

VBA - Excel Charting help

Question posted by: sitko (Familiar Sight) on July 23rd, 2008 09:02 PM
Hi,

I'm trying to create a chart in VBA in an excel sheet. When I manually create it, it creates it just find, but for some reason, when I try to automate its creation, it doesn't include both values in the Datalabels, here is the code I use:
Expand|Select|Wrap|Line Numbers
  1. ' (NumBottles is passed in)
  2.  
  3.     Dim wb As Excel.Workbook
  4.     Dim posSheet As Worksheet
  5.     Dim NodeNum As Integer
  6.     Dim ChartTitle As String
  7.  
  8.     Set wb = Application.ActiveWorkbook
  9.     Set posSheet = wb.Sheets("Positions")
  10.  
  11.     Charts.Add
  12.     ActiveChart.ChartType = xlXYScatter
  13.     ActiveChart.SeriesCollection(1).Name = "Filler Head Positions"
  14.     ActiveChart.SeriesCollection(1).XValues = posSheet.Range("N3:N" & NumBottles + 2)
  15.     ActiveChart.SeriesCollection(1).Values = posSheet.Range("O3:O" & NumBottles + 2)
  16.     ActiveChart.Location Where:=xlLocationAsObject, Name:="Positions"
  17.     With ActiveChart
  18.         .HasAxis(xlCategory, xlPrimary) = False
  19.         .HasAxis(xlValue, xlPrimary) = False
  20.     End With
  21.     ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
  22.     With ActiveChart.Axes(xlCategory)
  23.         .HasMajorGridlines = False
  24.         .HasMinorGridlines = False
  25.     End With
  26.     With ActiveChart.Axes(xlValue)
  27.         .HasMajorGridlines = False
  28.         .HasMinorGridlines = False
  29.     End With
  30.     ActiveChart.HasLegend = False
  31.      With ActiveChart.SeriesCollection(1) 'selection
  32.         .HasDataLabels = True 'selection
  33.         With .DataLabels
  34.             .ShowValue = True
  35. ' I wonder if there is some way to force both values to be visible here
  36.             .HorizontalAlignment = xlCenter
  37.             .VerticalAlignment = xlCenter
  38.             .ReadingOrder = xlContext
  39.             .Position = xlLabelPositionBelow
  40.             .Orientation = xlHorizontal
  41.         End With
  42.     End With
  43.     ActiveChart.SeriesCollection(1).Select
  44.     With Selection.Border
  45.         .Weight = xlHairline
  46.         .LineStyle = xlNone
  47.     End With
  48.     With Selection
  49.         .MarkerBackgroundColorIndex = xlNone
  50.         .MarkerForegroundColorIndex = xlAutomatic
  51.         .MarkerStyle = xlCircle
  52.         .Smooth = False
  53.         .MarkerSize = 15
  54.         .Shadow = False
  55.     End With
  56.     ActiveChart.PlotArea.Select
  57.     With Selection.Border
  58.         .ColorIndex = 57
  59.         .Weight = xlThin
  60.         .LineStyle = xlContinuous
  61.     End With
  62.     Selection.Interior.ColorIndex = xlNone
MikeTheBike's Avatar
MikeTheBike
Expert
267 Posts
July 25th, 2008
01:11 PM
#2

Re: VBA - Excel Charting help
Quote:
Hi,

I'm trying to create a chart in VBA in an excel sheet. When I manually create it, it creates it just find, but for some reason, when I try to automate its creation, it doesn't include both values in the Datalabels, here is the code I use:
Expand|Select|Wrap|Line Numbers
  1. ' (NumBottles is passed in)
  2.  
  3.     Dim wb As Excel.Workbook
  4.     Dim posSheet As Worksheet
  5.     Dim NodeNum As Integer
  6.     Dim ChartTitle As String
  7.  
  8.     Set wb = Application.ActiveWorkbook
  9.     Set posSheet = wb.Sheets("Positions")
  10.  
  11.     Charts.Add
  12.     ActiveChart.ChartType = xlXYScatter
  13.     ActiveChart.SeriesCollection(1).Name = "Filler Head Positions"
  14.     ActiveChart.SeriesCollection(1).XValues = posSheet.Range("N3:N" & NumBottles + 2)
  15.     ActiveChart.SeriesCollection(1).Values = posSheet.Range("O3:O" & NumBottles + 2)
  16.     ActiveChart.Location Where:=xlLocationAsObject, Name:="Positions"
  17.     With ActiveChart
  18.         .HasAxis(xlCategory, xlPrimary) = False
  19.         .HasAxis(xlValue, xlPrimary) = False
  20.     End With
  21.     ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
  22.     With ActiveChart.Axes(xlCategory)
  23.         .HasMajorGridlines = False
  24.         .HasMinorGridlines = False
  25.     End With
  26.     With ActiveChart.Axes(xlValue)
  27.         .HasMajorGridlines = False
  28.         .HasMinorGridlines = False
  29.     End With
  30.     ActiveChart.HasLegend = False
  31.      With ActiveChart.SeriesCollection(1) 'selection
  32.         .HasDataLabels = True 'selection
  33.         With .DataLabels
  34.             .ShowValue = True
  35. ' I wonder if there is some way to force both values to be visible here
  36.             .HorizontalAlignment = xlCenter
  37.             .VerticalAlignment = xlCenter
  38.             .ReadingOrder = xlContext
  39.             .Position = xlLabelPositionBelow
  40.             .Orientation = xlHorizontal
  41.         End With
  42.     End With
  43.     ActiveChart.SeriesCollection(1).Select
  44.     With Selection.Border
  45.         .Weight = xlHairline
  46.         .LineStyle = xlNone
  47.     End With
  48.     With Selection
  49.         .MarkerBackgroundColorIndex = xlNone
  50.         .MarkerForegroundColorIndex = xlAutomatic
  51.         .MarkerStyle = xlCircle
  52.         .Smooth = False
  53.         .MarkerSize = 15
  54.         .Shadow = False
  55.     End With
  56.     ActiveChart.PlotArea.Select
  57.     With Selection.Border
  58.         .ColorIndex = 57
  59.         .Weight = xlThin
  60.         .LineStyle = xlContinuous
  61.     End With
  62.     Selection.Interior.ColorIndex = xlNone
Hi

Pehaps you need something like this (I cannot see any in your code) to set the datalable value)

Expand|Select|Wrap|Line Numbers
  1. With ActiveChart.SeriesCollection(1) 'selection
  2.          .HasDataLabels = True 'selection
  3.          .Name = "Data Label Series 1 Name"
  4.         ' .........
  5.         'etc
  6.  
  7. End With
???


MTB

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,074 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Visual Basic Forum Contributors