+ Reply to Thread
Results 1 to 13 of 13

Thread: VBA code needed to help reduce series of characters

  1. #1
    Registered User
    Join Date
    12-13-2011
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    11

    VBA code needed to help reduce series of characters

    Hi!

    I have the following code that I borrowed from another thread and tweaked only slightly.

    Function ConcatRange(inputRange As Range, Optional delimiter As String, Optional delimiter2 As String) As String
        Dim oneCell As Range
        With inputRange
            If Not (Application.Intersect(.Parent.UsedRange, .Cells) Is Nothing) Then
                For Each oneCell In Application.Intersect(.Parent.UsedRange, .Cells)
                           
                    If oneCell.Text <> vbNullString Then
                        ConcatRange = ConcatRange & delimiter & oneCell.Text
                    End If
                   If oneCell.Text = vbNullString Then
                       ConcatRange = ConcatRange & delimiter2 & oneCell.Text
                   End If
                   Next oneCell
    
                ConcatRange = Mid(ConcatRange, Len(delimiter) + 1)
                
            End If
        End With
    End Function
    Basically this works beautifully to concatenate a large amount of columns into one and recognize the blank cells as something (in this case a back slash). For my worksheet in a single row my cells either have a word in them or are blank cells between words. So when I apply the above code now I see my result looking something like (purely for example)...

    / ALL CAPS TOGETHER/// all lower case together////

    Here, each back slash (according to the above code) represents one blank cell. What I ACTUALLY want to see is the following:

    ALL CAPS TOGETHER/ all lower case together/

    so that the words or 'phrases' are still separated by something (again, backslash) but only by ONE. Essentially, taking a series of blank cells and reading each series as a single backslash. Can anyone help?? I can not do these individually as my actual data I'm applying this to is several thousand columns.

    Thanks in advance!
    Last edited by PennyKat; 12-14-2011 at 06:01 PM. Reason: for clarity

  2. #2
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,764

    Re: VBA code needed to help reduce series of characters

    Maybe ...

    Function ConcatRange(rCat As Range, Optional sSep As String = "") As String
        Dim cell        As Range
        Dim rInt        As Range
    
        Set rInt = Intersect(rCat.Worksheet.UsedRange, rCat)
        If Not rInt Is Nothing Then
            For Each cell In rInt
                If Len(cell.Text) Then
                    ConcatRange = ConcatRange & cell.Text & sSep
                End If
            Next cell
        End If
    End Function
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    12-13-2011
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code needed to help reduce series of characters

    When I use a space as my delimiter this gives me...

    ALL CAPS TOGETHER all lower case together

    But I still need to be able to see where my blank cells were (via a single backslash) since this indicates to me the separator of a group of words making a phrase. So I would like to see...

    / ALL CAPS TOGETHER / all lower case together /

    (Only red to highlight... I don't need or want the output to have red backslashes.) Does this make sense?

  4. #4
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,764

    Re: VBA code needed to help reduce series of characters

    Post a workbook with examples and expected results that cover the waterfront.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    12-13-2011
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code needed to help reduce series of characters

    Attached is an example of a much smaller version of my real data. I applied my original VBA code to column T. The code is reading from column D through S per row.

    Please let me know if this is unclear. Again, I want column T to spit out... for instance...

    / could look like this / here's another phrase / Hi /
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    12-13-2011
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code needed to help reduce series of characters

    You know what, I seriously apologize. Here, if it helps, please take a look at this one. I've added column U to indicate what I want.
    Attached Files Attached Files

  7. #7
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,764

    Re: VBA code needed to help reduce series of characters

    What logic did you use to decide that

    could look like this/ here's another phrase/ Hi/

    dosn't include a leading slash, but

    /ALL CAPS TOGETHER /all lower case together /

    does?
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    12-13-2011
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code needed to help reduce series of characters

    I used the same code I first mentioned thread. That's the output I got per row. I believe it's due to this line of code...

                ConcatRange = Mid(ConcatRange, Len(delimiter) + 1)
    Like I mentioned, I borrowed some code to build this so I could be mistaken.

  9. #9
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,764

    Re: VBA code needed to help reduce series of characters

    That wasn't my question; I was referring to your desired results.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Registered User
    Join Date
    12-13-2011
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code needed to help reduce series of characters

    Furthermore, ideally I wouldn't have a leading slash BUT I honestly don't care as long as it's just one and not several. Doesn't make a difference to the way I'm analyzing.

  11. #11
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,764

    Re: VBA code needed to help reduce series of characters

    Function ConcatRange(rCat As Range) As String
        Dim cell        As Range
        Dim rInt        As Range
    
        Set rInt = Intersect(rCat.Worksheet.UsedRange, rCat)
        If Not rInt Is Nothing Then
            For Each cell In rInt
                If Len(cell.Text) Then
                    ConcatRange = ConcatRange & cell.Text & " "
                Else
                    If Right(ConcatRange, 1) <> "/" Then ConcatRange = ConcatRange & "/"
                End If
            Next cell
        End If
        
        If Left(ConcatRange, 1) = "/" Then ConcatRange = Mid(ConcatRange, 2)
    End Function
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  12. #12
    Registered User
    Join Date
    12-13-2011
    Location
    Denver, Colorado
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: VBA code needed to help reduce series of characters

    That's beautiful. Thank you for all the help!

  13. #13
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,764

    Re: VBA code needed to help reduce series of characters

    You're welcome, good luck.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0