Forum

Private Sub Workshe...
 
Notifications
Clear all

Private Sub Worksheet_Change(ByVal Target As Range)

5 Posts
4 Users
0 Reactions
832 Views
(@robejohg)
Posts: 2
New Member
Topic starter
 

I have the following Private sub that is to ensure that the value entered in the cell will be all CAPS. When I use this, it just loops and never get to End Sub...

Any suggestions?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim UpperCaseSym As Range
Set UpperCaseSym = Range("B9")

If Target.Address = UpperCaseSym.Address Then
UpperCaseSym = UCase(UpperCaseSym)

End If
End Sub

Thanks

Gib

 
Posted : 13/01/2024 8:14 pm
(@kjbox)
Posts: 69
Trusted Member
 

Try this

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$9" Then Target = UCase(Target)
End Sub

 
Posted : 14/01/2024 7:34 am
(@keebellah)
Posts: 373
Reputable Member
 

If it's only B9 that needs to be processed then Charle's code is perfect, but if you want to process another range you could use the worksheet change event together with the intersect command

 
Posted : 15/01/2024 4:18 am
(@debaser)
Posts: 837
Member Moderator
 

If you change cells in a Change event, you should always disable events while you do so to avoid recursion:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim UpperCaseSym As Range
Set UpperCaseSym = Range("B9")

If Not Intersect(Target, UpperCaseSym) is nothing Then
application.enableevents = false

UpperCaseSym = UCase(UpperCaseSym)

application.enableevents = True

End If
End Sub

 
Posted : 16/01/2024 5:11 am
(@robejohg)
Posts: 2
New Member
Topic starter
 

Was able to resolve this by turning off updates while the macro was in process:

Private Sub Worksheet_Change(ByVal Target As Range)

'This is to UPPERCASE the value entered in to the Ticker Cell
Dim UpperCaseSym As Range
Set UpperCaseSym = Range("B9")

If Target.Address = UpperCaseSym.Address Then
Application.EnableEvents = False ' Disable events to prevent a loop
UpperCaseSym.Value = UCase(UpperCaseSym.Value)
Application.EnableEvents = True ' Re-enable events
End If

End Sub

 

Thanks to all,

Gib

 
Posted : 17/01/2024 6:29 am
Share: