Results 1 to 6 of 6

Thread: Remove Hard Carriage Return And Leading And Trailing Spaces

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Rep Power
    0

    Remove Hard Carriage Return And Leading And Trailing Spaces

    Mr. Rick,

    Looking for a solution to my situation, I have found this 2013 post: https://www.mrexcel.com/forum/excel-...e-returns.html. I have tried to post my question here but it seems to be old and required to have a new post.

    Your solution works great from me to remove hard carriage returns. I would like to see if possible to incorporate this to an existing private sub I am using to trim spaces from start and end of entered text strings.

    My code is the following:

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim Cell As Range
      If Not Intersect(Target, Range("A4:A1004")) Is Nothing Then
        Application.EnableEvents = False
        For Each Cell In Intersect(Target, Range("A4:A1004"))
          If Len(Cell.Value) Then Cell.Value = Trim(Cell.Value)
        Next
        Application.EnableEvents = True
      End If
    End Sub
    I would like to include your solution to above mentioned, so that when entering information at the target range, the macro automatically removed the hard carriage returns as well

    Code:
    Sub ReplaceAtSignWithLineFeed_v3()
      Selection = Evaluate("IF(ROW(),SUBSTITUTE(" & Selection.Address & ",CHAR(10),""""))")
    End Sub
    By the way, my first macro portion takes some time to execute, if you can suggest a more effective and expedited way to do it, please let me know.

    Thanks in advance for your assistance.
    Last edited by Excel Fox; 04-08-2019 at 10:13 PM. Reason: Code Tags Added

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Hi David

    Try this

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim Cell As Range
      If Not Intersect(Target, Range("A4:A1004")) Is Nothing Then
        Application.EnableEvents = False
        With Intersect(Target, Range("A4:A1004"))
            .Value = Evaluate("IF(ROW(),TRIM(SUBSTITUTE(" & .Address & ",CHAR(10),"""")))")
        End With
        Application.EnableEvents = True
      End If
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Rep Power
    0
    Last edited by DocAElstein; 09-22-2023 at 05:15 PM.

  4. #4
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    That seems to be a completely different requirement, unrelated to what you are doing above. Best to start a new thread with the code/sample workbook
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  5. #5
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Rep Power
    0
    I have resolved the issue of the conditional formatting by modifying the target.

    Regarding the solution for the trim and carriage returns' removal, which work very efficiently, I am still running into a problem. My worksheet contains several other macros; one of the macros is to append the worked data to a different worksheet, and I am getting at the column "A" of the worksheet where I am applying your solution multiple rows with "#VALUE!" which when doing the append, it gives me incorrect results due to the contents of column "A".

    I am wondering if possible to have your solution, instead of hard coded for a range, to have it dynamically to be triggered by an input to a cell of the column "A", and within the range "A4:A1004"

    Thank you!

  6. #6
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10
    Hello David Montoya,
    I am finding it hard to understand what you are asking with this:
    Quote Originally Posted by David Montoya View Post
    I am wondering if possible to have your solution, instead of hard coded for a range, to have it dynamically to be triggered by an input to a cell of the column "A", and within the range "A4:A1004"
    As I understand it, the effect of this code line, .._
    Code:
      If Not Intersect(Target, Range("A4:A1004")) Is Nothing Then
    _.. , is to trigger, if a cell or cells in the range "A4:A1004" have its or their value changed.

    ( Further, the effect of this, .._
    Code:
        With Intersect(Target, Range("A4:A1004"))
    , _.. is to restrict action to the cell or cells which were changed. )

    That sounds close to what you are asking for.
    So I am confused about what you are actually wanting.
    It does not take much to coinfuse me. So I may have overlooked something, or missed the point
    It might be easier if you could give us a workbook with a reduced size sample, of desensitized data, to help explain what you want and / or what the problem is that you are having.

    Alan

    P.s. Welcome to excelfox.com :-)
    Last edited by DocAElstein; 04-15-2019 at 11:54 AM.

Similar Threads

  1. Wrap Text On Spaces Up To A Maximum Number Of Characters Per Line
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 15
    Last Post: 12-20-2016, 09:47 AM
  2. hard question
    By mahmoud-lee in forum Excel Help
    Replies: 1
    Last Post: 05-28-2013, 02:27 PM
  3. LTRIM() Function VBA: To Remove Spaces
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 05-17-2013, 12:32 AM
  4. Macro to delete trailing X's
    By Howardc in forum Excel Help
    Replies: 2
    Last Post: 03-29-2013, 12:43 PM
  5. VBA editor auto-deletes spaces at the ends of lines
    By LalitPandey87 in forum Excel Help
    Replies: 0
    Last Post: 06-26-2012, 07:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •