Change to upper case without changing the font format

praveenpatel421983

New Member
Joined
Aug 17, 2017
Messages
41
Hi all,

I working on a spreadsheet which contains columns with strings. These strings are entered by different users. I need these columns to be in upper case always due to some reasons. But when I use code to change it, everything turns into uppercase but format of text is gone.
eg: "This item is used in Product X" should become "THIS ITEM IS USED IN PRODUCT X" instead it becomes "THIS ITEM IS USED IN PRODUCT X". Words which are made bold with increased font size is not followed.

I went to many posts but couldn't find the solution. I used below code. Please let me know what to change in this
PHP:
Private Sub Worksheet_Activate()
    Range("A1:AF300") = [index(upper(A1:AF300),)]
End Sub

I also used this. this also didn't work
PHP:
Private Sub Worksheet_Activate()   
   For Each x In Range("A1:AF300")
      x.Value = UCase(x.Value)
   Next
End Sub

Please help

Thanks in advance!
Praveen
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this

Code:
Sub ChangeCaseOnly()
    Dim cel As Range, Chars As Integer, Asci As Integer 
    For Each cel In Range("A1:AF300")
        With cel
            For Chars = 1 To Len(.Text)
                Asci = Asc(Mid(.Text, Chars, 1))
                    If Asci < 123 And Asci > 96 Then .Characters(Chars, 1).Text = Chr(Asci - 32)
            Next Chars
        End With
    Next cel
End Sub
 
Upvote 0
Try this on a small sample first, since the code will loop each character in the cell it will take a long time to finish in a big data.

Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1086999a()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1086999-change-upper-case-without-changing-font-format.html[/COLOR][/I]

[COLOR=Royalblue]Dim[/COLOR] r [COLOR=Royalblue]As[/COLOR] Range
Application.ScreenUpdating = [COLOR=Royalblue]False[/COLOR]
[COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] r [COLOR=Royalblue]In[/COLOR] Range([COLOR=brown]"A1:A10"[/COLOR])
    [COLOR=Royalblue]For[/COLOR] k = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] Len(r)
        [COLOR=Royalblue]With[/COLOR] r.Characters(k, [COLOR=crimson]1[/COLOR])
             .[COLOR=Royalblue]Text[/COLOR] = UCase(.[COLOR=Royalblue]Text[/COLOR])
        [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]With[/COLOR]
    [COLOR=Royalblue]Next[/COLOR]
[COLOR=Royalblue]Next[/COLOR]
Application.ScreenUpdating = [COLOR=Royalblue]True[/COLOR]
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0
Sorry I had to come back on the post again. Yongle your code worked fine until now. For some reason it is not working for a cell which has bigger paragraph. I tried splitting that paragraph into smaller sentences and it worked for that but it didn't work for that big paragraph. I was not able to figure out what was the problem. Can you please help?
 
Upvote 0
The workaround is to reapply current text formatting character by character

- create an array to capture the font size for each character in a cell
- convert text to upper case
- apply captured font size to text (one character at a time)

NOTE : The technique would need extending if there anything other than font size differs between characters
(capture all values first, then convert to upper case, then apply captured values)

Code:
Sub ChangeCaseAndMaintainFontSize()
        Application.ScreenUpdating = False
    Dim cel As Range, Chars As Long, T1 As String
    Dim arr() As Integer
    For Each cel In Range("A1:AF300")
        T1 = cel.Text
'create array of font sizes
        ReDim arr(1 To Len(T1))
        For Chars = 1 To Len(T1)
            arr(Chars) = cel.Characters(Chars, 1).Font.Size
        Next
'change to upper case
        cel = UCase(T1)
'apply font size from array
        For Chars = 1 To Len(T1)
            cel.Characters(Chars, 1).Font.Size = arr(Chars)
        Next
        Erase arr
    Next cel
End Sub
 
Last edited:
Upvote 0
above modified to take account of empty cells and font sizes that are not whole numbers

Code:
Sub ChangeCaseAndMaintainFontSize()
    Application.ScreenUpdating = False
    Dim cel As Range, Chars As Long, T1 As String
    Dim arr() [COLOR=#ff0000]As Double[/COLOR]
    For Each cel In Range("E1:E10")
        T1 = cel.Text
'create array of font sizes
        [COLOR=#ff0000]If Len(T1) > 0 Then[/COLOR]
            ReDim arr(1 To Len(T1))
                For Chars = 1 To Len(T1)
                arr(Chars) = cel.Characters(Chars, 1).Font.Size
            Next
'change to upper case
            cel = UCase(T1)
'apply font size from array
            For Chars = 1 To Len(T1)
                cel.Characters(Chars, 1).Font.Size = arr(Chars)
            Next
            Erase arr
        [COLOR=#ff0000]End If[/COLOR]
    Next cel
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,384
Members
448,956
Latest member
JPav

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top