+ Reply to Thread
Results 1 to 10 of 10

Excel code running too fast?

Hybrid View

  1. #1
    Registered User
    Join Date
    12-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    4

    Excel code running too fast?

    Hello, it appears I have a sub which may be running too fast. It works properly when using "Step Into". When running the entire sub I receive an error stating the chart has no title, and the line .ChartTitle.Characters.Text gets highlighted yellow. Please see below. Thanks for the help.

    Sub SampleCharts()
    Dim i As Integer
    
    For i = 1 To Worksheets.Count Step 2
    Worksheets(i).Activate
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
        ActiveChart.ChartArea.Select
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(1).Name = "Sample " & ((i + 1) / 2) & "A"
        ActiveChart.SeriesCollection(1).XValues = "='" & Worksheets(i).Name & "'!$B$5:$B$316"
        ActiveChart.SeriesCollection(1).Values = "='" & Worksheets(i).Name & "'!$C$5:$C$316"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(2).Name = "Sample " & ((i + 1) / 2) & "B"
        ActiveChart.SeriesCollection(2).XValues = "='" & Worksheets(i + 1).Name & "'!$B$5:$B$316"
        ActiveChart.SeriesCollection(2).Values = "='" & Worksheets(i + 1).Name & "'!$C$5:$C$316"
        
        'The following code sets every other data set to the same line color
        'c = (i Mod 2) * 200
        With ActiveChart.SeriesCollection(1).Format.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(200, 200, 15)
        .Transparency = 0
        End With
        '
        With ActiveChart.SeriesCollection(2).Format.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(200, 0, 15)
        .Transparency = 0
        End With
        '
            
        'Following Code Sets Axis Scales
        With ActiveChart.Axes(xlValue, xlPrimary)
        .HasTitle = True
        .AxisTitle.Characters.Text = "Torque (in-oz)"
        .MaximumScale = 14
        .MinimumScale = 0
        .MajorUnit = 2
        End With
        With ActiveChart.Axes(xlCategory, xlPrimary)
        .HasTitle = True
        .AxisTitle.Characters.Text = "Angle (Degrees)"
        .MaximumScale = 370
        .MinimumScale = 0
        .MajorUnit = 60
        End With
        ActiveChart.HasLegend = False
        '
        Dim cTitle As String
        Dim eTitle As String
        
        eTitle = CStr((i + 1) / 2)
        cTitle = "Sample "
        With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = cTitle & eTitle
        End With
        Next i
     End Sub
    Last edited by etacv; 01-20-2013 at 03:39 PM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Excel code running too fast?

    your code works on my test workbook, attach a sample file for testing
    If solved remember to mark Thread as solved

  3. #3
    Forum Contributor
    Join Date
    01-17-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    211

    Re: Excel code running too fast?

    Have seen this on this forum before , strange thing is this only make's issues in 2007. Everything seems to work fine if you take out ".Characters"

  4. #4
    Registered User
    Join Date
    12-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Excel code running too fast?

    Thanks for the replies. Attaching a workbook.
    Edit: removed attachment.

    Quote Originally Posted by hulpeloos View Post
    Have seen this on this forum before , strange thing is this only make's issues in 2007. Everything seems to work fine if you take out ".Characters"
    Oddly enough it makes it worse for me. Using .Characters it will go through 4-5 iterations of the loop. Without .Characters it will stall on the 2nd. And again, using Step Into through the Debug menu will work fine for the whole loop.
    Last edited by etacv; 01-20-2013 at 03:38 PM.

  5. #5
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Excel code running too fast?

    Hello hulpeloos,

    Waarom help met naam soos hulpeloos?

    Your contribution is very much appreciated.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  6. #6
    Forum Contributor
    Join Date
    01-17-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    211

    Re: Excel code running too fast?

    oh wait now i see something else .... add the red text as indicated below

     With ActiveChart
        .HasTitle = False
        .HasTitle = True
        .ChartTitle.Characters.Text = cTitle & eTitle
    End With

  7. #7
    Registered User
    Join Date
    12-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Excel code running too fast?

    Interesting. Why the need for that addition?

  8. #8
    Forum Contributor
    Join Date
    01-17-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    211

    Re: Excel code running too fast?

    at bottom of your code you have :
        Dim cTitle As String
        Dim eTitle As String
        
        eTitle = CStr((i + 1) / 2)
        cTitle = "Sample "
        With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = cTitle & eTitle
        End With
        Next i
     End Sub
    so before the
      .HasTitle = True
    you put
     .HasTitle = False

  9. #9
    Registered User
    Join Date
    12-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Excel code running too fast?

    I understand that. Just trying to determine the justification for adding it. Is that good practice in any code?

    Edit: It did work. Thank you for the help. Does it need the .HasTitle=False before .HasTitle=True because of the loop? So after the first iteration it already thinks it has a title active?

    Thanks!
    Last edited by etacv; 01-20-2013 at 03:36 PM.

  10. #10
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,448

    Re: Excel code running too fast?

    Look at the VBA call of

    DoEvents

    and put a few in your code to allow other things to happen on your machine to slow your code down.
    http://support.microsoft.com/kb/118468
    One test is worth a thousand opinions.
    Click the * Add Reputation below 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)

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