(VBA) Range.Value = String still returns numeric

gifariz

Board Regular
Joined
May 2, 2021
Messages
128
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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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,223,882
Messages
6,175,166
Members
452,615
Latest member
bogeys2birdies

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