Is there a way to upper case a VBA array without a loop?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,535
Office Version
  1. 365
Platform
  1. Windows
Is there a way to convert a string array to upper case in a single statement? Or do I have to do it in a loop?

Thanks
 
Try this
VBA Code:
Sub test()
Dim arr As Variant
arr = Split(UCase(Join(Application.Transpose(Range("C5:C20").Value2), "|")), "|")
Range("F5").Resize(UBound(arr)) = Application.Transpose(arr)
End Sub
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You changed the range from a column to a row.
So I did. Crap. IMHO, that should not make one d*amned bit of difference. If C5:C20 is a 16x1 array, then C5:X5 should be a 1x22 array. This is what is so maddening about Excel and VBA. 😣😖💩

VBA Code:
arr=evaluate("upper(C5:C20)")
?ubound(arr,1)
 16
?ubound(arr,2)
 1
arr=evaluate("upper(C5:X5)")
?ubound(arr,1)
 22
?ubound(arr,2)
'Error subscript out of range

In any case, Peter's solution still works on my row range, but I will have to deal with a 2-dimensional array.
 
Upvote 0
Try this
VBA Code:
Sub test()
Dim arr As Variant
arr = Split(UCase(Join(Application.Transpose(Range("C5:C20").Value2), "|")), "|")
Range("F5").Resize(UBound(arr)) = Application.Transpose(arr)
End Sub
Thanks. That probably works, but Peter's solution is easier for me to grasp. Thanks
 
Upvote 0
Rory has provided the explanation.
To give a choice:
VBA Code:
Dim arr As Variant

'1-d array from column
arr = Application.Transpose(Evaluate("upper(C5:C20)"))

'1-d array from row
arr = Evaluate("upper(D4:G4)")
Perfect. I've moved the solution to here. Thanks.
 
Upvote 0
I couldn't agree more.
 
Upvote 0

Forum statistics

Threads
1,215,417
Messages
6,124,777
Members
449,187
Latest member
hermansoa

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