Array - Subscript out of range..

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

Unable to store data into Array getting subscript out of range.
Adding data in dictionary, storing output in array, and printing array in Range.

My data is in 150000 rows. so trying array and Dictionary Combination.

Expected output is in H and I Columns.
'Below attempted Code.
Rich (BB code):
Sub Dict_array_Combination()

    Dim dict As New Scripting.dictionary
    Dim arr As Variant
    Dim i As Long
    Dim ary As Variant
   
    arr = Range("A2:D13").Value
   
       Dim ary_english As Variant
        Dim ary_Maths As Variant
   
        ary_english = Range("G2:G10").Value
        ary_Maths = Range("G2:G10").Value
   
   
    'Store in Dictionary
    With dict
        For i = LBound(arr, 1) To UBound(arr, 1)
            If Not .Exists(arr(i, 1)) Then
                .Add (arr(i, 1)), Array(arr(i, 2), arr(i, 4))
            End If
        Next i
   
    'Store Output in Array     'Can we store two columns into array.
        For i = LBound(ary_english, 1) To UBound(ary_english, 1)
              ary_english(i, 1) = .Item(ary_english(i, 1))  'Store English Marks.
              ary_Maths(i, 1) = .Item(ary_Maths(i, 4))  ' 'Store Maths Marks.
        Next i
    
    End With
    
    'Print Array Output in Range
    Range("H2").Resize(UBound(ary_english, 1)).Value = ary_english 'Print english Marks
    Range("I2").Resize(UBound(ary_Maths, 1)).Value = ary_Maths ''Print Maths Marks
    

End Sub

Book1
ABCDEFGHI
1StudentEnglishHindiMathStudentEnglishMath
2Salman577258Salman5758
3Shahrukh915250Shahrukh9150
4Hrithik878676Hrithik8776
5Priyanka807475Priyanka8075
6Katrina798492Katrina7992
7Alia745376dharmendra  
8Madhuri615780Madhuri6180
9Amir778070Amir7770
10Amitabh817179Govinda  
11Johny Lever718072Amitabh8179
12Juhi718072
13Karishma658575
Sheet1
Cell Formulas
RangeFormula
H2:H11H2=IFERROR(VLOOKUP(G2,$A$1:$D$13,2,0),"")
I2:I11I2=IFERROR(VLOOKUP(G2,$A$1:$D$13,4,0),"")
 
Hi Fluff,

frankly speaking, I don't have an idea of it, which to select from these =><vba/> , <rich/>,</>,

Thanks
mg
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Thanks for the tags (y)

I'd stick to your original version, but create a new array
VBA Code:
        Dim ary_student As Variant
        ary_student = Range("G2:G10").Value
        ReDim ary_english(1 To UBound(ary_student), 1 To 1)
        ReDim ary_Maths(1 To UBound(ary_student), 1 To 1)
and then populate the maths & English arrays like
VBA Code:
        For i = LBound(ary_student, 1) To UBound(ary_student, 1)
            If .exists(ary_student(i, 1)) Then
               ary_english(i, 1) = .Item(ary_student(i, 1))(0)  'Store English Marks.
               ary_Maths(i, 1) = .Item(ary_student(i, 1))(1)  ' 'Store Maths Marks.
            End If
        Next i
 
Upvote 0
I would recommend using the <vba/> tags unless you want to highlight a piece of code like
Rich (BB code):
        Dim ary_student As Variant
        ary_student = Range("G2:G10").Value
        ReDim ary_english(1 To UBound(ary_student), 1 To 1)
        ReDim ary_Maths(1 To UBound(ary_student), 1 To 1)
In which case use the <rich/> tags
 
Upvote 0
Hi Fluff,

Wow !!! Superrrrb piece of Code ?, Many many thanks for your help. ? (y)

will remember tagging next time . :)

Thanks
mg
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,669
Messages
6,126,120
Members
449,293
Latest member
yallaire64

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