on Worksheet_Change, UCase Selection

2Took

Board Regular
Joined
Jun 13, 2022
Messages
203
Office Version
  1. 365
Platform
  1. Windows
Getting "runtime error 28 out of stack space" when running below code.
And only the C10 gets Upper Cased, while the rest of pasted sells remains lower cased.
Trying to get entire pasted range Upper Cased.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("c10")) Is Nothing And Range("c10").Value <> "" Then
Call UPCaseSelection
End If
End Sub


Sub UPCaseSelection()
Dim rng As Range
Set rng = Selection
For Each cell In rng
cell.Value = UCase(cell)
Next cell
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Each time UPCaseSelection changes a cell, Worksheet_Change is called, creating infinite recursion until you run out of stack space. You need to turn off events while making changes.

Also, indent to show structure.

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Application.Intersect(Target, Range("c10")) Is Nothing And Range("c10").Value <> "" Then
      Application.EnableEvents = False
      Call UPCaseSelection
      Application.EnableEvents = True
   End If
End Sub


Sub UPCaseSelection()
   Dim rng As Range
   Set rng = Selection
   For Each cell In rng
       cell.Value = UCase(cell)
   Next cell
End Sub
 
Upvote 0
Solution
What exactly are you trying to do?
What do you do that results in this error?

Note that your code is specifically written to only run when a value is manually entered into cell C10.

Also, what 6StringJazzer said (I saw his response pop up while I was trying to detail/explain that part, so I won't repeat what he said).
 
Upvote 0

Forum statistics

Threads
1,215,837
Messages
6,127,185
Members
449,368
Latest member
JayHo

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