+ Reply to Thread
Results 1 to 7 of 7

How do I speed up VBA code that clears cells

Hybrid View

Bobbbo How do I speed up VBA code... 06-02-2017, 01:28 PM
Richard Buttrey Re: How do I speed up VBA... 06-02-2017, 01:43 PM
cytop Re: How do I speed up VBA... 06-02-2017, 01:43 PM
Arkadi Re: How do I speed up VBA... 06-02-2017, 01:56 PM
kvsrinivasamurthy Re: How do I speed up VBA... 06-03-2017, 01:55 AM
Bobbbo Re: How do I speed up VBA... 06-03-2017, 02:11 PM
Arkadi Re: How do I speed up VBA... 06-03-2017, 07:05 PM
  1. #1
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    405

    How do I speed up VBA code that clears cells

    I have a worksheet that has a number of cells that need to be cleared. When I use the F8 key on the keyboard to walk through the VBA code I noticed that it takes seconds to "Clear Contents" The cells I need to clear are not contiguious. Is there a more efficent and faster way to clear cells? Speed is very important in this code. The code is below.
    Sheets("DATA ENTRY").Select
            Range("AA12:DV48").Select
            Selection.ClearContents
            Range("F12:Z12").Select 
            Selection.ClearContents
            Range("F15:Z15").Select 
            Selection.ClearContents
            Range("F19:Z20").Select 
            Selection.ClearContents
            Range("F28:Z28").Select 
            Selection.ClearContents
            Range("G41:Z42").Select 
            Selection.ClearContents
            Range("G44:Z45").Select 
            Selection.ClearContents
            Range("G47:Z48").Select 
            Selection.ClearContents
            Range("G51:Z51").Select  
            Selection.ClearContents
            Range("G56:Z56").Select  
            Selection.ClearContents
            Range("G58:Z61").Select   
            Selection.ClearContents
            Range("G64:Z68").Select 
            Selection.ClearContents
            Range("F79:Z85").Select  
            Selection.ClearContents
            Range("F89:Z92").Select  
            Selection.ClearContents
            Range("F94:Z94").Select  
            Selection.ClearContents
            Range("F96:Z96").Select  
            Selection.ClearContents
            Range("G101:Z102").Select  
            Selection.ClearContents
            Range("G104:Z109").Select  
            Selection.ClearContents
            Range("G111:Z111").Select  
            Selection.ClearContents
            Range("F138:Z138").Select 
            Selection.ClearContents
    Thanks in advance.
    Last edited by Bobbbo; 06-03-2017 at 07:40 PM. Reason: Mark it solved

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How do I speed up VBA code that clears cells

    Hi

    The most important thing you should do is remove all the 'Selection' stuff

    i.e. instead of
    Range("AA12:DV48").Select
    Selection.ClearContents
    just use
    Range("AA12:DV48").ClearContents
    It's rarely necessary to select or activate stuff. Just address any objects/ranges etc. directly.

    It would be easier to comment further if we had the workbook but if you're able to use Autofilter to filter rows 12, 15, 19:20, ...etc then you could then use a
    Range("F12:Z138").SPecialCells(xlCellTypeVisible).ClearContents
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How do I speed up VBA code that clears cells

    First off - don't use Select... just clear the ranges directly

    Range("G56:Z56").ClearContents
    ... just to start with.

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: How do I speed up VBA code that clears cells

    Doing the whole clear at once might be more efficient also, but definitely all those .select are redundant. Try this:

    Dim myRange As Range
    With Sheets("DATA ENTRY")
        Set myRange = Union(.Range("AA12:DV48"), .Range("F12:Z12"), .Range("F15:Z15"), .Range("F19:Z20"), .Range("F28:Z28"), _
        .Range("G41:Z42"), .Range("G44:Z45"), .Range("G47:Z48"), .Range("G51:Z51"), .Range("G56:Z56"), .Range("G58:Z61"), _
        .Range("G64:Z68"), .Range("F79:Z85"), .Range("F89:Z92"), .Range("F94:Z94"), .Range("F96:Z96"), .Range("G101:Z102"), _
        .Range("G104:Z109"), .Range("G111:Z111"), .Range("F138:Z138"))
    End With
    myRange.ClearContents
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: How do I speed up VBA code that clears cells

    One example
    Change this code

    Sheets("DATA ENTRY").select
            Range("AA12:DV48").Select
            Selection.ClearContents
            Range("F12:Z12").Select
            Selection.ClearContents
            Range("F15:Z15").Select
            Selection.ClearContents
    As
    Sheets("DATA ENTRY").Range("AA12:DV48, F12:Z12, F15:Z15").ClearContents

  6. #6
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    405

    Re: How do I speed up VBA code that clears cells

    Quote Originally Posted by kvsrinivasamurthy View Post
    One example
    Change this code

    Sheets("DATA ENTRY").select
            Range("AA12:DV48").Select
            Selection.ClearContents
            Range("F12:Z12").Select
            Selection.ClearContents
            Range("F15:Z15").Select
            Selection.ClearContents
    As
    Sheets("DATA ENTRY").Range("AA12:DV48, F12:Z12, F15:Z15").ClearContents
    Great Idea, thanks so much!

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: How do I speed up VBA code that clears cells

    If this solves the issue please remember to mark the thread as solved? 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. Replies: 4
    Last Post: 03-18-2022, 03:30 PM
  2. [SOLVED] VBA Code that Clears contents of specific column in row
    By Jamidd1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-20-2016, 07:49 AM
  3. vba code to clears cells and hide rows
    By Zimmerman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-04-2015, 10:09 AM
  4. Clear cell macro clears other cells not recorded
    By bryden2008 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-30-2015, 02:03 PM
  5. [SOLVED] Macro for a button that clears cells adjacent to a checked checkbox
    By daveblair197 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-20-2015, 07:40 AM
  6. Macro that saves cell data to a separate spreadsheet and clears cells?
    By peepo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-16-2010, 10:47 AM
  7. [SOLVED] Moving section of cells clears instead!?
    By Don Wiss in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-23-2005, 10:06 AM

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