Using Cap-Proper in Given Column

adoanvba

New Member
Joined
Nov 19, 2010
Messages
7
Hello,

Currently the code below perform the proper function (Capitalize the first letter) after entering in the column in the pop up window. Depending on the files, the column containing the "First Name" and "Last Name" may be in A:B or D and E etc. Is there a way to use this proper function on the column name "First Name" and "Last Name" rather than having the indicating which cloumn to perform the proper function?

Code:
Sub Proper()
    Dim varMyArrayList As Variant, _
        varMyArrayItem As Variant
    Dim intMyArrayCount As Integer
    Dim strFirstNameCol As String, _
        strLastNameCol As String
    Dim lngRowStart As Long, _
        lngRowEnd As Long
    Dim rngFirstName As Range, _
        rngLastName As Range, _
        rngCell As Range
 
    varMyArrayList = InputBox("Please enter the columns containing the first and last names (respectively) separated by a single comma i.e." & vbNewLine & "C,D", "Proper Case Converter")
 
    'Quit the routinue if the user has clicked the  button or _
    not put a comma between the column letters.
    If varMyArrayList = "" Or InStr(varMyArrayList, ",") = 0 Then Exit Sub
 
    varMyArrayItem = Split(varMyArrayList, ",") 'Splits words by a comma
 
    lngRowStart = 2 'Assumed starting row number.  Change if needed.
 
    For intMyArrayCount = 0 To UBound(varMyArrayItem)
 
        If intMyArrayCount = 0 Then
            strFirstNameCol = StrConv(CStr(varMyArrayItem(intMyArrayCount)), vbUpperCase)
        Else
            strLastNameCol = StrConv(CStr(varMyArrayItem(intMyArrayCount)), vbUpperCase)
        End If
 
    Next intMyArrayCount
 
    'Set the 'rngFirstName' range and then convert each cell therein to ProperCase.
    lngRowEnd = Cells(Rows.Count, strFirstNameCol).End(xlUp).Row
    Set rngFirstName = Range(strFirstNameCol & lngRowStart & ":" & strFirstNameCol & lngRowEnd)
 
    Application.ScreenUpdating = False
 
    For Each rngCell In rngFirstName
        rngCell.Value = StrConv(rngCell.Value, vbProperCase)
    Next rngCell
 
    Application.ScreenUpdating = True
 
    'Set the 'rngLastName' range and then convert each cell therein to ProperCase.
    lngRowEnd = Cells(Rows.Count, strLastNameCol).End(xlUp).Row
    Set rngLastName = Range(strLastNameCol & lngRowStart & ":" & strLastNameCol & lngRowEnd)
 
    Application.ScreenUpdating = False
 
    For Each rngCell In rngLastName
        rngCell.Value = StrConv(rngCell.Value, vbProperCase)
    Next rngCell
 
    Application.ScreenUpdating = True
 
End Sub


Please correct the code above to run the proper function on the column with the name "First Name" and "Last Name" without having enter the columns in the text box window. This way I dont have to look for the column "First Name" and "Last Name" and entering the column in the pop up text/window. Thanks!
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
The code will be something like this.
Change Selection to any other range.
Code:
    For Each c In Selection.Cells
        c.Value = Application.WorksheetFunction.Proper(c.Value)
    Next
 
Upvote 0

Forum statistics

Threads
1,215,107
Messages
6,123,127
Members
449,097
Latest member
mlckr

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