+ Reply to Thread
Results 1 to 26 of 26

Force text to Uppercase in active cells

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-22-2006
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    275

    Force text to Uppercase in active cells

    Hi all

    To ensure correct data entry, I need to ensure that the filled cells in a column are always in Uppercase for example, irrespective of whether they are typed in upper or lower case.

    I have found the following code, which seems to work quite well, even though I have the impression that it slowed my workbook down slightly.

    Private Sub Worksheet_Change(ByVal Target As Range)
     
    If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub
     
        On Error Resume Next
     
        If Not Intersect(Target, Range("D:D,F:F")) Is Nothing Then
     
            Application.EnableEvents = False
     
            Target = UCase(Target)
     
            Application.EnableEvents = True
     
        End If
     
        On Error GoTo 0
    End Sub
    Firstly, I would like to know if the code could be amended so that it only works on the cells in a column from the top of the column (either D1 or D2 down to the last occupied or active cell in that column.

    Secondly, is it possible to have a combined code that also creates a capitalised word for each word in a column but for different specified colums.

    In other words, if you have an address list where columns A, B, C & D are used for the main parts of the address; these would need the 1st letter to be capitalised and then Column E would contain the postcode, which needs to all in uppercase.

    This code would need to be used in various worksheets within one workbook on different columns and I fopund the above code was very easy to customise by changing the column reference.

    TIA ...spellbound
    Last edited by VBA Noob; 01-28-2009 at 03:35 PM.

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Force text to Uppercase in active cells

    Hi ,Try this for columns "A to D" (In column "D" the entire string is Capitalised.)
    You can change the data range and "All captals column" to suit.
    Dim Rng As Range, Dn As Range
    Set Rng = Range(Range("A2"), Range("D" & Rows.Count).End(xlUp))
        For Each Dn In Rng
            Dn.Value = UCase(Left(Dn, 1)) & Right(Dn, Len(Dn) - 1)
                If Split(Dn.Address, "$")(1) = "D" Then
                    Dn.Value = UCase(Dn)
                End If
            Next Dn
    Regards Mick

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Force text to Uppercase in active cells

    Hello Spellbound,

    This should slow you down. It will convert columns "A:D" to proper case (First letter of each word is capitalized and the rest lower case) while column "E" is converted to uppercase. Change the StartCell to the cell address the range starts at. It is set at "D2".
    Private Sub Worksheet_Change(ByVal Target As Range)
    
      Dim LastRow As Long
      Dim Rng As Range
      Dim StartCell As String
      Dim StartRow As Long
      
        StartCell = "D2"
        
          With Range(StartCell)
            C = .Column
            StartRow = .Row
          End With
          
          LastRow = Cells(Rows.Count, C).End(xlUp).Row
          LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
          Set Rng = Range(Cells(StartRow, C), Cells(LastRow, C))
          
       Application.EnableEvents = False
       
            Select Case Target.Column
              Case 1, 2, 3, 4
                Target = WorksheetFunction.Proper(Target)
              Case 5
                Target = UCase(Target)
            End Select
          
        Application.EnableEvents = True
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Contributor
    Join Date
    03-22-2006
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    275

    Re: Force text to Uppercase in active cells

    Hi Mick

    Thanks for your suggestion but I could not get your code to work.

    I clicked on the worksheet tab, then 'view code' and pasted it in as 'General' code, when that did not work, I also tried it as 'Worksheet Change' but that did not work either.

    So not sure what else to do as my knowledge of VBA is zilch.

    spellbound

  5. #5
    Forum Contributor
    Join Date
    03-22-2006
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    275

    Re: Force text to Uppercase in active cells

    Hi Roy

    Thanks for your suggestion, which I tried out and this works on its own but as my knowledge of VBA is zero, so I could not figure out how to combine both statements in one piece of code.

    The solution from Leith works with the combination that I need.

    Always open to suggestions though!

    spellbound
    Last edited by Spellbound; 01-30-2009 at 11:00 AM.

  6. #6
    Forum Contributor
    Join Date
    03-22-2006
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    275

    Re: Force text to Uppercase in active cells

    Hi Leith

    Thanks for your piece of coding although I was very puzzled as to your initial comment in that you said "This should slow you down".

    I was also quite puzzled as to why the range should start from D2 in your example, as from your description I would expect the start of this range to be A2.

    So I have made the following adjustments, which seem to work but correct me if I have done them wrong.

    For my particular worksheet, I need Columns B, C, D, E, F & G to be in Proper Case and Column H to be in Upper Case.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
      Dim LastRow As Long
      Dim Rng As Range
      Dim StartCell As String
      Dim StartRow As Long
      
        StartCell = "B2"
        
          With Range(StartCell)
            C = .Column
            StartRow = .Row
          End With
          
          LastRow = Cells(Rows.Count, C).End(xlUp).Row
          LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
          Set Rng = Range(Cells(StartRow, C), Cells(LastRow, C))
          
       Application.EnableEvents = False
       
            Select Case Target.Column
              Case 2, 3, 4, 5, 6, 7
                Target = WorksheetFunction.Proper(Target)
              Case 8
                Target = UCase(Target)
            End Select
          
        Application.EnableEvents = True
    
    End Sub
    However, I also need to include a second range in the same worksheet, where Columns K, L, M, & N needs to be in Proper Case and Column O needs to be in Upper Case.

    I assume that I could run a second piece of code as above but altered to accomodate the second range but is it possible to combine the two ranges within one piece of code.

    TIA ...spellbound

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Force text to Uppercase in active cells

    Have you tried

     Target = StrConv(Target, vbProperCase)
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1