+ Reply to Thread
Results 1 to 7 of 7

Thread: Slow action in Worksheet_Change

  1. #1
    Registered User
    Join Date
    03-30-2010
    Location
    Warwickshire
    MS-Off Ver
    Excel 2003
    Posts
    17

    Slow action in Worksheet_Change

    Dear all,

    I have the following code in the Private Sub Worksheet_Change(ByVal Target As Range). All work well, but the rounding to 2 decimals places in column C slows down the process. Basically, if I enter an amount, the response is slow and if I delete an amount the cursor and the cell flicker for a few seconds.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    'Force uppercase input
    On Error Resume Next
    
    If Not (Application.Intersect(Target, Range("C6:J6")) Is Nothing) Then
        With Target
             If Not .HasFormula Then
                .Value = UCase(.Value)
            End If
        End With
    End If
    
    If Not (Application.Intersect(Target, Range("D10:Q300")) Is Nothing) Then
        With Target
             If Not .HasFormula Then
                .Value = UCase(.Value)
            End If
        End With
    End If
    
    'Force rounding to 2 decimal places
    On Error GoTo ws_exit
    Application.EnableEvents = False
        
    If Not (Application.Intersect(Target, Range("D10:D300")) Is Nothing) Then
        With Target
            If Not .HasFormula Then
                   .Value = Round(.Value, 2)
            End If
            If .Value = 0 Then
                   .Value = ""
            End If
        End With
    End If
    
    ws_exit:
        Application.EnableEvents = True
    
    ' Message if previous row is blank
    If Target.Column = 1 Then
        If ActiveCell.Value <> "" And ActiveCell.Offset(-1, 0).Value = "" Then
        MsgBox "La ligne précédente ne doit pas être vide. Veuillez modifier votre saisie s'il vous plait."
        End If
    End If
    
    End Sub
    Is there any way to avoid this? I had the coding to stop the flickering before but it makes the situation worse.

    Application.ScreenUpdating = False
    
    Application.ScreenUpdating = True
    Thank you very much for all your help.

    Regards,
    cdmg
    Last edited by cdmg; 10-29-2010 at 06:33 AM.

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Slow action in Worksheet_Change

    Turning screen updating off should help, switching calculation to manual will probably also make a difference:

    Dim myCalc
    
    myCalc = Application.Calculation
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    
    ' ...your code here
    
    Application.ScreenUpdating = True
    Application.Calculation = myCalc

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Slow action in Worksheet_Change

    I can't test this without a sample workbook, but this might be faster

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icalc As Long
    
    With Application
    icalc = .Calculation
    .Calculation = xlCalculationManual
    'Force uppercase input
    On Error Resume Next
    
    If Not (.Intersect(Target, Range("C6:J6")) Is Nothing) Then
        With Target
             If Not .HasFormula Then
                .Value = UCase(.Value)
            End If
        End With
    End If
    On Error GoTo ws_exit
    .EnableEvents = False
    
    If Not (.Intersect(Target, Range("D10:Q300")) Is Nothing) Then
        With Target
             If Not .HasFormula Then
             .Value = Round(.Value, 2)
             .Value = UCase(.Value)
                ElseIf .Value = 0 Then
                   .Value = ""
            End If
        End With
    End If
    
    ws_exit:
        .EnableEvents = True
        .Calculation = icalc
    End With
    ' Message if previous row is blank
    If Target.Column = 1 Then
        If ActiveCell.Value <> "" And ActiveCell.Offset(-1, 0).Value = "" Then _
        MsgBox "La ligne précédente ne doit pas être vide. Veuillez modifier votre saisie s'il vous plait."
    End If
    
    End Sub
    Last edited by royUK; 10-29-2010 at 04:57 AM.
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  4. #4
    Registered User
    Join Date
    03-30-2010
    Location
    Warwickshire
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Slow action in Worksheet_Change

    Thank you Dom but it doesn't speed things up unfortunately.

    RoyUK, your option works but it does not action the other requirements, i.e. forcing the uppercase input and when I delete data on the spreadsheet, the value is changed to 0.

    Regards,
    cdmg

  5. #5
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Slow action in Worksheet_Change

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  6. #6
    Registered User
    Join Date
    03-30-2010
    Location
    Warwickshire
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Slow action in Worksheet_Change

    Hi RoyUK, I have managed to adapt your coding to work with my spreadsheet and it works perfectly now (see below)! Thank you very,very much for your help. This website is great!

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icalc As Long
    
    With Application
    icalc = .Calculation
    .Calculation = xlCalculationManual
    
    'Force uppercase input
    On Error Resume Next
    
    If Not (.Intersect(Target, Range("C6:J6")) Is Nothing) Then
        With Target
             If Not .HasFormula Then
                .Value = UCase(.Value)
            End If
        End With
    End If
    
    If Not (.Intersect(Target, Range("E10:Q300")) Is Nothing) Then
        With Target
             If Not .HasFormula Then
                .Value = UCase(.Value)
            End If
        End With
    End If
    
    On Error GoTo ws_exit
    .EnableEvents = False
    
    'Force rounding to 2 decimal places
    If Not (.Intersect(Target, Range("D10:D300")) Is Nothing) Then
        With Target
             If Not .HasFormula Then
             .Value = Round(.Value, 2)
             End If
             If .Value = 0 Then
                   .Value = ""
            End If
        End With
    End If
    
    ws_exit:
        .EnableEvents = True
        .Calculation = icalc
    End With
    
    ' Message if previous row is blank
    If Target.Column = 1 Then
        If ActiveCell.Value <> "" And ActiveCell.Offset(-1, 0).Value = "" Then _
        MsgBox "La ligne précédente ne doit pas être vide. Veuillez modifier votre saisie s'il vous plait."
    End If
    
    End Sub
    Again, thank you very much for solving my problem.

    Kind regards,
    cdmg

  7. #7
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Slow action in Worksheet_Change

    Glad we helped

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ 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.2.0