+ Reply to Thread
Results 1 to 4 of 4

Amend Upper Case macro to ignore brackets

Hybrid View

Rick_Stanich Amend Upper Case macro to... 03-10-2008, 05:39 PM
Rick_Stanich With code from Rick Rothstein... 03-11-2008, 10:21 AM
Rick_Stanich Rick Rothstein took the time... 03-14-2008, 01:44 PM
Rick_Stanich Add Link to cross posted forum 03-20-2008, 05:30 PM
  1. #1
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Amend Upper Case macro to ignore brackets

    I got this macro from some NG some where (Thanks), what I need help on is making the macro ignore all characters in brackets "()".
    I am absolutely dying trying to mod this macro.
    Heck, is it even possible?

    'Change Text to Upper Case or Proper Case. See Also:
    'Force Upper Case/Proper Case
    
    'Excel has 2 built in functions for converting text to
    'either UPPER CASE or Proper Case. The 2 functions that
    'do this are shown below;
    
    '=UPPER(A1)
    '=PROPER(A1)
    
    'These Excel functions work well when referring to cells
    'that house the text. However, there are many instances
    'when using the Worksheet Function approach is not practical.
    'The Excel macro code below can be used to change existing
    'text to either UPPER CASE or Proper Case. If you run the
    'macro with only a single cell selected it will work on the
    'entire Worksheet. If you run the macro with more than 1
    'cell selected it will work on only your selection.
    'The other settings that the StrConv Function take are
    'shown below. See the Excel VBA help for specifics.
    
    Sub ConvertCase()
        Dim rAcells As Range, rLoopCells As Range
        Dim lReply As Long
    
        'Set variable to needed cells
        If Selection.Cells.Count = 1 Then
            Set rAcells = ActiveSheet.UsedRange
        Else
            Set rAcells = Selection
        End If
    
    
        On Error Resume Next    'In case of NO text constants.
        'Set variable to all text constants
        Set rAcells = rAcells.SpecialCells(xlCellTypeConstants, xlTextValues)
    
        If rAcells Is Nothing Then
            MsgBox "Could not find any text."
            On Error GoTo 0
            Exit Sub
        End If
    
        lReply = MsgBox("Select 'Yes' for UPPER CASE or 'No' for Proper Case.", _
                        vbYesNoCancel, "OzGrid.com")
        If lReply = vbCancel Then Exit Sub
    
        If lReply = vbYes Then    ' Convert to Upper Case
            For Each rLoopCells In rAcells
                rLoopCells = StrConv(rLoopCells, vbUpperCase)
            Next rLoopCells
        Else    ' Convert to Proper Case
            For Each rLoopCells In rAcells
                rLoopCells = StrConv(rLoopCells, vbProperCase)
            Next rLoopCells
        End If
    
    End Sub
    As always, I value all help provided.

    To the MOD who improved my title, Thanks!
    Last edited by Rick_Stanich; 03-10-2008 at 06:43 PM.
    Regards

    Rick
    Win10, Office 365

  2. #2
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177
    With code from Rick Rothstein (MVP - VB), I have this modified code.
    I have no idea how he figured this out, but damn, thats some skill.

    Sub ConvertCase()
        Dim rAcells As Range
        Dim rLoopCells As Range
        Dim X As Long
        Dim lReply As Long
        Dim TextLine As String
        Dim Parsed() As String
    
        'Set variable to needed cells
        If Selection.Cells.Count = 1 Then
            Set rAcells = ActiveSheet.UsedRange
        Else
            Set rAcells = Selection
        End If
    
        On Error Resume Next    'In case of NO text constants.
        'Set variable to all text constants
        Set rAcells = rAcells.SpecialCells(xlCellTypeConstants, xlTextValues)
    
        If rAcells Is Nothing Then
            MsgBox "Could not find any text."
            On Error GoTo 0
            Exit Sub
        End If
    
        lReply = MsgBox("Select 'Yes' for UPPER CASE; 'No' for Proper Case.", _
                        vbYesNoCancel, "OzGrid.com")
        If lReply = vbCancel Then Exit Sub
    
        For Each rLoopCells In rAcells
            TextLine = Replace(rLoopCells, ")", "()")
            Parsed = Split(TextLine, "(")
            For X = 0 To UBound(Parsed) Step 2
              Parsed(X) = StrConv(Parsed(X), IIf(lReply = vbYes, _
                                  vbUpperCase, vbProperCase))
            Next
            rLoopCells = Replace(Join(Parsed, "("), "()", ")")
        Next
    End Sub

  3. #3
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177
    Rick Rothstein took the time to educate myself on how this was accomplished, from this I thought the information may be helpful to others.

    Here is his explanation:
    Let's show you what I did with an example. Consider this string of text...

    TextFromCell = "One two (Three Four) Five (Six) Seven"

    First off, since the Split function only works with a single delimiter, let
    convert the closing parentheses to opening parentheses, but in such a way
    that we can find them again later in order to turn them back to closing
    parentheses. To do that, I am going to replace all ')' with '()'....

    ' TextLine is Dim'med as a simple String
    TextLine = Replace(TextFromCell, ")", "()")

    At this point, TextLine contains this...

    "One two (Three Four() Five (Six() Seven"

    Now, we split this using the open parenthesis as the delimiter.

    ' ParsedLine is Dim'med as a dynamic String array
    ParsedLine = Split(TextLine, "(")

    Okay, at this point the ParsedLine array has 5 elements (index numbers 0
    through 4)

    Element 0: "One two "
    Element 1: "Three Four"
    Element 2: ") Five "
    Element 3: "Six"
    Element 4: ") Seven"

    Notice that text inside the parentheses are located at elements 1 and 3. As
    it turns out, no matter how many parentheses-grouped pieces of text you
    have, they will always occur at an odd-numbered element index... even if the
    text starts with an open parenthesis (with no text in front of it). So, to
    process only the text **not** located inside parentheses, all we have to do
    is loop through the even numbered element indexes starting with index number
    zero. The loop structure to do that is...

    For X = 0 To UBound(ParsedLine) Step 2
    '
    ' ParsedLine(X) is text not inside any parentheses, do something to
    it here
    '
    Next

    Okay, now the elements of the array look like this (assuming we are upper
    casing it)...

    Element 0: "ONE TWO "
    Element 1: "Three Four"
    Element 2: ") FIVE "
    Element 3: "Six"
    Element 4: ") SEVEN"

    Now, we rejoin the array using the Join function and specify the opening
    parenthesis (what we used to break it the original text apart with) as the
    delimiter. Once this is done, our joined text string looks like this...

    TextLine = "ONE TWO (Three Four() FIVE (Six() SEVEN"

    All that is left is to replace the '()' symbol pair with ')" and assign it
    back to the cell where it came from...

    TextFromCell = Replace(TextLine, "()", ")")

    Now we do this for every cell in the range we are processing.

    Rick
    The explanation was so intuitive I am capable of following it and my VB skills don't compare to many, certainly not Rick's.

    LOL
    I feel like I am talking about myself in the third person.

  4. #4
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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