Capitalize first letter of string with vba

Dokat

Active Member
Joined
Jan 19, 2015
Messages
303
Office Version
  1. 365
Hi,

I am trying capitalize first letter of each string and lower case rest between columns C and F. I am using below vba code however it's giving me run time error 91. Can anyone help me with this problem?


VBA Code:
Sub ConvertUppercaseInRange()

Application.ScreenUpdating = False
Worksheets("StaPVM").Activate

            Dim rng As Range
            
            rng = Columns("c:f").Select

            For Each rng In ActiveSheet.UsedRange

            rng.Value = StrConv(rng.Value, vbProperCase)

            Next rng
            
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi Dokat,

Try this:

VBA Code:
Option Explicit
Sub ConvertUppercaseInRange()

    Dim wsSrc As Worksheet
    Dim rngCell As Range
    Dim lngLastRow As Long

    Application.ScreenUpdating = False
    
    Set wsSrc = ThisWorkbook.Sheets("StaPVM")
    lngLastRow = wsSrc.Range("C:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For Each rngCell In wsSrc.Range("C2:F" & lngLastRow)
        If Len(rngCell) > 0 And IsNumeric(rngCell) = False Then
            rngCell.Value = StrConv(rngCell.Value, vbProperCase)
        End If
    Next rngCell
            
    Application.ScreenUpdating = True
    
End Sub

Note if the text is the result of a formula it will be replaced with its proper case value.

Regards,

Robert
 
Upvote 0
I see you have a solution but I'll tell you the problem with your original code. Select does not return a value. You should probably have used Set if you wanted to set rng to a range but Select would still be a problem; you would just remove it. Your assignment is invalid and I am sure that is where you are getting the error (always give the error message and the line of code when asking about an error). That is line is useless anyway because you immediately use rng as an iterator.
 
Upvote 0
Hi Dokat,

Try this:

VBA Code:
Option Explicit
Sub ConvertUppercaseInRange()

    Dim wsSrc As Worksheet
    Dim rngCell As Range
    Dim lngLastRow As Long

    Application.ScreenUpdating = False
   
    Set wsSrc = ThisWorkbook.Sheets("StaPVM")
    lngLastRow = wsSrc.Range("C:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
    For Each rngCell In wsSrc.Range("C2:F" & lngLastRow)
        If Len(rngCell) > 0 And IsNumeric(rngCell) = False Then
            rngCell.Value = StrConv(rngCell.Value, vbProperCase)
        End If
    Next rngCell
           
    Application.ScreenUpdating = True
   
End Sub

Note if the text is the result of a formula it will be replaced with its proper case value.

Regards,

Robert
Thank you it worked perfectly
 
Upvote 0
Hi Dokat,

Try this:

VBA Code:
Option Explicit
Sub ConvertUppercaseInRange()

    Dim wsSrc As Worksheet
    Dim rngCell As Range
    Dim lngLastRow As Long

    Application.ScreenUpdating = False
   
    Set wsSrc = ThisWorkbook.Sheets("StaPVM")
    lngLastRow = wsSrc.Range("C:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
    For Each rngCell In wsSrc.Range("C2:F" & lngLastRow)
        If Len(rngCell) > 0 And IsNumeric(rngCell) = False Then
            rngCell.Value = StrConv(rngCell.Value, vbProperCase)
        End If
    Next rngCell
           
    Application.ScreenUpdating = True
   
End Sub

Note if the text is the result of a formula it will be replaced with its proper case value.

Regards,

Robert
Is there anyway to exclude a string from the range. For Ex: If there is a string ("DEAN FARMS") between column C:F do not change the upper or lower case of the string?
 
Upvote 0
I see you have a solution but I'll tell you the problem with your original code. Select does not return a value. You should probably have used Set if you wanted to set rng to a range but Select would still be a problem; you would just remove it. Your assignment is invalid and I am sure that is where you are getting the error (always give the error message and the line of code when asking about an error). That is line is useless anyway because you immediately use rng as an iterator.
Thanks for calling out the issue.
 
Upvote 0
May be:
VBA Code:
For Each rngCell In wsSrc.Range("C2:F" & lngLastRow)
        If Len(rngCell) > 0 And IsNumeric(rngCell) = False Then
             rngCell.Value = Replace(StrConv( rngCell.Value, vbProperCase), "Dean Farms", "DEAN FARMS", , , vbBinaryCompare = True)
        End If
    Next rngCell
 
Upvote 0
May be:
VBA Code:
For Each rngCell In wsSrc.Range("C2:F" & lngLastRow)
        If Len(rngCell) > 0 And IsNumeric(rngCell) = False Then
             rngCell.Value = Replace(StrConv( rngCell.Value, vbProperCase), "Dean Farms", "DEAN FARMS", , , vbBinaryCompare = True)
        End If
    Next rngCell
Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,174
Members
448,870
Latest member
max_pedreira

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