+ Reply to Thread
Results 1 to 7 of 7

Clear merged cells with VBA, any difference between the two formulas?

  1. #1
    Forum Contributor
    Join Date
    08-12-2019
    Location
    NYC
    MS-Off Ver
    MS 365
    Posts
    167

    Clear merged cells with VBA, any difference between the two formulas?

    Hi,

    I want to automatically clear cells across my workbook with VBA (to empty it). Certain cells are merged so the common approach of .clearcontents does't really work properly.

    It seems that the following two approaches work correctly and I'm wondering if one should be used over the other:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    mRange2 is my named range.

    Thanks

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,701

    Re: Clear merged cells with VBA, any difference between the two formulas?

    Please Login or Register  to view this content.
    Change Range reference as required

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

    Lightbulb Re: Clear merged cells with VBA, any difference between the two formulas?


    Hi,

    try mRange2.MergeArea(1).Value2 = Empty

  4. #4
    Forum Contributor
    Join Date
    08-12-2019
    Location
    NYC
    MS-Off Ver
    MS 365
    Posts
    167

    Re: Clear merged cells with VBA, any difference between the two formulas?

    Thanks to both. It seems like the two approaches I mention in my initial message both work so the questions really is: is there a reason I should use one over the others (incl. the ones you suggest)?

    Thanks

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: Clear merged cells with VBA, any difference between the two formulas?

    jolivanes made some mistakes in his code. We check whether the cells are merged with the MergeCells property. When we run the loop through all cells of the range, the line
    Please Login or Register  to view this content.
    will work correctly only for the first cell of the merged range, for the rest it will report an error. And there is no need to test whether the cells are merged. You can use MergeArea for any cell. More correct code (with merged cells check)
    Please Login or Register  to view this content.
    or without checking
    Please Login or Register  to view this content.
    In contrast, the code suggested by Marc L could look like this:
    Please Login or Register  to view this content.
    Sometimes I see codes where users clean cells with an empty string (""). In my opinion, this is not the correct solution. The Clear, ClearContents methods should be used, but these should be used in conjunction with MergeArea. And if we don't want to use these methods, cells can be cleared by assigning them Null. There are subtle differences between the "", vbNullString, Empty, Null assignments, but these are "invisible" to the average user. Personally, I recommend the use of Null. For example
    Please Login or Register  to view this content.
    or shorter the same
    Please Login or Register  to view this content.
    Artik

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

    Arrow Re: Clear merged cells with VBA, any difference between the two formulas?


    As when reading some blank range from a brand new worksheet in an array the content is Empty and not Null …

  7. #7
    Forum Contributor
    Join Date
    08-12-2019
    Location
    NYC
    MS-Off Ver
    MS 365
    Posts
    167

    Re: Clear merged cells with VBA, any difference between the two formulas?

    Thanks everyone, appreciate the feedback. Just to be clear, what is the difference between the code I'm mentioning in my initial post (which seems to work) and the more complex ones proposed in the answers? Is there a particular reason to use the more complex ones?

    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. [SOLVED] Clear contents merged cells
    By Vlad2112 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-14-2021, 12:54 PM
  2. 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
  3. [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
  4. 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
  5. [SOLVED] Formulas with cells that are merged
    By greteberit in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-11-2014, 04:45 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

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