+ Reply to Thread
Results 1 to 4 of 4

Need help speeding up Clear cells and merged cell

Hybrid View

  1. #1
    Registered User
    Join Date
    11-05-2012
    Location
    shelton, wa
    MS-Off Ver
    Office 365
    Posts
    24

    Need help speeding up Clear cells and merged cell

    Hello, I am two codes that I am calling when I click an icon in office 365's Excel. The two VBA's that I have, I found online looking for a code to do this process. I am not a VBA writer so that is why I am stuck in this spot right now. I ran the code and it took a little over two minutes to run. I know it could be faster if the two items did not look at every cell on the active sheet, but only the area that I allow people to input info. N3:AZ500 is my range that I need these two to look at instead of the whole sheet. Can anyone help out on changing the Range? I understand what the code is doing, it's just the Dim Rng As Range and such is where I get lost. Thanks again.




    'THIS WILL CLEAR OUT ANY MERGED CELLS WHERE THE PROTECTION OPTION IN FORMAT CELL IS NOT SELECTED TO LOCKED
    Sub ClearMergedCells()
        Dim Rng As Range, cel As Range, rClearA As Range
        Set Rng = ActiveSheet.UsedRange
        For Each cel In Rng
            If cel.MergeCells And cel.Locked = False Then
                If rClearA Is Nothing Then
                    Set rClearA = cel
                Else
                    Set rClearA = Union(rClearA, cel)
                End If
            End If
        Next cel
        rClearA.ClearContents
    End Sub
    
    'THIS WILL CLEAR OUT ANY UNMERGED CELLS WHERE THE PROTECTION OPTION IN FORMAT CELL IS NOT SELECTED TO LOCKED
    Sub ClearUnlockedCells()
    
        Dim WorkRange As Range
        Dim Cell As Range
        Set WorkRange = ActiveSheet.UsedRange
        
        For Each Cell In WorkRange
            If Cell.Locked = False Then Cell.Value = ""
        Next Cell
    
    End Sub
    
    'THIS CALLS THE ABOVE TWO CODES AND RUNS THEM
    Sub CLEAN_SHEET()
    Call ClearMergedCells
    Call ClearUnlockedCells
    End Sub
    Last edited by SHELTONUNDERDOG; 12-10-2021 at 04:05 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,559

    Re: Need help speeding up Clear cells and merged cell

    To answer your direct question you would make this change:
        Set Rng = ActiveSheet.Range("N3:AZ500")
    However, this may not give much speed improvement. That's still a lot of cells (almost 20,000), and I can't think of a way to do this without checking them individually.

    Also merged cells will almost always cause problems and should be avoided.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    11-05-2012
    Location
    shelton, wa
    MS-Off Ver
    Office 365
    Posts
    24

    Re: Need help speeding up Clear cells and merged cell

    Thanks, it worked and was faster but not fast. Your right about the amount of cells still slowing it down.

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,555

    Re: Need help speeding up Clear cells and merged cell

    UsedRange limits the search to a certain range (used range), so the whole sheet is not searched. However, if the used range is significantly larger than the range N3: AZ500, then in fact quite a lot of unnecessary cells are processed. To limit the scope of searches, we can use the SpecialCells property. I don't know what type of data to expect in unlocked cells. If they are only constant values, you can search the range:
     ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
    when there are only formulas, replace the constant with xlCellTypeFormulas. If both types are present, this becomes a problem, create two loops for each type separately. Also note that when using SpecialCells, the sheet cannot be protected at this point.

    The ClearUnlockedCells macro has a performance problem, because it cleans the cells one at a time. To speed up its operation, it should be built similarly to ClearMergedCells - create a union.

    Assuming that the unprotected cells can contain only constants (without formulas), the sheet is not protected at this point (the photo and the assumption of protection can be added to the procedure) and that we always execute the main macro CLEAN_SHEET, i.e. we want to clean the merged and non-merged cells, the procedure could look like this:
    Sub CLEAN_SHEET_1()
        Dim Rng As Range, cel As Range, rClearA As Range
        Dim i           As Long
        
        With Application
          .ScreenUpdating = False
          .Calculation = xlCalculationManual
        End With
    
        'Set Rng = Range("N3:AZ500").SpecialCells(xlCellTypeConstants)
        'Or
        Set Rng = ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
    
        For Each cel In Rng
            If Not cel.Locked Then
                i = i + 1
    
                If rClearA Is Nothing Then
                    Set rClearA = cel
                Else
                    Set rClearA = Union(rClearA, cel)
                End If
    
                'When a large union of ranges is formed, it is better to clean the range in batches.
                If i >= 500 Then
                    rClearA.Value = Empty
                    i = 0
                    Set rClearA = Nothing
                End If
    
            End If
        Next cel
    
        If Not rClearA Is Nothing Then
            rClearA.Value = Empty
        End If
        
        With Application
          .Calculation = xlCalculationAutomatic
          .ScreenUpdating = True
        End With
    
        MsgBox "Done.", vbInformation
    End Sub
    Artik

+ 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. Clear merged cells with VBA, any difference between the two formulas?
    By MagnusNovak in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-17-2021, 07:09 PM
  2. [SOLVED] Clear contents merged cells
    By Vlad2112 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-14-2021, 12:54 PM
  3. Clear contents in Merged and Un-Merged, Unprotected Cells of a Protected Worksheet.
    By Magnetite in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2020, 04:23 AM
  4. [SOLVED] Clear merged and non-merged cells in named range.
    By IMM Tech in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2018, 12:37 PM
  5. Clear merged cell contents in comand button(clear all)
    By mohan_984 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-25-2015, 10:39 AM
  6. Clear contents of all unlocked cells (many are merged cells)
    By 2funny4words in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 06-08-2009, 09:21 AM
  7. [SOLVED] Clear Contents won't work on merged cells
    By ArthurJ in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-20-2006, 01:25 PM

Tags for this Thread

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