+ Reply to Thread
Results 1 to 5 of 5

Macro has a bug when producing a graph

Hybrid View

  1. #1
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    21

    Macro has a bug when producing a graph

    Hello, I have got well under way with help from many of you in producing my macro to do some processing on data files. I got the code sorted to select data and produce a graph. I copied and modified this code to then select different data and produce another graph......that worked ok. I then copied and modified to produce a third graph and a bug cropped up when running the macro. It looks like the graph was created as I wanted by I got error message "The index into the specified collection is out of bounds"

    At the moment after running the macro the three graphs are on top of each other, you can drag to separate which I will try and do further in the macro. The graphs don't look too meaningful but that's only because I have deleted many rows of data to keep under the maximum filesize for upload here.

    Once someone kindly replies to a post I am aware that the post needs marking as resolved. I have also been giving positive feedback to the person who has helped too. Is it etiquette to also reply to the thread to say " thanks that works great" or similar?

    Please find the code and excel file attached, thanks.



    Sub Macro1()
    '
    ' Macro1 Macro
    
    ' This copies all the data from sheet 1 to a new sheet, then filters it and formats it to show all the downlinks sent to the tool
        Sheets("Sheet1").Select
        Sheets("Sheet1").Copy After:=Sheets(1)
        Sheets("Sheet1 (2)").Select
        Sheets("Sheet1 (2)").Name = "Downlinks"
        Cells.Select
        Cells.EntireColumn.AutoFit
        Rows("16:16").Select
        Selection.Delete Shift:=xlUp
        Rows("1:14").Select
        Selection.EntireRow.Hidden = True
        Rows("15:15").Select
        Selection.AutoFilter
        ActiveWindow.ScrollColumn = 2
        ActiveWindow.ScrollColumn = 3
        ActiveWindow.ScrollColumn = 4
        ActiveWindow.ScrollColumn = 5
        ActiveWindow.ScrollColumn = 6
        ActiveWindow.ScrollColumn = 7
        ActiveWindow.ScrollColumn = 8
        ActiveWindow.ScrollColumn = 9
        ActiveWindow.ScrollColumn = 10
        ActiveWindow.ScrollColumn = 11
        ActiveWindow.ScrollColumn = 12
        ActiveWindow.ScrollColumn = 13
        ActiveWindow.ScrollColumn = 14
        ActiveWindow.ScrollColumn = 15
        ActiveWindow.ScrollColumn = 16
        ActiveWindow.ScrollColumn = 17
        ActiveWindow.ScrollColumn = 18
        ActiveWindow.ScrollColumn = 19
        ActiveWindow.ScrollColumn = 20
        ActiveWindow.ScrollColumn = 21
        ActiveWindow.ScrollColumn = 22
        ActiveWindow.ScrollColumn = 23
        ActiveWindow.ScrollColumn = 24
        ActiveWindow.ScrollColumn = 25
        ActiveWindow.ScrollColumn = 26
        ActiveWindow.ScrollColumn = 27
        ActiveWindow.ScrollColumn = 28
        ActiveWindow.ScrollColumn = 29
        ActiveSheet.Range("$A$15:$AR$3676").AutoFilter Field:=40, Criteria1:="<>"
        Columns("F:AM").Select
        Range("AM15").Activate
        Selection.EntireColumn.Hidden = True
        ActiveWindow.ScrollColumn = 5
        ActiveWindow.ScrollColumn = 4
        ActiveWindow.ScrollColumn = 3
        ActiveWindow.ScrollColumn = 2
        ActiveWindow.ScrollColumn = 1
        Columns("A:D").Select
        Range("D15").Activate
        Selection.EntireColumn.Hidden = True
        Columns("E:E").ColumnWidth = 22.29
        Columns("E:E").Select
        Range("E15").Activate
        With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Range("AS15").Select
        Sheets("Sheet1").Select
    
    ' Formats column E to a different date / time format so it appears better on the x axis of the graphs
    Columns("E:E").Select
        Selection.NumberFormat = "dd/mmm hh:mm"
        Range("E1").Select
        
        
    'Selects Column E (date / time) and Column P (subtwist) to the last row of data and back upto row 17. This selection is in preparation for creating a graph of
    'Subtwist and time.
    Dim lr As Long
    Dim rngAllData As Range
    
    lr = Worksheets("Sheet1").Cells(Rows.Count, "P").End(xlUp).Row
    
    Set rngAllData = Union(Worksheets("Sheet1").Range("E17:E" & lr), Worksheets("Sheet1").Range("P17:P" & lr))
    rngAllData.Select
    
    ' inserts a line graph with markers to graph subtwist over time, scales the size of the graph to suit the page too.
    ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
    
    
        ActiveChart.SetSourceData Source:=Range("Sheet1!E17:E" & lr & ",Sheet1!P17:P" & lr)
        ActiveSheet.Shapes("Chart 1").ScaleWidth 1.8520833333, msoFalse, _
            msoScaleFromBottomRight
        ActiveSheet.Shapes("Chart 1").ScaleHeight 1.0364585156, msoFalse, _
            msoScaleFromTopLeft
        ActiveSheet.Shapes("Chart 1").ScaleWidth 1.4308211474, msoFalse, _
            msoScaleFromTopLeft
        ActiveSheet.Shapes("Chart 1").ScaleHeight 0.9832499359, msoFalse, _
            msoScaleFromTopLeft
        ActiveChart.ChartTitle.Select
        ActiveChart.ChartTitle.Text = "Subtwist"
        Selection.Format.TextFrame2.TextRange.Characters.Text = "Subtwist"
        With Selection.Format.TextFrame2.TextRange.Characters(1, 8).ParagraphFormat
            .TextDirection = msoTextDirectionLeftToRight
            .Alignment = msoAlignCenter
        End With
        With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
            .BaselineOffset = 0
            .Bold = msoFalse
            .NameComplexScript = "+mn-cs"
            .NameFarEast = "+mn-ea"
            .Fill.Visible = msoTrue
            .Fill.ForeColor.RGB = RGB(89, 89, 89)
            .Fill.Transparency = 0
            .Fill.Solid
            .Size = 14
            .Italic = msoFalse
            .Kerning = 12
            .Name = "+mn-lt"
            .UnderlineStyle = msoNoUnderline
            .Spacing = 0
            .Strike = msoNoStrike
        End With
        ActiveChart.ChartArea.Select
    
    'Selects Column E (date / time) and Column J (Vibration) to the last row of data and back upto row 17. This selection is in preparation for creating a graph of
    'Vibration and time.
    Dim lr2 As Long
    Dim rngAllData2 As Range
    
    lr2 = Worksheets("Sheet1").Cells(Rows.Count, "J").End(xlUp).Row
    
    Set rngAllData2 = Union(Worksheets("Sheet1").Range("E17:E" & lr2), Worksheets("Sheet1").Range("J17:J" & lr2))
    rngAllData2.Select
    
    ' Adds a graph (graph 2) for vibration and scales it to suit the page size.
    
    ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
    
    
        ActiveChart.SetSourceData Source:=Range("Sheet1!E17:E" & lr2 & ",Sheet1!J17:J" & lr2)
        ActiveSheet.Shapes("Chart 2").ScaleWidth 1.8520833333, msoFalse, _
            msoScaleFromBottomRight
        ActiveSheet.Shapes("Chart 2").ScaleHeight 1.0364585156, msoFalse, _
            msoScaleFromTopLeft
        ActiveSheet.Shapes("Chart 2").ScaleWidth 1.4308211474, msoFalse, _
            msoScaleFromTopLeft
        ActiveSheet.Shapes("Chart 2").ScaleHeight 0.9832499359, msoFalse, _
            msoScaleFromTopLeft
        ActiveChart.ChartTitle.Select
        ActiveChart.ChartTitle.Text = "Vibration"
        Selection.Format.TextFrame2.TextRange.Characters.Text = "Vibration"
        With Selection.Format.TextFrame2.TextRange.Characters(1, 8).ParagraphFormat
            .TextDirection = msoTextDirectionLeftToRight
            .Alignment = msoAlignCenter
            
        'With Selection.Format.TextFrame2.TextRange.Characters(1, 8).ParagraphFormat
            
        End With
        With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
            .BaselineOffset = 0
            .Bold = msoFalse
            .NameComplexScript = "+mn-cs"
            .NameFarEast = "+mn-ea"
            .Fill.Visible = msoTrue
            .Fill.ForeColor.RGB = RGB(89, 89, 89)
            .Fill.Transparency = 0
            .Fill.Solid
            .Size = 14
            .Italic = msoFalse
            .Kerning = 12
            .Name = "+mn-lt"
            .UnderlineStyle = msoNoUnderline
            .Spacing = 0
            .Strike = msoNoStrike
        End With
        ActiveChart.ChartArea.Select
    '
    
    'Selects Column E (date / time) and Column R (Cmd TF) to the last row of data and back upto row 17. This selection is in preparation for creating a graph of
    'CmdTF and time.
    Dim lr3 As Long
    Dim rngAllData3 As Range
    
    lr3 = Worksheets("Sheet1").Cells(Rows.Count, "R").End(xlUp).Row
    
    Set rngAllData3 = Union(Worksheets("Sheet1").Range("E17:E" & lr3), Worksheets("Sheet1").Range("R17:R" & lr3))
    rngAllData3.Select
    
    ' Adds a graph (graph 3) for vibration and scales it to suit the page size.
    
    ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
    
    
        ActiveChart.SetSourceData Source:=Range("Sheet1!E17:E" & lr3 & ",Sheet1!R17:R" & lr3)
        ActiveSheet.Shapes("Chart 3").ScaleWidth 1.8520833333, msoFalse, _
            msoScaleFromBottomRight
        ActiveSheet.Shapes("Chart 3").ScaleHeight 1.0364585156, msoFalse, _
            msoScaleFromTopLeft
        ActiveSheet.Shapes("Chart 3").ScaleWidth 1.4308211474, msoFalse, _
            msoScaleFromTopLeft
        ActiveSheet.Shapes("Chart 3").ScaleHeight 0.9832499359, msoFalse, _
            msoScaleFromTopLeft
        ActiveChart.ChartTitle.Select
        ActiveChart.ChartTitle.Text = "CmdTF"
        Selection.Format.TextFrame2.TextRange.Characters.Text = "CmdTF"
        With Selection.Format.TextFrame2.TextRange.Characters(1, 8).ParagraphFormat
            .TextDirection = msoTextDirectionLeftToRight
            .Alignment = msoAlignCenter
        End With
        With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
            .BaselineOffset = 0
            .Bold = msoFalse
            .NameComplexScript = "+mn-cs"
            .NameFarEast = "+mn-ea"
            .Fill.Visible = msoTrue
            .Fill.ForeColor.RGB = RGB(89, 89, 89)
            .Fill.Transparency = 0
            .Fill.Solid
            .Size = 14
            .Italic = msoFalse
            .Kerning = 12
            .Name = "+mn-lt"
            .UnderlineStyle = msoNoUnderline
            .Spacing = 0
            .Strike = msoNoStrike
        End With
        ActiveChart.ChartArea.Select
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,447

    Re: Macro has a bug when producing a graph

    the error is related to the text formatting of a number of characters that is greater than the text length
    you had 8 character length when the text is only 5 long

        Selection.Format.TextFrame2.TextRange.Characters.Text = "CmdTF"
        With Selection.Format.TextFrame2.TextRange.Characters(1, 5).ParagraphFormat
            .TextDirection = msoTextDirectionLeftToRight
            .Alignment = msoAlignCenter
        End With
        With Selection.Format.TextFrame2.TextRange.Characters(1, 5).Font
    you can add code to position chart objects to avoid the overlay problem.

    It's always good to have the OP acknowledge the reply has worked either by seeing the SOLVED status or a last post comment
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    21

    Re: Macro has a bug when producing a graph

    Worked a treat thanks Andy, what can I check for to look what character length something is ready for when I do the next graph, Im not sure what the 1,8 or 1,5 relates to in my data? Cheers Dominic

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,447

    Re: Macro has a bug when producing a graph

    if you want it to apply to all the characters in the text then you don't need a start and length value

        With Selection.Format.TextFrame2.TextRange.Characters.ParagraphFormat
            .TextDirection = msoTextDirectionLeftToRight
            .Alignment = msoAlignCenter
        End With

  5. #5
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Office 365
    Posts
    21

    Re: Macro has a bug when producing a graph

    Perfect, covers all bases now, thanks very much :-)

+ 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. My Macro is not producing the correct output. Help please!
    By ioecho in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-03-2015, 12:54 PM
  2. Problems Producing a Stack Graph
    By masond3 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 10-04-2013, 07:34 AM
  3. Macro no longer producing desired data
    By xforum142riidax in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-20-2011, 03:25 PM
  4. Macro no longer producing desired data
    By xforum142riidax in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-20-2011, 01:19 PM
  5. recorded macro for pagesetup producing error
    By suprme32excel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-27-2009, 05:51 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