Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Uppercase Function: How to Convert Text

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To convert text to uppercase in Excel VBA, use the UCase function. This built-in VBA function takes a string as an argument and returns a new string where every lowercase letter is converted to its uppercase equivalent, while numbers and symbols remain unchanged. For example, UCase("hello world") results in "HELLO WORLD".

What You'll Need

  • Microsoft Excel installed on a Windows or Mac computer.
  • The Developer tab enabled in the Excel Ribbon.
  • A basic understanding of how to open the Visual Basic Editor (VBE).
  • A dataset containing text that needs to be standardized to uppercase.

Step-by-Step

  1. Enable the Developer Tab: If you do not see the Developer tab at the top of your Excel screen, right-click any existing tab on the Ribbon and select Customize the Ribbon. In the right-hand list, check the box next to Developer and click OK.
  2. Open the Visual Basic Editor: Click the Developer tab and then click the Visual Basic button (or press Alt + F11 on Windows). This opens the VBE window where all your macros and custom functions are written.
  3. Insert a New Module: In the VBE menu, go to Insert > Module. This creates a blank code window. It is best practice to place general-purpose functions in a module rather than inside a specific sheet or the ThisWorkbook object to ensure they are available across the entire workbook.
  4. Write a Custom Function (UDF): If you want to use the uppercase functionality as a formula directly in your cells, write a User Defined Function. Type the following code into the module:
    Function MakeUppercase(text As String) As String
            MakeUppercase = UCase(text)
        End Function
    This creates a custom formula called "=MakeUppercase()" that you can use just like the standard SUM or AVERAGE functions.
  5. Write a Macro for Bulk Conversion: If you need to convert an entire range of cells instantly without using formulas, write a Sub procedure. Use this code:
    Sub ConvertRangeToUppercase()
            Dim cell As Range
            For Each cell In Selection
                If Not IsEmpty(cell) Then
                    cell.Value = UCase(cell.Value)
                End If
            Next cell
        End Sub
    This macro loops through every cell you have currently highlighted and replaces the existing text with the uppercase version.
  6. Run the Macro: Return to your Excel worksheet. Select the range of cells you wish to convert. Go to the Developer tab, click Macros, select "ConvertRangeToUppercase" from the list, and click Run.
  7. Save the Workbook: Because your file now contains VBA code, you must save it in a macro-enabled format. Go to File > Save As and choose the Excel Macro-Enabled Workbook (.xlsm) extension. If you save it as a standard .xlsx file, all your VBA code will be permanently deleted.

Common Mistakes

One of the most frequent errors is attempting to use UCase on a cell that contains an error value, such as #N/A or #VALUE!. If the VBA code encounters an error value while trying to process a string, the macro will crash with a "Type Mismatch" error. To prevent this, always include a check to ensure the cell value is not an error using the IsError() function before applying UCase.

Another common mistake is forgetting to handle empty cells. While UCase can technically handle an empty string, running a loop over a massive range (like an entire column) that contains thousands of empty cells will significantly slow down your macro. Adding a condition like If Not IsEmpty(cell) Then ensures the code only processes cells that actually contain data.

Users also frequently confuse the VBA UCase function with the Excel worksheet function UPPER(). While they achieve the same result, UCase is only recognized within the VBA editor. If you try to type =UCase(A1) directly into a cell without first creating a User Defined Function (UDF), Excel will return a #NAME? error because it does not recognize the VBA command as a native worksheet formula.

Tips

To make your uppercase macro more robust, consider adding a check for the data type. If a cell contains a date or a number, UCase will treat it as a string, but the conversion might not be what you intended. You can use If VarType(cell.Value) = vbString Then to ensure the macro only touches text cells.

If you are dealing with extremely large datasets (tens of thousands of rows), looping through cells one by one is inefficient because every interaction between VBA and the worksheet slows down the process. Instead, read the entire range into a Variant Array, process the uppercase conversion within the array in memory, and then write the entire array back to the sheet in one operation. This can reduce the execution time from minutes to seconds.

For those who need a more nuanced approach, consider combining UCase with Left and Right functions. For example, if you only want the first letter of a word to be uppercase (Proper Case), you can use UCase(Left(text, 1)) & LCase(Mid(text, 2)). Although VBA has a built-in StrConv function for proper casing, building your own logic allows for more control over specific acronyms or naming conventions.

Troubleshooting

If your macro does not run, first check if macros are enabled in your security settings. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure that "Disable all macros with notification" is selected. When you open the file, you must click "Enable Content" on the yellow security bar at the top of the screen.

If you receive a "Compile Error: Variable not defined," it is likely because you have Option Explicit turned on at the top of your module. This is actually a good practice, but it means every variable (like cell or text) must be declared using a Dim statement. Ensure you have Dim cell As Range at the start of your Sub procedure.

If the text is not changing to uppercase despite the code running without errors, check if the cells are formatted as "Text" or if there are leading spaces. While UCase ignores spaces, it cannot change the case of characters that aren't letters. Ensure the cells do not contain non-breaking spaces or hidden characters from a web import that might be interfering with how Excel perceives the string.

Frequently Asked Questions

Can UCase handle non-English characters?

Yes, UCase generally handles Unicode characters and accented letters based on the system's locale settings, though some complex character sets may require the StrConv function for more precise linguistic conversion.

Is there a way to do this without VBA?

Yes, you can use the native Excel formula =UPPER(A1) in a neighboring cell. VBA is only necessary if you want to overwrite the original data automatically or automate the process as part of a larger workflow.

Does UCase change the original data?

The UCase function itself does not change the original data; it returns a modified copy of the string. However, if you use it in a macro like cell.Value = UCase(cell.Value), you are explicitly telling Excel to replace the original value with the uppercase version.

Can I make only the first letter uppercase?

While UCase makes everything uppercase, you can use StrConv(string, vbProperCase) to capitalize only the first letter of every word in a string.

Will this work on Excel for Mac?

Yes, the UCase function and basic VBA modules are supported in Excel for Mac, although the keyboard shortcuts to open the VBE may differ (typically Option + F11).