0

I am trying to take the values of a range and put them into an array. I have the following code which works great:

Dim rg As Range
Dim Inputs() As Variant
Set rg = ("A1:B10")
Inputs = rg

But when I try to use Long instead of Variant I get a Type Mismatch error, even though all of the values are numbers. Is it possible to create a Long array instead of a Variant array?

2
  • Yes, but you have to pass them to the array 1 by 1. Like iterate each range then pass to the array.
    – L42
    Commented Apr 30, 2014 at 2:08
  • If the cells contain numbers then the array contents are of Double and not Long. Commented Apr 30, 2014 at 4:59

1 Answer 1

0

Something like this perhaps:

Option Explicit
Option Base 1

Sub RangeToLong()
Dim rng As Range
Dim mylong() As Long, i As Long

Set rng = Sheet3.Range("A1:A10")
ReDim mylong(rng.Cells.Count)
'Debug.Print LBound(mylong), UBound(mylong)
For i = LBound(mylong) To UBound(mylong)
    mylong(i) = rng(i)
    'Debug.Print mylong(i)
Next
End Sub

Edit1: For 2D Long Array

Option Explicit
Option Base 1

Sub RangeToLong()
Dim rng As Range
Dim mylong() As Long, i As Long, j As Long

Set rng = Sheet3.Range("A1:B10")
ReDim mylong(1 To rng.Rows.Count, 1 To rng.Columns.Count)
'Debug.Print LBound(mylong), UBound(mylong)
For i = LBound(mylong, 1) To UBound(mylong, 1)
    For j = LBound(mylong, 2) To UBound(mylong, 2)
        mylong(i, j) = rng(i, j)
        'Debug.Print mylong(i, j)
    Next
Next
End Sub
1
  • To downvoter: Is my answer not useful? :) Sorry, I'm just wondrin.
    – L42
    Commented Apr 30, 2014 at 9:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.