Hey ExcelForum,
My following code below keeps erroring out at the line: GetTopRange = URng.Address but I can't figure out why and what's going wrong, I've attached an example below please see
Sub CreateCharts()
Dim Location As String
Dim GroupRange As Range, ValueRange As Range
Dim LastRow As Long, LastRowAC As Long
LastRow = Range("C65536").End(xlUp).Row
Range("AC4:AC" & LastRow) = Range("C4:C" & LastRow).Value
Range("$AC$3:$AC$" & LastRow).RemoveDuplicates Columns:=1, Header:=xlYes
LastRow = Range("K65536").End(xlUp).Row
LastRowAC = Range("AC65536").End(xlUp).Row
For i = 4 To LastRowAC
Location = Range("AC" & i).Value
GroupRange = Range(GetTopRange(Range("C3:C" & LastRow), Location, 10)).Offset(0, 6)
ValueRange = GroupRange.Offset(0, 17)
AddChart GroupRange, ValueRange, Location
Next i
End Sub
Sub AddChart(rLabel As Range, rValues As Range, sTitle As String)
Dim Cht As Chart
Set Cht = Charts.Add
With Cht
.Name = sTitle
.ChartType = xlPie
.SetSourceData Source:=Union(rLabel, rValues)
.HasTitle = True
.ChartTitle.Characters.Text = sTitle
End With
End Sub
Function GetTopRange(Rng As Range, StrLine As String, NumCount As Long) As String
Application.Volatile
Dim Cell As Range, URng As Range
For Each Cell In Rng.SpecialCells(xlCellTypeConstants)
If Cell.Value = StrLine Then
If URng Is Nothing Then
Set URng = Cell
Else
Set URng = Union(URng, Cell)
End If
If URng.Cells.Count = NumCount Then
Exit For
End If
End If
Next Cell
GetTopRange = URng.Address
End Function
Hopefully someone can help me with my problem.
Thanks
Bookmarks