VBA help...Excel keeps crashing

Aphten

New Member
Joined
Jul 24, 2024
Messages
5
Office Version
  1. 2021
Platform
  1. Windows
Hi there,

I am trying to do multiple things and I am having trouble figuring this out.

I have a number of cells that I need auto capitalized and I have a number of cells that need to automatically change to proper case.

This is what a friend came up with and it seems to be working without issues.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RNG As Range
Dim cel As Range
Set RNG = Intersect(Range("J3,AT2,Y76,AG76,AO76,AU76,AI4"), Target)

If Not RNG Is Nothing Then
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each cel In RNG
cel.Value = StrConv(cel.Value, vbProperCase)
Next cel
Application.EnableEvents = True
Application.ScreenUpdating = True

End If

If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub

On Error Resume Next

If Not Intersect(Target, Range("C29:C55,V28:V55,AI3,H7,Y5,AC5")) Is Nothing Then
Application.EnableEvents = False
Target = UCase(Target)
Application.EnableEvents = True

End If
End Sub



I am also needing to add in some "if this cell = this, then this cell = this other thing, but every time I add it in, excel either keeps crashing or all the coding stops working or both. Yay. :(

This is what I am trying to add in there:

VBA Code:
If Range("B3").Value = "A" Then
Range("C3").Value = "EF"
End If

If Range("B4").Value = "A" Then
Range("C4").Value = "JH"
End If

If Range("B5").Value = "A" Then
Range("C5").Value = "TUY"
End If

Lastly, I will add that I do have some conditioning formatting as well...not sure if that makes a different or not. If I have conditional formatting for B3, will this cause problems? So B3 makes C3 = EF when it is A, but then I also have conditional formatting so that it will fill a certain color depending on what letter it is.
 
Last edited by a moderator:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
It would help if you posted the full code you're using, including your additions.

But I am guessing that this might help:

VBA Code:
Application.EnableEvents = False
If Range("B3").Value = "A" Then Range("C3").Value = "EF"
If Range("B4").Value = "A" Then Range("C4").Value = "JH"
If Range("B5").Value = "A" Then Range("C5").Value = "TUY"
Application.EnableEvents = True

If you make changes to a worksheet in Sub Worksheet_Change, you need to wrap the code this way (as your friend did) so that the change doesn't trigger recursive calls on the Sub.
 
Upvote 0
@Aphten
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
Members
453,021
Latest member
Justyna P

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