+ Reply to Thread
Results 1 to 19 of 19

VBA optimization

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-21-2016
    Location
    UK
    MS-Off Ver
    2016
    Posts
    131

    VBA optimization

    Hello all,

    Could you please help me optimizing VBA?

    I got series of VBA scripts running every hour and I think it can be optimized as I have seen excel being not responded for over 12 minutes so I believe it just needs optimization. Please see code below:

    Sub clear_sheets()
    
        Sheets("S01").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        
        Sheets("S02").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        
        Sheets("S03").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        
        Sheets("S04").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        
        Sheets("S05").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        
        Sheets("S06").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        
        Sheets("S07").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        
        Sheets("S08").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
    
        Sheets("S09").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        
        Sheets("S10").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        
        Sheets("S11").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
        
        Sheets("MENU").Select
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA optimization

    Hi !

    This bad code (full of Select, Selection, awfull ! ) ends in tenth of a second !

    So if this code runs over 12 minutes, the truth is elsewhere

  3. #3
    Forum Contributor
    Join Date
    09-21-2016
    Location
    UK
    MS-Off Ver
    2016
    Posts
    131

    Re: VBA optimization

    Quote Originally Posted by Marc L View Post
    Hi !

    This bad code (full of Select, Selection, awfull ! ) ends in tenth of a second !

    So if this code runs over 12 minutes, the truth is elsewhere
    No, this is only the part of a code that I'm asking you guys to optimize.

  4. #4
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: VBA optimization

    Sub clear_sheets()
    Dim sh As Sheets
    Application.caluculation = xlManual
    For Each sh In Sheets
    If Left(sh.Name, 1) = "s" Then sh.Range("A3:C3").ClearContents
      Next
        
        Application.caluculation = xlAutomatic
        
        Sheets("MENU").Select
    
    End Sub

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,924

    Re: VBA optimization

    I'd do something like below.

    Sub clear_sheets()
    Dim ws As Worksheet
    
    OptimizeVBA True
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name Like "S??" Then
            ws.Range("A3:C" & ws.Cells(Rows.Count, "C").End(xlUp).Row).ClearContents
        End If
    Next ws
    
    OptimizeVBA False
    
    End Sub
    
    Sub OptimizeVBA(isOn As Boolean)
        Application.Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic)
        Application.ScreenUpdating = Not (isOn)
        Application.EnableEvents = Not (isOn)
    End Sub
    OptimizeVBA is trick I learned at http://analystcave.com/

  6. #6
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    274

    Re: VBA optimization

    Try worksheets ("S11").rows("1:" & rows.count).clearcontents for each sheet.

  7. #7
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: VBA optimization

    The previous reply indicates, the code you provided would in no way run for 12 minutes, there must be some other code that is kicking in that is causing your code to run for a long time, possibly a wack of formulas could be causing the issue. Then set your calculate to manual.

    Anyway, you can get rid of your selects,

    Sheets("S01").Select
        Range("A3:C3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.ClearContents
    Can be modified to possibly
    with Sheets("S01")
        .Range(.Range("A3"), .Range("C3").End(xlDown)).ClearContents
     end with
    Last edited by davesexcel; 12-13-2016 at 02:12 PM.

  8. #8
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VBA optimization

    
    
    Sub clear_sheets()
    
    Optimise (True)
    
    For Count = 1 To 11
        
        Sheets("S0" & Count).Range("A3:C3", Range("A3:C3").End(xlDown)).ClearContents
        
    Next
        Sheets("MENU").Select
    Optimise (False)
    
    End Sub
    
    Sub Optimise(Flag As Boolean)
    SFlag = Not (Flag)
    
    Application.ScreenUpdating = SFlag
    Application.DisplayAlerts = SFlag
    Application.EnableEvents = SFlag
    Application.DisplayStatusBar = SFlag
    ActiveSheet.DisplayPageBreaks = SFlag
    
    If Flag = True Then
    Application.Calculation = xlCalculationManual
    Else
    Application.Calculation = xlCalculationAutomatic
    End If
    
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  9. #9
    Forum Contributor
    Join Date
    09-21-2016
    Location
    UK
    MS-Off Ver
    2016
    Posts
    131

    Re: VBA optimization

    Quote Originally Posted by mehmetcik View Post
    
    
    Sub clear_sheets()
    
    Optimise (True)
    
    For Count = 1 To 11
        
        Sheets("S0" & Count).Range("A3:C3", Range("A3:C3").End(xlDown)).ClearContents
        
    Next
        Sheets("MENU").Select
    Optimise (False)
    
    End Sub
    
    Sub Optimise(Flag As Boolean)
    SFlag = Not (Flag)
    
    Application.ScreenUpdating = SFlag
    Application.DisplayAlerts = SFlag
    Application.EnableEvents = SFlag
    Application.DisplayStatusBar = SFlag
    ActiveSheet.DisplayPageBreaks = SFlag
    
    If Flag = True Then
    Application.Calculation = xlCalculationManual
    Else
    Application.Calculation = xlCalculationAutomatic
    End If
    
    End Sub

    What if I would like to perform the same task but across sheets that are named differently. For example "AMPL" YEHK" "UIOS". Would it be possible to perform the task as efficiently as if a ran on sheets named S0*

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA optimization


    Yes for example if their indexes are closed like worksheet #4 to #6 …

    Or like post #7 …

  11. #11
    Forum Contributor
    Join Date
    09-21-2016
    Location
    UK
    MS-Off Ver
    2016
    Posts
    131

    Re: VBA optimization

    Quote Originally Posted by Marc L View Post

    Yes for example if their indexes are closed like worksheet #4 to #6 …

    Or like post #7 …
    Which option is quicker to run? My project is still in early stages so i'm able to change the structure of it.

  12. #12
    Forum Contributor
    Join Date
    09-21-2016
    Location
    UK
    MS-Off Ver
    2016
    Posts
    131

    Re: VBA optimization

    Quote Originally Posted by mehmetcik View Post
    Sub clear_sheets()
    
    Optimise (True)
    
    SNames = Split("AMPL, YEHK, "UIOS,"AMPL, YEHK, "UIOS, AMPL, YEHK, UIOS",", ")
    
    For Count =0  To Ubound(SNames)
        
        Sheets(Sname(Count)).Range("A3:C3", Range("A3:C3").End(xlDown)).ClearContents
        
    Next
        Sheets("MENU").Select
    Optimise (False)
    
    End Sub
    
    Sub Optimise(Flag As Boolean)
    SFlag = Not (Flag)
    
    Application.ScreenUpdating = SFlag
    Application.DisplayAlerts = SFlag
    Application.EnableEvents = SFlag
    Application.DisplayStatusBar = SFlag
    ActiveSheet.DisplayPageBreaks = SFlag
    
    If Flag = True Then
    Application.Calculation = xlCalculationManual
    Else
    Application.Calculation = xlCalculationAutomatic
    End If
    
    End Sub
    Im trying to run this code below, but it doesnt seem to work. Compiler highlighting line 5.

    Sub clear_sheets()
    
    Optimise (True)
    
    Snames = Split(AA, BB, CC)
    
    For Count = 3 To UBound(Snames)
        
        Sheets(Snames(Count)).Range("A3:C3", Range("A3:C3").End(xlDown)).ClearContents
        
    Next
        Sheets("MENU").Select
    Optimise (False)
    
    End Sub
    
    Sub Optimise(Flag As Boolean)
    SFlag = Not (Flag)
    
    Application.ScreenUpdating = SFlag
    Application.DisplayAlerts = SFlag
    Application.EnableEvents = SFlag
    Application.DisplayStatusBar = SFlag
    ActiveSheet.DisplayPageBreaks = SFlag
    
    If Flag = True Then
    Application.Calculation = xlCalculationManual
    Else
    Application.Calculation = xlCalculationAutomatic
    End If
    
    End Sub
    Last edited by ChipsSlave; 12-13-2016 at 04:31 PM.

  13. #13
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: VBA optimization

    Your range has to be qualified as well
    Sheets(Snames(Count)).Range("A3:C3").End(xlDown)
    Look at my original code, that is why I used with and end with

  14. #14
    Forum Contributor
    Join Date
    09-21-2016
    Location
    UK
    MS-Off Ver
    2016
    Posts
    131

    Re: VBA optimization

    Quote Originally Posted by davesexcel View Post
    Your range has to be qualified as well
    Sheets(Snames(Count)).Range("A3:C3").End(xlDown)
    Look at my original code, that is why I used with and end with
    I'm sorry but I don't follow. Could you please explain me this a bit more.

  15. #15
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VBA optimization

    Sub clear_sheets()
    
    Optimise (True)
    
    SNames = Split("AMPL, YEHK, "UIOS,"AMPL, YEHK, "UIOS, AMPL, YEHK, UIOS",", ")
    
    For Count =0  To Ubound(SNames)
        
        Sheets(Sname(Count)).Range("A3:C3", Range("A3:C3").End(xlDown)).ClearContents
        
    Next
        Sheets("MENU").Select
    Optimise (False)
    
    End Sub
    
    Sub Optimise(Flag As Boolean)
    SFlag = Not (Flag)
    
    Application.ScreenUpdating = SFlag
    Application.DisplayAlerts = SFlag
    Application.EnableEvents = SFlag
    Application.DisplayStatusBar = SFlag
    ActiveSheet.DisplayPageBreaks = SFlag
    
    If Flag = True Then
    Application.Calculation = xlCalculationManual
    Else
    Application.Calculation = xlCalculationAutomatic
    End If
    
    End Sub

  16. #16
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VBA optimization

    Sorry, this version works.


    
    Sub clear_sheets()
    
    Optimise (True)
    
    Snames = Split("AMPL, YEHK, UIOS, AMPL, YEHK, UIOS, AMPL, YEHK, UIOS", ", ")
    
    For Count = 0 To UBound(Snames)
    MyRange = Range("A3:C3", Range("A3:C3").End(xlDown)).Address
        
        Sheets(Snames(Count)).Range(MyRange).ClearContents
        
    Next
        Sheets("MENU").Select
    Optimise (False)
    
    End Sub
    
    Sub Optimise(Flag As Boolean)
    SFlag = Not (Flag)
    
    Application.ScreenUpdating = SFlag
    Application.DisplayAlerts = SFlag
    Application.EnableEvents = SFlag
    Application.DisplayStatusBar = SFlag
    ActiveSheet.DisplayPageBreaks = SFlag
    
    If Flag = True Then
    Application.Calculation = xlCalculationManual
    Else
    Application.Calculation = xlCalculationAutomatic
    End If
    
    End Sub

  17. #17
    Forum Contributor
    Join Date
    09-21-2016
    Location
    UK
    MS-Off Ver
    2016
    Posts
    131

    Re: VBA optimization

    Quote Originally Posted by mehmetcik View Post
    Sorry, this version works.


    
    Sub clear_sheets()
    
    Optimise (True)
    
    Snames = Split("AMPL, YEHK, UIOS, AMPL, YEHK, UIOS, AMPL, YEHK, UIOS", ", ")
    
    For Count = 0 To UBound(Snames)
    MyRange = Range("A3:C3", Range("A3:C3").End(xlDown)).Address
        
        Sheets(Snames(Count)).Range(MyRange).ClearContents
        
    Next
        Sheets("MENU").Select
    Optimise (False)
    
    End Sub
    
    Sub Optimise(Flag As Boolean)
    SFlag = Not (Flag)
    
    Application.ScreenUpdating = SFlag
    Application.DisplayAlerts = SFlag
    Application.EnableEvents = SFlag
    Application.DisplayStatusBar = SFlag
    ActiveSheet.DisplayPageBreaks = SFlag
    
    If Flag = True Then
    Application.Calculation = xlCalculationManual
    Else
    Application.Calculation = xlCalculationAutomatic
    End If
    
    End Sub
    I can confirm it works. Thank you for your support.

  18. #18
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,982

    Re: VBA optimization

    @ChipsSlave

    Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    For normal conversational replies, try using the QUICK REPLY box below.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  19. #19
    Forum Contributor
    Join Date
    09-21-2016
    Location
    UK
    MS-Off Ver
    2016
    Posts
    131

    Re: VBA optimization

    Quote Originally Posted by mehmetcik View Post
    Sorry, this version works.
    I'm sorry for bugging you, but could you show me how do I optimize this code?

    It should filter table COMP_summ_9 by DTTD, FDTL,... and then select A2:C2 ctrl+shift down and copy selection to the DTTD,FDTL,... sheets and paste it into A3 cell. I was able to optimize other bits of the code just this one seems a little bit to difficult to me as I am not an expert of VBA. Thank you.

    Sub distribute_dsp_data_9()
    
        Sheets("raw_data_1_9").Visible = True
    
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="DTTD"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("DTTD").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="FDTL"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("FDTL").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="FULL"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("FUL ON").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="GSSL"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("GSSL").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="MCHL"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("MCHL").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="NGC"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("NGC").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="PCSL"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("PCSL").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="PC"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("PRO").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="TSL"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("TSL").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="UKED"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("UKED").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2, Criteria1 _
            :="WCDL"
        Range("C2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Selection.Copy
        Sheets("WCDL").Select
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
        Sheets("raw_data_1_9").Select
        ActiveSheet.ListObjects("COMP_summ_9").Range.AutoFilter Field:=2
    
        Sheets("raw_data_1_9").Visible = False
    
    End Sub

+ 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] sumproduct optimization
    By miller_ilya in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 09-18-2013, 09:23 AM
  2. Warehouse Optimization
    By Matt Lee in forum Excel General
    Replies: 8
    Last Post: 12-29-2010, 05:14 PM
  3. Optimization with solver
    By Homeboy_8 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-15-2010, 03:15 AM
  4. cutlength optimization
    By EMAN in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2010, 11:59 PM
  5. Optimization Question
    By Multistrada in forum Excel General
    Replies: 3
    Last Post: 11-08-2010, 12:10 PM
  6. Code optimization
    By d_omin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-27-2010, 04:13 AM
  7. I need of optimization -Please help!
    By Mm73 in forum Excel General
    Replies: 1
    Last Post: 08-25-2008, 02:18 PM
  8. optimization
    By kckar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-09-2005, 01:05 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