+ Reply to Thread
Results 1 to 4 of 4

Macro to create a new line within a text string if specific characters appear mid-string

Hybrid View

Groovicles Macro to create a new line... 07-29-2014, 11:01 AM
Olly Re: Macro to create a new... 07-29-2014, 11:22 AM
Groovicles Re: Macro to create a new... 07-29-2014, 11:29 AM
Olly Re: Macro to create a new... 07-29-2014, 11:32 AM
  1. #1
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Macro to create a new line within a text string if specific characters appear mid-string

    hi everyone,

    I have the following code:

    Sub PointForm()
        Dim intcount As Integer
        Dim rngRange As Range
        Dim strTMP As String
        Dim splitVals As Variant
        Dim totalVals As Long
        For Each rngRange In Range("$E$1:$E$3000, $G$1:$G$3000")
        On Error Resume Next
            If Trim(rngRange.Value) <> "" Then
                strTMP = rngRange.Text
                intcount = InStr(strTMP, "&&")
                If intcount <> 0 Then
                    splitVals = Split(ActiveCell.Value, Chr(10))
                    totalVals = UBound(splitVals)
                End If
            End If
        On Error GoTo 0
        Next rngRange
    End Sub
    What this code is supposed to do, is go through each cells in the ranges specified. If the text strings in the cell contains a && the a new line after the && is created and bringing the remaining string to the right of the && with it. I do not get an error with this only, only it doesn't actually seem to do anything.

    So here's the effect:

    Current (in a single cell):
    The sun is out && The sun is out

    Desired result (in a single cell):

    The sun is out
    The sun is out

    Any ideas?

    Thanks everyone!

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Macro to create a new line within a text string if specific characters appear mid-stri

    Try simply:
    Sub foo()
    Sheet1.Range("$E$1:$E$3000, $G$1:$G$3000").Replace What:="&&", Replacement:=Chr(10)
    End Sub
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Macro to create a new line within a text string if specific characters appear mid-stri

    Olly,

    You, good sir, are a genius. Thanks very much

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Macro to create a new line within a text string if specific characters appear mid-stri

    Happy to help. Thanks for the feedback.

+ 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] Macro to convert specific text and format specific text within a string
    By Mencae in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-06-2012, 12:13 AM
  2. Replies: 8
    Last Post: 08-07-2012, 10:15 AM
  3. Searching for Text String and copying specific portion based on characters
    By undergraduate in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-30-2010, 12:45 PM
  4. search for specific characters in text string and separate them out
    By neowok in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-09-2008, 09:21 AM
  5. [SOLVED] Text String - Specific Characters
    By Kiser in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-09-2006, 10:50 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