Worksheet ranges> vba arrays, what mistake am I making?

andydtaylor

Active Member
Joined
Feb 15, 2007
Messages
360
Office Version
  1. 2016
Hi guys,

What am I doing wrong here? Runtime error 13 type mismatch. I get that the error says I am declaring my variables wrong, but I still can't see it.

Thanks,


Code:
Sub Permulations()

Dim alloc_series() As Variant
Dim comparison() As Variant
Dim measure() As Variant
With ThisWorkbook.Worksheets("Inputs")
    alloc_series = .Range("Alloc_series")
    comparison = .Range("Comparison")
    measure = .Range("measure")
End With



End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I'm not sure but could you try with making a variable of type range and assigning the named range that variable using 'Set'. After that assign the variable to the array.
This is what i mean:
Code:
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
With ThisWorkbook.Worksheets("Inputs")
Set rng1 = .Range("Alloc_series")
Set rng2 = .Range("Comparison")
Set rng3 = .Range("measure")
alloc_series = rng1
comparison = rng2
measure = rng3
End With
 
Upvote 0
Try:
Rich (BB code):
Sub Permulations_v1()

Dim alloc_series()  as Variant
Dim comparison()  as Variant
Dim measure()      as Variant

With ThisWorkbook.Sheets("Inputs")
    alloc_series = .Range("Alloc_series").Value
    comparison = .Range("Comparison").Value
    measure = .Range("measure").Value
End With

End Sub
 
Upvote 0
Hi guys,

What am I doing wrong here? Runtime error 13 type mismatch. I get that the error says I am declaring my variables wrong, but I still can't see it.

Thanks,


Rich (BB code):
Sub Permulations()

Dim alloc_series() As Variant
Dim comparison() As Variant
Dim measure() As Variant
With ThisWorkbook.Worksheets("Inputs")
    alloc_series = .Range("Alloc_series")
    comparison = .Range("Comparison")
    measure = .Range("measure")
End With

End Sub
Remove the parentheses highlighted in red above... you do not want your variables to be an array of variants (each element of which would contain something), you want them to be variants that end up being assigned a single array each.
 
Upvote 0

Forum statistics

Threads
1,215,368
Messages
6,124,520
Members
449,169
Latest member
mm424

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