Results 1 to 1 of 1

VBA to change label, marker style and colour

Threaded View

Rajoli VBA to change label, marker... 09-27-2012, 08:31 AM
  1. #1
    Registered User
    Join Date
    09-27-2012
    Location
    Ottawa,Canada
    MS-Off Ver
    Excel 2010
    Posts
    1

    VBA to change label, marker style and colour

    Firstly, hello everyone, let me introduce myself.

    My name is Rajoli and I'm a new member.

    I have a unique problem in which the code works with no problems in Excel 2007 and sort of works in Excel 2010.
    If I choose to have marker style to be X or star or plus, it works fine in Excel 2007.
    In Excel 2010, X or star or plus are shown as a square.

    Here is the code and data:

    Symbol Color
    X Y Name Code Code
    78% 55% Alpha 4 1
    77% 80% Bravo 5 53
    66% 69% Charlie 9 11
    11% 67% Delta 1 9
    11% 52% Echo 2 46
    65% 13% FoxTrot 3 7
    56% 32% Golf 7 6


    ' Copyright (c) 2009 Clear Lines Consulting LLC. All rights reserved.
    ' http://www.clear-lines.com
    ' Licensed under the Microsoft Public License (Ms-PL):
    ' see http://www.microsoft.com/opensource/licenses.mspx

    Option Explicit
    Const sizeOfMarker As Integer = 8
    
    ' Column 1 contains X values
    ' Column 2 contains Y values
    ' Column 3 contains label
    ' Column 4 contains symbol code
    ' Column 5 contains color code
    Public Sub DrawScatterplot()
    
    Dim sheet As Worksheet
    Set sheet = ActiveSheet
    
    Dim data As Range
    Set data = Selection
    
    Dim firstRow As Integer
    Dim lastRow As Integer
    Dim firstColumn As Integer
    Dim lastColumn As Integer
    
    firstRow = data.Row
    firstColumn = data.Column
    
    Dim lastCell As Range
    Set lastCell = data.End(xlToRight)
    lastColumn = lastCell.Column
    Set lastCell = data.End(xlDown)
    lastRow = lastCell.Row
    
    Dim rows As Integer
    rows = lastRow - firstRow + 1
    Dim columns As Integer
    columns = lastColumn - firstColumn + 1
    
    ' Identify the range containing the plot data
    Dim scatterData As Range
    Set scatterData = Range(Cells(firstRow, firstColumn), Cells(firstRow + rows - 1, firstColumn + 1))
    
    ' Identify the ranges containing labels, symbols and colors
    Dim labels As Range
    Dim pointSymbols As Range
    Dim pointColors As Range
    
    Set labels = Range(Cells(firstRow, firstColumn + 2), Cells(firstRow + rows - 1, firstColumn + 3))
    Set pointSymbols = Range(Cells(firstRow, firstColumn + 3), Cells(firstRow + rows - 1, firstColumn + 4))
    Set pointColors = Range(Cells(firstRow, firstColumn + 4), Cells(firstRow + rows - 1, firstColumn + 5))
    
    Charts.Add
    ActiveChart.ChartType = xlXYScatter
    ActiveChart.SetSourceData Source:=scatterData
    ActiveChart.Location Where:=xlLocationAsObject, Name:=sheet.Name
    ActiveChart.HasLegend = False
    ActiveChart.PlotArea.Interior.Color = xlNone
    
    ' Add and format Gridlines
    With ActiveChart.Axes(xlCategory)
        .HasMajorGridlines = True
        .HasMinorGridlines = False
    End With
    With ActiveChart.Axes(xlCategory).MajorGridlines.Border
        .ColorIndex = 16
        .Weight = xlHairline
        .LineStyle = xlDot
    End With
        
    With ActiveChart.Axes(xlValue)
        .HasMajorGridlines = True
        .HasMinorGridlines = False
    End With
    With ActiveChart.Axes(xlValue).MajorGridlines.Border
        .ColorIndex = 16
        .Weight = xlHairline
        .LineStyle = xlDot
    End With
    
    ' Iterate over each point of the first series:
    ' add a label, format marker style and color.
    Dim i As Integer
    With ActiveChart.SeriesCollection(1)
        .ApplyDataLabels
        For i = 1 To rows
            With .Points(i)
                .DataLabel.Text = labels.Cells(i, 1).Value2
                .MarkerStyle = pointSymbols.Cells(i, 1).Value2
                .MarkerBackgroundColorIndex = pointColors.Cells(i, 1).Value2
                .MarkerForegroundColorIndex = pointColors.Cells(i, 1).Value2
                .markerSize = sizeOfMarker
            End With
        Next
    End With
    
    End Sub
    Attached Files Attached Files
    Last edited by Rajoli; 09-27-2012 at 09:25 AM. Reason: Added model

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1