Macro Not Working? Change case when saving

jeffcoleky

Active Member
Joined
May 24, 2011
Messages
274
The following macro isn't working. I have it placed in the code for Sheet1. Can anyone help me understand why it won't work? it is supposed to change data certain columns to "proper" and others to upper-case.

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim cell    As Range
    Application.EnableEvents = False
    Select Case Sh.CodeName
        Case "Sheet1"
            For Each cell In Target.Cells
                Select Case cell.Column
                    Case 2, 10, 11, 15, 21, 24, 29
                        cell.Value = StrConv(cell.Text, vbUpperCase)
                    Case 5, 6, 7, 9, 12, 13, 14, 18, 19, 20, 22, 23, 27
                        cell.Value = StrConv(cell.Text, vbProperCase)
                End Select
            Next cell


    End Select
 End Sub

I'd love for it to ONLY apply to UNHIDDEN rows. However, at this point having it work on ALL cells would be better than it not working at all which is where it is now...
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Right at the start you disable events but you never enable them at the end.

That means that this code will only ever run once, after that it won't be triggered as events are disabled.

Add this at the end.
Code:
Application.EnableEvents = True
 
Upvote 0
Norie,

I just tried enabling events at the end and it doesn't seem to have made a difference. Here is some sample data to use:


Excel 2010
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1AAAAABBBBBCCCCCDDDDDEEEEEFFFFFGGGGGHHHHHIIIIIJJJJJKKKKKLLLLLMMMMMNNNNNOOOOOPPPPPQQQQQRRRRRSSSSSSTTTTTTTUUUUVVVVVWWWWWXXXXXXYYYYYYZZZZZ
2aaaaabbbbbcccccdddddeeeeefffffggggghhhhhiiiiijjjjjkkkkklllllmmmmmnnnnnooooopppppqqqqqrrrrrsssssstttttttuuuuvvvvvwwwwwxxxxxxyyyyyyzzzzz
3AaaaaBbbbbCccccDddddEeeeeFffffGggggHhhhhIiiiiJjjjjKkkkkLllllMmmmmNnnnnOooooPppppQqqqqRrrrrSsssssTttttttUuuuVvvvvWwwwwXxxxxxYyyyyyZzzzz
Sheet1


Here is the code with your suggested change included:
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim cell    As Range
    Application.EnableEvents = False
    Select Case Sh.CodeName
        Case "Sheet1"
            For Each cell In Target.Cells
                Select Case cell.Column
                    Case 2, 10, 11, 15, 21, 24, 29
                        cell.Value = StrConv(cell.Text, vbUpperCase)
                    Case 5, 6, 7, 9, 12, 13, 14, 18, 19, 20, 22, 23, 27
                        cell.Value = StrConv(cell.Text, vbProperCase)
                End Select
            Next cell
    End Select


    Application.EnableEvents = True


End Sub
 
Upvote 0
When do you want this code to run?
 
Upvote 0
Where did you put this code?

Are you sure it's actually being triggered?

Is it only meant to run when Sheet1 is changed?
 
Upvote 0
The code you posted is for the workbook SheetChange event, and should be in the ThisWorkbook module.

However if you only want the code to run on a specific sheet, eg Sheet1, you should use that sheet's Change event which would go in the worksheet's module.
Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range

    Application.EnableEvents = False

    For Each cell In Target.Cells
        Select Case cell.Column
            Case 2, 10, 11, 15, 21, 24, 29
                cell.Value = StrConv(cell.Text, vbUpperCase)
            Case 5, 6, 7, 9, 12, 13, 14, 18, 19, 20, 22, 23, 27
                cell.Value = StrConv(cell.Text, vbProperCase)
        End Select
    Next cell
    
    Application.EnableEvents = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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