Automate sparkline creation (loop help!)

thebawp

New Member
Joined
Jun 19, 2009
Messages
14
I have a spreadsheet which consists of two sheets - a summary sheet and a data sheet. On the summary sheet I have a unique list of names that exist in the data sheet in column A, and in the data sheet, there are multiple rows per name (column A) and a number next to each one which denotes 'visits' (column B). Each name may have a different number of rows.

'Summary Sheet'

AB
1Name 1Sparkline (Name1)
2Name 2Sparkline (Name2)
3Name 3Sparkline (Name3)
4Name 4Sparkline (Name4)
5Name 5Sparkline (Name5)
6Name 6Sparkline (Name6)

<tbody>
</tbody>

'Data Sheet'

AB
1Name 12
2Name 13
3Name 14
4Name 21
5Name 32
6Name 31
7Name 43
8Name 41
9Name 56
10Name 57
11Name 510
12Name 61
13Name 62

<tbody>
</tbody>


I have created a named range for each name in the summary sheet which follows this format:
Name1
Code:
=INDEX('Data Sheet'!$B:$B,MATCH('Summary Sheet'!$A$1,'Data Sheet'!$A:$A,0)):INDEX('Data Sheet'!$B:$B,MATCH('Summary Sheet'!$A$1,'Data Sheet'!$A:$A,1))
Name2
Code:
=INDEX('Data Sheet'!$B:$B,MATCH('Summary Sheet'!$A$2,'Data Sheet'!$A:$A,0)):INDEX('Data Sheet'!$B:$B,MATCH('Summary Sheet'!$A$2,'Data Sheet'!$A:$A,1))
Each range is named 'name1', 'name2', 'name3' etc, and returns the data in range for each name (i.e. for Name 1 the range returned would be B1:B3 in the Data Sheet). I then use each named range to generate a sparkline for each name in the summary sheet.

What I would like to be able to do is to automate this process, as the data will change on a regular basis, and there could be different names etc - whilst in the dummy data I have restricted it to just 6 names, the actual data could have hundreds of names, so to do this manually just isn't practical. I think VBA could be utilised to create each range but I'd like this to be done dynamically (i.e. based on the number of names in the summary sheet). I also think there are a few parts to the process i.e:

1. Create a named range for each name in the Summary Sheet that references to the Data Sheet (possibly using the above formula format although if there's a better way I'm happy to use it!).

I think this could be done using a loop to increment the cell reference (i.e. from A1 to A2 to A3 etc) based on each name in the list, I always struggle with loops so I'm looking for a little assistance in this area! The basis of creating the named range would be:
Code:
Sub AddNamedRange()


ActiveWorkbook.Names.Add Name:="Name1", RefersTo:= _
        "=INDEX('Data Sheet'!$B:$B,MATCH('Summary Sheet'!$A$1,'Data Sheet'!$A:$A,0)):INDEX('Data Sheet'!$B:$B,MATCH('Summary Sheet'!$A$1,'Data Sheet'!$A:$A,1))"
End Sub


2. Create a sparkline group in column B in the Summary Sheet for each unique name that uses the created named ranges specific to those names. The VBA for this seems quite simple if you know the range and you know the named ranges, but I'm not sure how I could feed the information from step 1 (i.e. all the created named ranges) into step 2, so again some assistance would be much appreciated!


Code:
Sub CreateSparkline ()    Range("$B$1:$B$5").SparklineGroups.Add Type:=xlSparkLine, SourceData:= _
        "Name1, Name2, Name3, Name4, Name5"
End Sub

I hope that all makes sense! Writing it out has given me some further thoughts so I will attempt to update the thread if I make any progress. Thanks :)
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

thebawp

New Member
Joined
Jun 19, 2009
Messages
14
In regards to part 1, I have managed to create a macro with some VBA I found online (which I have promptly lost the address of!).

Code:
Sub CreateMultipleNamedRangesBasedOnList()

Dim srcrng As Range
Dim namedref As String
Dim rng As Range
Dim RangeName As String
Dim i As Integer
Dim ii As Integer


i = 1

Worksheets("Summary Sheet").Activate
Set srcrng = Worksheets("Summary Sheet").Range("A2", ActiveSheet.Range("A2").End(xlDown))

    For Each cell In srcrng
        RangeName = "Name" & i
        namedref = "=INDEX('Data Sheet'!$B:$B,MATCH('Summary Sheet'!$A$" & i & ",'Data Sheet'!$A:$A,0)):INDEX('Data Sheet'!$B:$B,MATCH('Summary Sheet'!$A$" & i & ",'Data Sheet'!$A:$A,1))"
        
        Worksheets("Summary Sheet").Names.Add Name:=RangeName, RefersTo:=namedref
        
        i = i + 1
        
    Next cell
    
    MsgBox "Ranges Created."
    
End Sub

This creates all of the named ranges needed for part 2.
 
Last edited:

thebawp

New Member
Joined
Jun 19, 2009
Messages
14
Well with a little persistence I think I've solved this, it may be helpful for someone else in the future so here is the VBA used.

Code:
Sub Generate_Sparklines()

'This macro identifies and creates named ranges for each name present in the summary sheet based on
'the data in the data sheet and then uses this information to create a sparkline for each name in the summary list
Dim srcrng As Range
Dim DataRange As Range
Dim namedref As String
Dim RangeName As String
Dim myString As String
Dim i As Integer
Dim ii As Integer


'i is a counter used to generate named range names i.e. name1, name2, name3 etc
i = 1
'ii is a reference to the starting cell for the namedref formula i.e. the first formula makes reference to A2
ii = 2


Worksheets("Summary Sheet").Activate


'identifies the range of names
Set srcrng = Worksheets("Summary Sheet").Range("A2", ActiveSheet.Range("A2").End(xlDown))


    For Each cell In srcrng
    
        RangeName = "Name" & i


        namedref = "=INDEX('Data Sheet'!$B:$B,MATCH('Summary Sheet'!$A$" & ii & ",'Data Sheet'!$A:$A,0)):INDEX('Data Sheet'!$B:$B,MATCH('Summary Sheet'!$A$" & ii & ",'Data Sheet'!$A:$A,1))"
        
        Worksheets("Summary Sheet").Names.Add Name:=RangeName, RefersTo:=namedref
        
        i = i + 1
        ii = ii + 1
        
    Next cell
    
    'Adds named ranges to workbook in order to create sparklines
    
    Range("L1").Select
    Selection.ListNames
    Range("L1").Select
    
    'Pickes up named ranges
    Set DataRange = Worksheets("Summary Sheet").Range("L1", ActiveSheet.Range("L1").End(xlDown))


    'Loop through each named range in created list and stores to a string
    For Each cell In DataRange.Cells
        myString = myString & "," & cell.Value
    Next cell


    'Remove first delimiter from string (;|;)
    myString = Right(myString, Len(myString) - 1)
    
    'Creates sparklines group for all names
    srcrng.Offset(ColumnOffset:=1).SparklineGroups.Add Type:=xlSparkLine, SourceData:=myString
    
    'Clears contents from L:M (named ranges printed in workbook -no longer required)
    Columns("L:M").ClearContents
    
    Range("A1").Select
    
    MsgBox "Sparklines Created."
    
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,127,100
Messages
5,622,725
Members
415,924
Latest member
togo

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
Top