(VBA) Range.Value = String still returns numeric

gifariz

Board Regular
Joined
May 2, 2021
Messages
112
Office Version
  1. 365
Platform
  1. Windows
Hi all. I have simple question but couldn't solve it.
I want to return a Variant array with String data (Variant/String) into sheet, but it keeps being written as numeric in the sheet.
I could actually use text conversion function in sheet, but it would be great if the vba could return it as String directly.

Here is the code example:
VBA Code:
Sub trial()
Dim Data()
ReDim Data(1 To 2, 1 To 1)
Data(1, 1) = 1      'Integer
Data(2, 1) = "1"    'String
Range("A1:A2").Value = Data
End Sub

Thank you in advance.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
VBA Code:
Sub trial()
Dim Data()
ReDim Data(1 To 2, 1 To 1)
Data(1, 1) = "'" & 1     'String
Data(2, 1) = "'" & "1"   'String
Range("A1:A2").Value = Data
End Sub
 
Upvote 0
Solution
You could also do this:

VBA Code:
Sub InserVar()

    Dim iv As Integer
    iv = 1
    
    Cells(1, 1).Value = CStr(iv)
    Cells(2, 1).Formula = "=" & iv
    

End Sub
 
Upvote 0
And since it is always good to have options:
(format the cells as Text)

Rich (BB code):
Sub trial()
Dim Data()
ReDim Data(1 To 2, 1 To 1)
Data(1, 1) = 1      'Integer
Data(2, 1) = "1"    'String

With Range("A1:A2")
    Range("A1:A2").NumberFormat = "@"
    Range("A1:A2").Value = Data
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,086
Members
448,944
Latest member
sharmarick

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