Range.Value and Range.Value2 in VBA not working with new Geography and Stocks data types for some columns

ttt123

Board Regular
Joined
May 31, 2006
Messages
120
Office Version
  1. 365
Platform
  1. Windows
When using VBA, if I want to get a table of cell values, I usually use Range.Value or Range.Value2 over the entire table range and get a 2d-array as a result. This is much, much faster than getting each cell in the table one at a time using Range.Value/Range.Value2.

However, I noticed with the new Geography and Stocks data types I cannot reliably get values using Range.Value or Range.Value2 for certain fields. Cells that contain the little card icon on the left side of the cell do not seem to work with Range.Value/Range.Value2 and instead return "Error 2015" in VBA. Range.Text returns the correct value, but it has the disadvantage of not being vectorized and can only work with a single cell at a time.

I am looking to see if anyone knows a way to retreive the correct values in VBA in a vectorized manner where I do not have to iterate through each individual cell. Currently, the best approach that I can come up with is to use Range.Value2 and then for each position where there is an error, fill it in with Range.Text. An alternate solution would be to copy the range to the clipboard then PasteSpecial->values in a temp worksheet/workbook and then get Range.Value2 for the pasted values followed by deleting the temp worksheet/workbook. Both of these methods are slow and I'd also prefer to avoid using the clipboard since it will overwrite exisitng clipboard data. Does anyone have any suggestions as to a better way to get these values in a vectorized manner?

For example, look at the following spreadsheet which uses some VBA functions for testing.

VBA Code:
Function RangeText(rng As Range) As String
    RangeText = rng.Text
End Function
Function RangeValue(rng As Range) As String
    RangeValue = rng.Value
End Function
Function RangeValue2(rng As Range) As String
    RangeValue2 = rng.Value2
End Function
Function RangeFormula(rng As Range) As String
    RangeFormula = rng.Formula
End Function
Function RangeFormula2(rng As Range) As String
    RangeFormula2 = rng.Formula2
End Function

Sample spreadsheet with ShowFormulas disabled:
Test1.png


Sample spreadsheet with ShowFormulas enabled:
Test2.png
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,215,737
Messages
6,126,573
Members
449,318
Latest member
Son Raphon

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