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),"")
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Which line gives the error?
 
Upvote 0
Hi Fluff,

Below line , throwing error.
ary_Maths(i, 1) = .Item(ary_Maths(i, 4)) ' 'Store Maths Marks.

This line works.
ary_english(i, 1) = .Item(ary_english(i, 1)) 'Store English Marks.

also I am not sure how to store two values in array and print.


Thanks for your help in advance.


Thanks
mg

1596046035751.png
 
Upvote 0
Your ary_Maths is only one column wide, but you are trying to look at the the 4th column.
 
Upvote 0
Hi Fluff,

Thanks for highlighting the bug, in ary_english there are two items.

Getting error message at below.

Rich (BB code):
 'Print Array Output in Range
    Range("H2:H10").Value = ary_english 'Print english Marks  {No Output}

    'Subscript out of range
    Range("h2").Resize(UBound(ary_english)).Value = ary_english(0) 'Print Maths Marks
     Range("i2").Resize(UBound(ary_english)).Value = ary_english(1) 'Print Maths Marks

how to extract items from Ary_english into ranges.

Thanks
mg
 
Upvote 0
Why are you putting bot the sets of marks into each array?
Surely you should put the English marks into the English array & the same for maths
 
Upvote 0
Hi Fluff,

You are right , Array_English Contains maths and English subjects Score,

But afterwords I am not getting idea, how to extract in Output Array.


Thanks
mg
 
Upvote 0
You are right , Array_English Contains maths and English subjects Score,
But why?
And if you want them in one array, why are you putting exactly the same values into the maths array?
 
Upvote 0
Hi Fluff,

Below code is working giving me result as expected,
But code is very lenghty , is there any way we can shorten the Code. Thanks.


Rich (BB code):
Sub Dict_array_Combination()

    Dim dict_English As New Scripting.dictionary
    Dim dict_Maths 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 English Score and print it
    With dict_English
        For i = LBound(arr, 1) To UBound(arr, 1)
            If Not .Exists(arr(i, 1)) Then
                .Add (arr(i, 1)), arr(i, 2)  'English score
            End If
        Next i
  
            '-------Store English Marks-------
        For i = LBound(ary_english, 1) To UBound(ary_english, 1)
             ary_english(i, 1) = .Item(ary_english(i, 1))
       Next i
   
    End With
   
      Range("H2:H10").Value = ary_english  ' One go Print English Score
     
     
   
            'Store Maths Score and print it
     With dict_Maths
        For i = LBound(arr, 1) To UBound(arr, 1)
            If Not .Exists(arr(i, 1)) Then
                .Add (arr(i, 1)), arr(i, 4)   ' Maths Score
            End If
        Next i
  
            '-------Store English Marks-------
        For i = LBound(ary_Maths, 1) To UBound(ary_Maths, 1)
             ary_Maths(i, 1) = .Item(ary_Maths(i, 1))
       Next i
   
    End With
   
    Range("I2:I10").Value = ary_Maths  'One go Print Maths Score
   

End Sub
 
Upvote 0
Please edit your post to include tags, you should know that by now.
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

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