change marco dynamic range to setup range

jaik22

Board Regular
Joined
Sep 23, 2016
Messages
102
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)


If Target.Cells.Count = 1 Then
    Target.Value = UCase(Target.Value)
End If
End Sub

I have this vba to change all lowercase to uppercase in the workbook. However, I would like to use this macro only on c12:c19 and
c21:33. Is there any way to change dynamic range to setup range?

Thank you!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    Dim myRange As Range
    
    Set myRange = Range("C12:C19, C21:C33")
    
    If Target.Cells.Count = 1 Then
        If Not Intersect(Target, myRange) Is Nothing Then
            Application.EnableEvents = False
            Target.Value = UCase(Target.Value)
            Application.EnableEvents = True
        End If
    End If

End Sub
Note: You usually want to use EnableEvents = False when making the change so that the change the code makes doesn't fire off another event and call itself. If you aren't careful, you can get yourself caught in an infinite loop sometimes!
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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