Preserve Variant type when copying to Range
Question
Reduced to its bare essentials, this is my code:
Public Sub test_sub()
Dim myVar As Variant
myVar = "1/1"
Range("A1") = myVar
End Sub
My problem is that my string "1/1" is being rendered in Excel as a date:
when I want it to be displayed as just "1/1".
How could I direct Excel not to reinterpret string values when I copy a Variant array to an Range?
N.B. I know that I could prefix my string with a ` to achieve this, but in my full code I'm actually copying quite a large array to the Range, so I'm hoping there's a method which doesn't involve looping over the whole array and adding in these ticks.
Solution
Quick and dirty, If you know the range you are going to paste the array into, you could format it as text first
Range("A1").NumberFormat = "@"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow