+ Reply to Thread
Results 1 to 5 of 5

VBA Macro erroring out with example code / document

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    VBA Macro erroring out with example code / document

    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
    Attached Files Attached Files
    Last edited by Hyflex; 02-25-2014 at 08:04 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Macro erroring out with example code / document

    Hi,

    Any chance that
    GroupRange = Range(GetTopRange(Range("C3:C" & LastRow), Location, 10)).Offset(0, 6)
    should actually say
    GroupRange = Range(GetTopRange(Range("C4:C" & LastRow), Location, 10)).Offset(0, 6)
    ???

    Stepping through the code, the first location is "London", but the first row that is being looked at is the header row (row 3). And changing the 3 to a 4 does get the code past this point.

    Some other changes you will need to make:
    1. In the "CreateCharts" subroutine, you will need to change GroupRange = to Set GroupRange =, and similarly for ValueRange = on the next line.
    2. In the "AddChart" subroutine, you will have to add your series manually. The reason for this is because when using SetSourceData in your case, Excel is unable to recognise chartable data in the range that you have given it, so it creates the chart without an initial series. This also means the chart doesn't have any other features either... no plot area, no legend, and no placeholder for a title. So the code errors out when you try to set the title, because the title placeholder doesn't exist. You can change this by manually adding each series in VBA rather than trying to set the source data.

    I hope this has helped

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,663

    Re: VBA Macro erroring out with example code / document

    There's something wrong with the structure of your IF..Then.. Else blocks; however, notice
    (1) that URng is never initialized
    (2) the first time through the loop, Cell.Value= "Location" but StrLine="London" therefore, the test evaluates to False and immediately exits to GetTopRange = URng.Address [see (1)]

    
    Function GetTopRange(ByRef Rng As Range, ByVal StrLine As String, ByVal 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     'always
                    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
    Last edited by protonLeah; 02-25-2014 at 03:40 AM.
    Ben Van Johnson

  4. #4
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Re: VBA Macro erroring out with example code / document

    Thanks guys, the fixed code is:

    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("AC3:AC" & LastRow) = Range("C3:C" & LastRow).Value
        Range("$AC$3:$AC$" & LastRow).RemoveDuplicates Columns:=1, Header:=xlYes
        LastRowAC = Range("AC65536").End(xlUp).Row
        
        For i = 4 To LastRowAC
            Sheets("MainSheet").Activate
            Location = Range("AC" & i).Value
            Set GroupRange = Range(GetTopRange(Range("C4:C" & LastRow), Location, 10)).Offset(0, 6)
            Set ValueRange = GroupRange.Offset(0, 11)
            
            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

  5. #5
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA Macro erroring out with example code / document

    No worries.

    Please don't forget to mark this thread as solved and please click on the * next to my post to say thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro Code for multi-user .xlsm document
    By maheshle in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-27-2012, 03:20 PM
  2. Print macro erroring if there are hidden sheets
    By davidjoe in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-03-2012, 03:51 PM
  3. add something to m code to keep from erroring
    By daniels012 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-28-2009, 09:55 AM
  4. Basic Macro (Jump to Cell) erroring out
    By kethyar in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-12-2008, 06:54 PM
  5. [SOLVED] VBA code erroring in XL97...
    By sharpie23 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-15-2006, 03:30 PM

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