Excel VBA Uppercase String: UCase Guide
In this article
To convert a string to uppercase in Excel VBA, use the built-in function UCase: UCase("hello") returns HELLO. It is a native VBA function, so nothing needs to be installed or enabled; you just need the Visual Basic Editor open and a place to put the code. UCase always returns a text string, leaves numbers and punctuation untouched, and is the standard way to normalise text before comparing, matching, or writing it to a cell.
What You'll Need
- A copy of Excel for Windows or Mac with macros enabled (the .xlsm format if you plan to save the workbook).
- Access to the Visual Basic Editor, which is on the Developer tab or reachable with the Alt+F11 shortcut on Windows. If you do not see the Developer tab, right-click the ribbon, choose Customize the Ribbon, and tick Developer in the list on the right. On current Excel for Mac, the Developer tab is available from the ribbon preferences in the same way.
- A test workbook you do not mind changing, or a scratch worksheet where you can type sample text.
- Macro security set to allow the project you are writing. If macros are blocked, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and choose to be notified rather than disabled, then confirm with your IT policy before changing anything.
Menu labels and ribbon names can differ slightly by Excel version, platform, and region, so if a label does not match exactly, look for the closest equivalent.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 on Windows, or use Developer > Visual Basic on the ribbon. A separate window opens with the Project Explorer on the left.
- Create or choose a module. In the Project Explorer, right-click your workbook name, choose Insert > Module. Code placed in a standard module can be run from anywhere in the workbook. Code in a worksheet module only runs in response to events on that sheet.
- Write a minimal procedure. In the code window type
Sub UppercaseTest(), press Enter, and inside the Sub typeMsgBox UCase("excel vba"). Close the Sub withEnd Sub. Press F5 to run it. A message box should showEXCEL VBA. - Test with a real cell. Replace the MsgBox line with
Range("A1").Value = UCase(Range("A1").Value). Type some mixed-case text into A1, click back into the procedure, and press F5. Cell A1 now holds the text in capitals. If A1 is empty, the result is an empty string rather than an error. - Handle a whole range with a loop. To convert A1 through A10, use a loop such as
For Each c In Range("A1:A10"): c.Value = UCase(c.Value): Next c, declaringDim c As Rangeat the top. This overwrites the original text, so work on a copy or a spare column first. - Convert text without changing the source. If you want to keep the original, write the result to another column:
Range("B1").Value = UCase(Range("A1").Value). This is safer for anything you may need to reverse later. - Compare text case-insensitively with confidence. UCase is often used to normalise both sides of a comparison:
If UCase(Range("A1").Value) = UCase(Range("B1").Value) Then. Note that VBA's default Option Compare Binary already treats text as case-sensitive, so wrapping both values in UCase is the honest way to ignore case rather than relying on Option Compare Text. - Save the workbook as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). Saving as the ordinary .xlsx format strips the code without warning.
- Run without the editor. To keep the conversion as a repeatable one-click action, assign the procedure to a shape or a button: right-click the shape, choose Assign Macro, and pick your Sub. Use a Sub that reads a fixed range so it does not need a selection.
Common Mistakes
The most frequent error is mixing up UCase with UCASE written as a worksheet function. UCase is a VBA function; in a cell you would use =UPPER(A1) instead. They do the same job but do not live in the same place.
Second, people write UCase Range("A1") without parentheses and hit a syntax error, because a function call that returns a value needs parentheses when used on the right of an assignment. Third, UCase on a cell containing a number or date converts the displayed value to text, which can turn a genuine number into a string and break later calculations. Check the cell type first if it matters.
Fourth, running a loop that overwrites cells without a backup can destroy data permanently, especially if the source column feeds formulas elsewhere. Fifth, code in a worksheet module does not appear in the Macro list by default, which leaves people convinced their macro is missing. Put reusable procedures in a standard module. Finally, a disabled macro will simply do nothing when you click a button, so check the trust settings before assuming the code is wrong.
Tips
- UCase works character by character and respects the system locale for a small number of characters, so results for accented letters can differ between machines. Test with real data rather than assuming.
- Combine UCase with Trim to remove stray spaces:
UCase(Trim(Range("A1").Value)). This is useful before matching IDs, postcodes, or email addresses. - Use
Application.WorksheetFunction.Upperonly if you specifically want the worksheet engine; for typical VBA work, plain UCase is faster and simpler. - Keep a helper procedure that takes a string argument so you can reuse it:
Function ToCaps(s As String) As String: ToCaps = UCase(s): End Function. - When converting a range, turn off screen updating first with
Application.ScreenUpdating = Falseand turn it back on at the end. It makes loops noticeably faster on large ranges. - Test on a copied sheet with a small range before running anything across thousands of rows.
Troubleshooting
The message box never appears. Macros are probably disabled. Check the Trust Center macro settings and the yellow security bar above the sheet. Only change these settings if your organisation allows it.
Compile error: Expected: end of statement. A function call is missing parentheses, or a string literal is missing its closing quote. Check the line the editor highlights.
Runtime error 13, type mismatch. The cell holds an error value such as #N/A rather than text. Wrap the read in a check: If Not IsError(Range("A1").Value) Then before calling UCase.
The macro runs on the wrong sheet. Unqualified Range references act on the active sheet. Qualify them explicitly, for example Worksheets("Data").Range("A1"), so the code targets the sheet you meant no matter what is on screen.
Macros ran once and now do nothing. If you saved as .xlsx, the code was discarded. Reopen the last .xlsm copy and save again in the macro-enabled format.
If that did not work, step through the procedure line by line with F8 to see which line fails, and confirm the workbook is not protected and the sheet is not read-only. If the workbook is shared or protected, unlock it first or ask the owner.
Frequently Asked Questions
Does UCase change the original cell?
No. UCase returns a new string. The cell only changes if you assign the result back to it, as in Range("A1").Value = UCase(Range("A1").Value).
What is the difference between UCase and the UPPER worksheet function?
They produce the same conversion. UCase is used in VBA code, while UPPER is written in a worksheet formula such as =UPPER(A1). You cannot call UCase as a formula in a cell.
Can I uppercase only part of a string?
Yes, by combining UCase with Left, Mid, or Right. For example, Left(s, 1) & UCase(Mid(s, 2)) capitalises only the first character and capitalises the rest.
How do I convert an entire column?
Loop through the used cells in that column, or read the range into a variant array, uppercase each item, then write it back. On large ranges, turning off screen updating makes the loop much faster.
Will UCase work on numbers and dates?
The result is text, so a number becomes a text string and a date becomes the text of its displayed format. Convert only text columns unless you deliberately want strings.
Does UCase handle non-English letters?
It generally does, but certain accented and special characters depend on the machine's locale settings. Verify with a sample of your real data before relying on it.