+ Reply to Thread
Results 1 to 12 of 12

Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Enter)

  1. #1
    Registered User
    Join Date
    10-25-2022
    Location
    Washington
    MS-Off Ver
    365
    Posts
    7

    Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Enter)

    I need vba code to find any spaces greater than 1, remove those spaces and replace that empty space with a new line (Alt + Enter). Then move down to the next cell in the selection and repeat.

    1: Each line cannot be more than 70 characters per cell. (more of a fail safe as most of the time there won't be more than 70 characters before the spaces)

    2: I need to be able to select the the cells that I want this to apply to. ( The selection will always be 1 column only)

    3: It will only apply to visible rows. (Not applying to hidden rows by filter)


    Below is an example of the starting data, and what the desired results should be.

    Any help would be much appreciated.

    CSD example.png
    Attached Files Attached Files
    Last edited by Elreezy; 10-26-2022 at 02:18 PM. Reason: New attachment

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,613

    Re: Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Ent

    Please Login or Register  to view this content.
    Last edited by protonLeah; 10-25-2022 at 08:57 PM.
    Ben Van Johnson

  3. #3
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Ent

    Hello.
    Another alternative is to apply the following to your data range:

    PHP Code: 
    Sub Example4()
    Dim saQ&, i&, Tmp
    Application
    .ScreenUpdating False
    = [a1].CurrentRegion.Resize(, 1).ValueUBound(a)
    [
    b1].Resize(Q) = "|"
    [a1].CurrentRegion.Offset(, 2).ClearChrW(9742)
    For 
    2 To Q
      Tmp 
    Split(Replace(a(i1), "   ""@" s), "@")
      
    Tmp Filter(TmpsFalse): Tmp Join(TmpvbLf)
      
    a(i1) = Replace(Replace(Tmp" """), s"")
    Next
    Columns
    ("C").ColumnWidth 82: [C1].Resize(Q) = a: [C1].Resize(Q).Rows.AutoFit
    End Sub 

  4. #4
    Registered User
    Join Date
    10-25-2022
    Location
    Washington
    MS-Off Ver
    365
    Posts
    7

    Re: Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Ent

    The first macro posted almost worked, but it only worked on the first Cell (A2). It took those results and copied them all the way down. It needs to adjust cell A2, then move down and adjust Cell A3, ect... until the last cell with data in it.

    The second macro worked perfectly, except it needs to overwrite the existing data, instead of putting it 2 columns over.
    Last edited by Elreezy; 10-26-2022 at 12:17 PM. Reason: forgot to include what code i was referring to

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

    Cool Try this !


    According to your attachment a VBA demonstration as a beginner starter :

    PHP Code: 
    Sub Demo1()
           Const 
    "¤"
             
    Dim Rg As RangeF%, SC&
             
    Application.ScreenUpdating False
        
    For Each Rg In Selection
            
    If Not Rg.Rows(1).Hidden Then
                F 
    0
                S 
    Split(Rg)
            For 
    0 To UBound(S) - 1
                
    If S(C) = "" And S(1) = "" Then
                   S
    (C) = vbLf:  1
                    
    While S(1) = "":   1:   S(C) = D:  Wend
                End 
    If
            
    Next
                
    If F Then Rg Join(Filter(SDFalse))
            
    End If
        
    Next
             Application
    .ScreenUpdating True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 10-26-2022 at 02:19 PM. Reason: optimization ...

  6. #6
    Registered User
    Join Date
    10-25-2022
    Location
    Washington
    MS-Off Ver
    365
    Posts
    7

    Re: Try this !

    This works perfectly. Now I just need to have my starting point at J2 instead of A1. I'm somewhat of a novice but usually pretty good at manipulating existing code to my needs. However, I am sturggling to get this to work in Column J (with J2 being the starting point as i have headers).

  7. #7
    Registered User
    Join Date
    10-25-2022
    Location
    Washington
    MS-Off Ver
    365
    Posts
    7

    Re: Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Ent

    Also, this won't effect any existing data to columns left or right of the column being adjusted will it?

  8. #8
    Registered User
    Join Date
    10-25-2022
    Location
    Washington
    MS-Off Ver
    365
    Posts
    7

    Re: Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Ent

    My apologies I should have done that initially. I over estimated my ability to manipulate code.

    As in my original post, I was wanting to be able to highlight with a selection the data I will be manipulating. Example: Select J2:J611, then run this macro.

    The reason for this is because sometimes i may need to make similar adjustments in columns K. Its imperative it does not effect any existing data outside of the selection.

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

    Re: Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Ent


    According to the initial post my previous demonstration updated to work with the selection ...

  10. #10
    Registered User
    Join Date
    10-25-2022
    Location
    Washington
    MS-Off Ver
    365
    Posts
    7

    Re: Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Ent

    I have attached an example of an actual file i will be working with.

  11. #11
    Registered User
    Join Date
    10-25-2022
    Location
    Washington
    MS-Off Ver
    365
    Posts
    7

    Re: Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Ent

    Sorry your right Mark. it is working perfectly NOW! thank you so much

  12. #12
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Need help creating VBA to remove spaces, replace it with a new line in cell (Alt + Ent

    With these new specs (title in J1 and data from J2 and down), then it's simpler. Look at:

    PHP Code: 
    Sub Example4()
    Dim saQ&, i&, Tmp
    Application
    .ScreenUpdating False
    Range("J1", [J1].End(xlDown)).ValueUBound(a): ChrW(9742)
    For 
    2 To Q
      Tmp 
    Split(Replace(a(i1), "   ""@" s), "@")
      
    Tmp Filter(TmpsFalse): Tmp Join(TmpvbLf)
      
    a(i1) = Replace(Replace(Tmp" """), s"")
    Next
    [J1].Resize(Q) = a: [J1].Resize(Q).Rows.AutoFit
    End Sub 

+ 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. Creating a clone line of data to remove the <alt> + Enter.
    By sungen99 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-12-2019, 12:06 PM
  2. [SOLVED] Remove extra spaces in each line in a cell
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-16-2015, 06:23 AM
  3. Remove leading and trailing spaces in each new line break in cell
    By halo12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-29-2015, 07:10 AM
  4. Replies: 0
    Last Post: 02-01-2014, 09:52 AM
  5. [SOLVED] VBA to replace strings of spaces (2 or more) with 1 alt+enter
    By jaimelwilson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-04-2013, 08:40 PM
  6. [SOLVED] Remove spaces and replace with comma.
    By abjac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2013, 01:01 PM
  7. Using the Replace to remove Spaces in a Number
    By TWent in forum Excel General
    Replies: 5
    Last Post: 05-09-2011, 02:34 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