Arrays and data types

parry

MrExcel MVP
Joined
Aug 20, 2002
Messages
3,355
I have data in column A from row 1 downwards. All the data in Column A is a string being FileName1, FileName2 etc. However, even though the data in column A is a string when I try to put this directly into an array it comes up with a RunTime Error 13 - Type Mismatch.

If I change the array data type to a variant then it works. Why? I know you can have arrays of any type so why is this failing when the source data is of the same type as the array. Any clues would be appreciated.

This doesnt work
Code:
Sub Test1()
'THIS FAILS WITH RUN TIME ERROR 13: TYPE MISMATCH
Dim SourceArr() As String

'Place data source into an array
If IsEmpty([a65536]) Then
SourceArr = Range("a1:a" & Range("a65536").End(xlUp).Row)
Else
SourceArr = [a1:a65536]
End If

End Sub

This works
Code:
Sub Test2()
'THIS WORKS
Dim SourceArr() As Variant

'Place data source into an array
If IsEmpty([a65536]) Then
SourceArr = Range("a1:a" & Range("a65536").End(xlUp).Row)
Else
SourceArr = [a1:a65536]
End If

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,216,740
Messages
6,132,445
Members
449,728
Latest member
teodora bocarski

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