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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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