Subscript out of range

akhmadsabri

New Member
Joined
Jun 14, 2016
Messages
3
Hi guys,

So I am new to VBA.
I created a function to add 2 arrays and return a new array with added values.
I count the length of the arrays and resize the new array to the size of the passed arrays.
However I got subscript out of range error on the line "addedArray(k) = array1(k) + array2(k)"
When I hover over k and myCount1, it shows both = 4.
I even tried to resize addedArray to myCount+1 bit still get the same error.
Can anyone please help me understand why I got the error?

Here is the code full code:

Option Base 1
Dim emailAddress As String
Dim addedArray As Variant
Dim value3 As String
Dim myCount1 As Integer
Dim myCount2 As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim size As Integer

Function addProducts(array1 As Variant, array2 As Variant)

'Count element of array1
myCount1 = 1
For i = LBound(array1) To UBound(array1)
myCount1 = myCount1 + 1
Next

'Count element of array2
myCount2 = 1
For j = LBound(array1) To UBound(array1)
myCount2 = myCount2 + 1
Next


'added values of array if they have the same length
If myCount1 = myCount2 Then

'resize addedArray to the size of myCount1
ReDim addedArray(myCount1)
For k = 1 To myCount1
addedArray(k) = array1(k) + array2(k)
Next

addProducts = addedArray

ElseIf myCount1 <> myCount2 Then
MsgBox ("Arrays hast to be the same lengths")
End If

End Function
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
so I changed myCount1 and myCount2 to start at 0, and it seems to work fine.
I thought since I used option based 1, i need to start counting from 1 instead of 0
 
Upvote 0
What are the values of Ubound(array1) and Ubound(array2)? Same question for the Lbound values.

Is red font below a typo in your post or ....?

'Count element of array2
myCount2 = 1
For j = LBound(array1) To UBound(array1)
myCount2 = myCount2 + 1
Next
 
Upvote 0
I can't see the value of them unless i got the error. But array1 contains (1,2,3) and array2 contains (3,2,1).

Yes the red one was a typo since I copied and pasted codes.
 
Upvote 0

Forum statistics

Threads
1,215,415
Messages
6,124,764
Members
449,187
Latest member
hermansoa

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