Changing values of cells to uppercase in a private sub

Dallie

New Member
Joined
Apr 12, 2018
Messages
13
Hi all,

I have this code here that simply turns the cells to uppercase.

Code:
[
Sub Turn_Uppercase()
   For Each x In Range("C3:H200")
           x.Value = UCase(x.value)
   Next
End Sub
/CODE]

Does anyone know how would I adapt this code so that I can run in a sheet tab (Private sub worksheet_change)? rather than a module as others maybe using this sheet and have no knowledge of macros.

Thanks in advance guys!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Whoops!

Code:
 Sub Turn_Uppercase()
   For Each x In Range("C3:H200")
           x.Value = UCase(x.value)
   Next
 End Sub
 
Upvote 0
How do you intend the others to call that routine, a button, automatically capitalize every entry or something else?
 
Upvote 0
If you want it to be automatic for user entry, but not for copy pasted ranges, put this in the code module for the sheet in question.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
        If .Cells.Count = 1 Then
            Application.EnableEvents = False
            .FormulaR1C1 = UCase(.FormulaR1C1)
        End If
    End With
    Application.EnableEvents = True
End Sub
If you want it to also effect copy pasted multi-cell ranges, this (slower) code would work.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oneCell As Range
    Application.EnableEvents = False
    For Each oneCell In Target.Cells
            With oneCell
            .FormulaR1C1 = UCase(.FormulaR1C1)
            End With
    Next oneCell
    Application.EnableEvents = True
End Sub

In either case, you would have to run something like your sub to make all the existing entries upper case before automaticaly capitalizing the new data.
 
Last edited:
Upvote 0
I would not need this for copy-pasted ranges I don't think, so the faster macro would be perfectly suitable. If I needed to change this to specific cells (C3:200) would I need to change the Target.Cells? reference to Target.Range?

Thanks again,
Dallie
 
Upvote 0
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
        If Not Application.Intersect(.Cells, Range("C3:C200")) Is Nothing
            Application.EnableEvents = False
            .FormulaR1C1 = UCase(.FormulaR1C1)
        End If
    End With
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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