Convert character to upper case, but only in specific circumstances?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
Here's another one I've been struggling with this afternoon.

I have this code to convert all text in a named range (SubLotColumn) to uppercase:

VBA Code:
If Not Intersect(Target, Range("SubLotColumn")) Is Nothing Then

        Application.EnableEvents = False
        
        If Trim(Target) <> "" Then
        Target = UCase(Target)
        End If

        Application.EnableEvents = True

End If

This works fine except that there are instances when I don't want the text uppercase. I want all instances of the letter M to be uppercase when it is followed by a dash such as this:

1124M-50

However, if the text appears without a dash following such as this:

Christmas Eve

...I do not want it uppercase.

It will always be an M.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
How about
VBA Code:
If Not Intersect(Target, Range("SubLotColumn")) Is Nothing Then

        Application.EnableEvents = False
        
        If InStr(1, Target, "-") > 0 Then
        Target = UCase(Target)
        End If

        Application.EnableEvents = True

End If
 
Upvote 0
Solution
My submission would be ..
VBA Code:
    If Trim(Target) <> "" Then
        Target = VBA.Replace(Target, "m-", "M-")
    End If
 
Upvote 0
How about
VBA Code:
If Not Intersect(Target, Range("SubLotColumn")) Is Nothing Then

        Application.EnableEvents = False
       
        If InStr(1, Target, "-") > 0 Then
        Target = UCase(Target)
        End If

        Application.EnableEvents = True

End If

Works great. Thanks!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,553
Messages
6,120,176
Members
448,948
Latest member
spamiki

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