3

I would like to get excel data into an array in VBA, so I do the following:

Dim Arr() As Variant
Arr = ActiveWorkbook.Sheets("Sheet1").Range("C28:R29")

As soon as I run this, I get a type mismatch error 13. I thought the problem was because the 1st row (2 rows in total) represents a string (header) and the 2nd row represents numbers, so I tried to only get one row like this:

Arr= ActiveWorkbook.Sheets("Sheet1").Range("C28:R28")

To no avail, I still get the same problem.

Does anyone know what could be wrong?

Regards Crouz

1 Answer 1

5

It's pretty simple, just add .Value (I discovered that trick not so long ago and I'm already fan! :) )

Arr= ActiveWorkbook.Sheets("Sheet1").Range("C28:R28").Value2
Sign up to request clarification or add additional context in comments.

4 Comments

I have never understood this. Arr=Range("C28:R28") will work but Arr= ActiveWorkbook.Sheets("Sheet1").Range("C28:R28") does not. Don't both variations return a Range object? Any thoughts?
A lot of but no useful I'm afraid, I never used the Array=Range so I don't really now but it might have to do with the data type (numerical, text, ...) that is in your range. But I can't help you more that this!
When putting ranges in arrays use Value2 rather than Value. Charles Williams explains this well at his blog.
Ok, jhere is the link : fastexcel.wordpress.com/2011/11/30/… And to sum up, .Value convert some of your data to keep dates and such, and cut your number with a 4 digits accuracy, and .Value2 is faster because it doesn't convert anything and so it can't threaten your data integrity!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.