0

I 'm defining an array like this and populating it directly without using any loops

But its throwing error "Type mismatch error"

Dim battarray() As Integer
x = Sheets("Names").Range("a4")
ReDim battarray(x) As Integer
battarray() = Array(40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50)

Can some one help me fix this

8
  • What's in Sheets("Names").Range("a4")? Commented Apr 30, 2014 at 6:04
  • 3
    Remove As Integer and try again. Array(40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50) creates an array of variant, which cant be assigned to an array of integers. Commented Apr 30, 2014 at 6:04
  • @bernieIn Sheets("Names").Range("a4") is an integer which gives dynamic size to the array, based on that Array(40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50) also changes Commented Apr 30, 2014 at 6:07
  • 1
    Try this x = Val(Trim(Sheets("Names").Range("a4").Value)) Commented Apr 30, 2014 at 6:07
  • 1
    What line is throwing 'Type Mismatch'? Also, did you remove As Integer from line 1 and line 3 in the sample code you posted? Commented Apr 30, 2014 at 6:22

1 Answer 1

3

Remove both instances of As Integer, and (optionally) replace them by As Variant.

Array(40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50) creates an array of type Variant. This cannot be assigned to an array of type Integer.

Note that if you're going to hard-code the contents of battarray, there's no point ReDimming the array beforehand. This is sufficient:

Dim battarray() As Variant
battarray() = Array(40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50)
Sign up to request clarification or add additional context in comments.

Comments

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.