+ Reply to Thread
Results 1 to 5 of 5

Clear contents when cell only contains hard return

  1. #1
    Registered User
    Join Date
    10-13-2021
    Location
    Sacramento, CA
    MS-Off Ver
    10
    Posts
    3

    Clear contents when cell only contains hard return

    I have written a macro to collapse my comments into the first blank column in a row that are spread out over several columns on any given row, which was working fine, but now the linked report that is feeding this information into my report has cells that have no info but instead hard returns which appear blank but my macro is not reading these cells as blank. What can I add to my macro to get it to clear all cells that only have hard returns and no other info before running the remainder of the tasks I have set for it?
    This is what I have tried but it is not working, is there some other code for hard return?
    ' Delete blanks
    Dim c As Range
    Dim rng As Range
    Set rng = Range("U4:BX98")

    For Each c In rng
    If c.Value = "CHAR(10)" Then c.ClearContents
    Next c

  2. #2
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Clear contents when cell only contains hard return

    Assuming there would only be a single "Line Feed" (not hard return) and no other characters (such as a space), then your line of code should be...

    If c.Value = Chr(10) Then c.ClearContents

    CHAR is an Excel function... its VBA equivalent is Chr. Side note... VBA has a predefined constant for Chr(10), it is vbLf, so you could write that line of code like this as well...

    If c.Value = vbLf Then c.ClearContents

    Now in your write up, you said "all cells that only have hard returns" which is plural. If the cell had two or more Line Feed characters in it, the above line of code would not clear the cell because it is only looking for a single Line Feed. If you need to check for one or more Line Feed feed characters (and again, with no other characters), this line of code would do that...

    If Not c.Value Like "*[!" & vbLf & "]*" Then

  3. #3
    Registered User
    Join Date
    10-13-2021
    Location
    Sacramento, CA
    MS-Off Ver
    10
    Posts
    3

    Re: Clear contents when cell only contains hard return

    Thank you for your response. I am pretty new to VBA so yes some of these not used as much predefined constants are not familiar to me. Yes, you are correct there are up to 5 Line Feed characters. How should the last code you provided be used when I put it in there it clear all contents not just the ones with only line feed characters?

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Clear contents when cell only contains hard return

    What I wrote would replace what you already have up to the "Then" keyword. Here is the complete line of code...

    If Not c.Value Like "*[!" & vbLf & "]*" Then c.ClearContents

    This line of code will only clear cells containing nothing but Line Feed characters (no matter how many that is)... it will not clear any other cells.

  5. #5
    Registered User
    Join Date
    10-13-2021
    Location
    Sacramento, CA
    MS-Off Ver
    10
    Posts
    3

    Re: Clear contents when cell only contains hard return

    Thank you so much, you fixed my issue and so fast thank you!

+ 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 depending on cell contents
    By terratushi in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-15-2017, 08:02 PM
  2. [SOLVED] Needing VBA code to clear contents of cell when the contents of another cell is cleared
    By jeh0714 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-15-2016, 03:49 PM
  3. 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
  4. Clear Contents of a cell if contents = 1/0/00
    By superiorsvc in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-03-2015, 06:25 PM
  5. [SOLVED] Clear contents based on contents of another cell
    By Katrina DTE in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-13-2013, 02:44 PM
  6. Recorded macro has hard cell contents.
    By DocBrown in forum Excel General
    Replies: 4
    Last Post: 08-11-2006, 02:20 PM
  7. How do I split a one cell which has a hard return?
    By GatorGirl in forum Excel General
    Replies: 2
    Last Post: 06-19-2006, 05:10 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