'********************************************************************

'*

'* Function strFormat

'*

'*   Author: NetworkAdminKB.com

'*  Created: 2005-01-12

'* Modified: 2006-12-23

'*

'* Purpose: Formats a given string into the specified case.

'*          intFormat = 1 = ALL UPPER CASE

'*          intFormat = 2 = all lower case

'*          intFormat = 3 = Every Word Starts Capitalized

'*          intFormat = 4 = Only the first word is capitalized.

'*

'* Input:   strAny     A string that is to be formated.

'*          intFormat  An integer (1-4) representing the format to change

'*                      the string to.

'*

'* Output:  Returns the given string in the given format.

'*

'*  Notes: Numbers are returned as numbers and not strings.

'*

'* Changes:

'* 2006-12-23: Re-formated code layout.

'* 2006-12-23: Removed Public from the Function Declaration statement.

'* 2006-12-23: Removed individual If Not IsNumeric Statements to a single

'*               check prior to the select statement.

'********************************************************************

Function strFormat(ByVal strAny, ByVal intFormat)

  'Version 1.1 2006-12-23

 

  Dim strTemp1, strTemp2, intCount, strWord

 

  Const vbFirstWord  = 4

  Const vbProperCase = 3

  Const vbLowerCase  = 2

  Const vbUpperCase  = 1

 

  strTemp1 = strAny

 

  'UCase and LCase will convert numbers to strings, to prevent

  'this we check for number prior to converting case.

  If IsNumeric(strTemp1) Then

    StrFormat = strTemp1

    Exit Function

  End If 'IsNumeric(strTemp1)

 

  Select Case intFormat

    Case vbUpperCase

      strTemp1 = UCase(strTemp1)

    Case vbLowerCase

      strTemp1 = LCase(strTemp1)

    Case vbProperCase

      strTemp1 = LCase(strTemp1)

      strTemp2 = Split(strTemp1)

      For intCount = 0 To UBound(strTemp2)

        strWord = strTemp2(intCount)

        If Len(Trim(strWord)) > 0 Then

          strWord = UCase(Left(strWord, 1)) & _

          Right(strWord, Len(strWord) - 1)

          strTemp2(intCount) = strWord

        End If 'Len(Trim(strWord)) > 0

      Next 'intCount

      strTemp1 = Join(strTemp2)

    Case vbFirstWord

      strTemp2 = UCase(Left(strTemp1, 1))

      strTemp1 = LCase(Right(strTemp1, Len(strTemp1) -1))

      strTemp1 = strTemp2 & strTemp1

    Case Else

  End Select 'intFormat

 

  strFormat = strTemp1

End Function 'strFormat

Article ID: 423, Created On: 9/25/2011, Modified: 9/25/2011