Convert variant array to string array

spidaman

Board Regular
Joined
Jul 26, 2015
Messages
116
Office Version
  1. 365
Platform
  1. Windows
I have an array of 5 variant objects (text from userform textboxes) that I need to convert to string objects so that I can subsequently use the Left function with them. I found a thread posted by Mark O'Brien in 2002 about this (Convert Variant Array to String Array) but cannot get it to work.

Can anyone help convert my array to string objects please?

VBA Code:
Sub ArraytoString()

Dim Targets(1 To 5) As Variant
Dim MyTArray(1 To 5) As String
Dim i As Integer

Set Targets(1) = frmRapid1.txtNum1
Set Targets(2) = frmRapid1.txtNum2
Set Targets(3) = frmRapid1.txtNum3
Set Targets(4) = frmRapid1.txtNum4
Set Targets(5) = frmRapid1.txtNum5

ReDim MyTArray(UBound(Targets)) As String   'throws error array already dimensioned

For i = 1 To UBound(Targets)
MyTArray(i) = CStr(Targets(i))
Next

End Sub

Any help much appreciated.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
It says it already dimensioned because you did that with the Dim statement when you put the 1 to 5 in it. Thats dimensioning the array.
 
Upvote 0
It says it already dimensioned because you did that with the Dim statement when you put the 1 to 5 in it. Thats dimensioning the array.
Thanks steve the fish not familiar with re-dimensioning arrays. Got it working now, much appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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