Proper case -- speed issue

wmtsub

Active Member
Joined
Jun 20, 2018
Messages
322
I use this on many work sheets and it works well. But i am working on a table and I think it is doing the entire possible range rather than the populated range. How can I modify this so it only works on populated cells?



Sub ProperCase()
awsn = ActiveSheet.Name
ib = InputBox("Count What Row")
Dim LastRow As Integer
With Worksheets(awsn)
LastRow = .Cells(Rows.Count, ib).End(xlUp).Row
.Range("A2:xfd" & LastRow).Value = .Evaluate("INDEX(PROPER(A2:xfd" & LastRow & "),)")
End With
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I use this on many work sheets and it works well. But i am working on a table and I think it is doing the entire possible range rather than the populated range. How can I modify this so it only works on populated cells?

Sub ProperCase()
awsn = ActiveSheet.Name
ib = InputBox("Count What Row")
Dim LastRow As Integer
With Worksheets(awsn)
LastRow = .Cells(Rows.Count, ib).End(xlUp).Row
.Range("A2:xfd" & LastRow).Value = .Evaluate("INDEX(PROPER(A2:xfd" & LastRow & "),)")
End With
End Sub
I presume what I highlighted in red should read "Column", not "Row", correct?

Question: Are you trying to do this for all of the filled cells on the active sheet or are you asking the above question so that some rows below the calculated last row will be left alone?
 
Upvote 0
Going on the assumption that you're trying to convert all cells in the active sheet to proper case.

Code:
Public Sub ConvertActiveSheet()
  If TypeOf ActiveSheet Is Worksheet Then
    ConvertTextToProperCase ActiveSheet
  End If
End Sub

'-----------------------------------------------------------------------------

Private Sub ConvertTextToProperCase(ByVal Sh As Worksheet)

' Method to convert all text constants on
' the worksheet argument to proper case.

  Dim TextConstants As Range
  Dim Cell As Range
  
  On Error GoTo ErrorHandler
  Set TextConstants = Sh.Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
    
  For Each Cell In TextConstants.Cells
    Cell.Value = StrConv(Cell.Text, vbProperCase)
  Next Cell
  
ExitHandler:
  Set TextConstants = Nothing
  Set Cell = Nothing
  Exit Sub
  
ErrorHandler:
  MsgBox Err.Description, vbExclamation
  Resume ExitHandler
End Sub
 
Upvote 0
Nice. I hardly use the Goto (F5) when working in Excel. Definitely helps reduce the cells to be changed.
 
Upvote 0
Going on the assumption that you're trying to convert all cells in the active sheet to proper case.

Rich (BB code):
Public Sub ConvertActiveSheet()
  If TypeOf ActiveSheet Is Worksheet Then
    ConvertTextToProperCase ActiveSheet
  End If
End Sub

'-----------------------------------------------------------------------------

Private Sub ConvertTextToProperCase(ByVal Sh As Worksheet)

' Method to convert all text constants on
' the worksheet argument to proper case.

  Dim TextConstants As Range
  Dim Cell As Range
  
  On Error GoTo ErrorHandler
  Set TextConstants = Sh.Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
    
  For Each Cell In TextConstants.Cells
    Cell.Value = StrConv(Cell.Text, vbProperCase)
  Next Cell
  
ExitHandler:
  Set TextConstants = Nothing
  Set Cell = Nothing
  Exit Sub
  
ErrorHandler:
  MsgBox Err.Description, vbExclamation
  Resume ExitHandler
End Sub
I would suggest changing the highlighted line of code to this...

Cell.Value = Application.Proper(Cell.Text)

as Excel's Proper function is much better than VB's StrConv/vbProperCase function (VB's function mishandles text after dashes, parentheses, and such).
 
Last edited:
Upvote 0
I would suggest changing the highlighted line of code to this...

Cell.Value = Application.Proper(Cell.Text)

as Excel's Proper function is much better than VB's StrConv/vbProperCase function (VB's function mishandles text after dashes, parentheses, and such).

Thanks for the tip. Didn't know that.
 
Upvote 0

Forum statistics

Threads
1,214,422
Messages
6,119,396
Members
448,891
Latest member
tpierce

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