Consulting

Results 1 to 7 of 7

Thread: line breaks & spaces

  1. #1

    line breaks & spaces

    I'd like to insert a line break into or replace a certain amount of spaces. I've tried using find & replace and did a couple searches that referenced char(10) as line break but it didn't work. I also tried finding char(9) for tabs but I doubt they make it through excels text import process.

    q.1.
    I need a macro for replacing for every 10 blank spaces and put a line break in the cells of a whole column or selection in excel?


    ---====---=-=-=-=-=-=-=----AND


    I found this vba for removing spaces in a cell
    Public Sub RemoveSpaces()
    Application.ActiveCell = Trim(Application.ActiveCell)
    End Sub

    q.2.
    How can I make it trim for a whole column or selection?
    [FONT=verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif]

    Yes I am new to all this and fiddled a bit to actually get the vb editor up, I appreciate the help.[/font]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Welcome to VBAX

    Can you post a sample workbook using Manage Attachments in the Go Advanced reply section.

    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Try this. Paste into a regular module (Insert Module from the VBE), not into a worksheet module (also on the VBE, but different)

    As it is now, it replaces 10 spaces with a line break and turns on text wrap for the cells or columns that are selected


    [VBA]
    Option Explicit

    Const s10Spaces As String = " "

    Sub ReplaceSpaces()

    Dim rData As Range, rCell As Range

    If Not TypeOf Selection Is Range Then Exit Sub

    Set rData = Nothing
    On Error Resume Next
    Set rData = Intersect(Selection, Selection.Parent.UsedRange)
    On Error GoTo 0

    If rData Is Nothing Then Exit Sub

    Application.ScreenUpdating = False

    With rData
    .Replace What:=s10Spaces, Replacement:=vbLf
    .WrapText = True
    End With

    Application.ScreenUpdating = True
    End Sub

    Paul
    [/VBA]

  4. #4
    This is a "macro enabled" file

  5. #5
    this is 2003 excel format

  6. #6
    Quote Originally Posted by Paul_Hossler
    Try this. Paste into a regular module (Insert Module from the VBE), not into a worksheet module (also on the VBE, but different)

    As it is now, it replaces 10 spaces with a line break and turns on text wrap for the cells or columns that are selected


    [vba]
    Option Explicit

    Const s10Spaces As String = " "

    Sub ReplaceSpaces()

    Dim rData As Range, rCell As Range

    If Not TypeOf Selection Is Range Then Exit Sub

    Set rData = Nothing
    On Error Resume Next
    Set rData = Intersect(Selection, Selection.Parent.UsedRange)
    On Error GoTo 0

    If rData Is Nothing Then Exit Sub

    Application.ScreenUpdating = False

    With rData
    .Replace What:=s10Spaces, Replacement:=vbLf
    .WrapText = True
    End With

    Application.ScreenUpdating = True
    End Sub

    Paul
    [/vba]
    Wow, works perfectly!!! I went from this to this . Thanks Paul!!

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Yes, he does have that effect on people.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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