Array Overflow Errow

VBA_Cancer

New Member
Joined
Nov 6, 2017
Messages
17
Code:
Sub Searching()
Dim FilePath As String
Dim strFilename As String: strFilename = "C:\Users\Jack\Desktop\test.txt"
Dim strTextLine As String
Dim iFile As Integer: iFile = FreeFile
Dim income_a(10) As Integer, i As Integer
i = 0
Open strFilename For Input As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile]#iFile[/URL] 
Do Until EOF(1)
    Line Input [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , strTextLine
    If Split(strTextLine, ",")(0) = searchtxt.Value Then
        If IsNumeric(Split(strTextLine, ",")(1)) Then
        income_a(i) = CInt(Split(strTextLine, ",")(1))
        i = i + 1
        End If
    End If
Loop
allp.Value = WorksheetFunction.Sum(income_a)
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFile]#iFile[/URL]

The complete file that is being open is this:
Code:
ABC123,30000,25,15
ABC123,50000,x,...
ABC123,45000,22,32
ABC123,...,23,14

I been trying to fetch all of the integers in the 2nd position of each line and store them in an array. This keeps giving me an overflow error and I don't know why!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Without looking too much, you get that when you have infinite loops.

I'm guessing you need an i instead of a 1:

Code:
[COLOR=#333333]Do Until EOF([/COLOR][COLOR=#ff0000][B]i[/B][/COLOR][COLOR=#333333])[/COLOR]
 
Upvote 0
Without looking too much, you get that when you have infinite loops.

I'm guessing you need an i instead of a 1:

Code:
[COLOR=#333333]Do Until EOF([/COLOR][COLOR=#ff0000][B]i[/B][/COLOR][COLOR=#333333])[/COLOR]

when I change it to i, it gives me "bad file name or number" error. Also I thought this is not an infinite loops as it goes through all of the lines?

I am pretty new to excel vba, this is what I understand from what I found on the internet.
 
Upvote 0
Ignore me I should have spotted it was End Of File. I'll have a closer look...
 
Upvote 0
Tested your code and it does indeed go through each line of the file.

On Which line does the error occur?
 
Upvote 0
This keeps giving me an overflow error and I don't know why!

Hi, welcome to the forum.

Integers can only store numbers up to 32,767 - from your sample file it seems you are trying to store numbers larger than that.

Try changing this line to:

Rich (BB code):
Dim income_a(10) As Long, i As Integer

And this line to:

Rich (BB code):
income_a(i) = CLng(Split(strTextLine, ",")(1))
 
Last edited:
Upvote 0
Hi, welcome to the forum.

Integers can only store numbers up to 32,767 - from your sample file it seems you are trying to store numbers larger than that.

Try changing this line to:

Rich (BB code):
Dim income_a(10) As Long, i As Integer

And this line to:

Rich (BB code):
income_a(i) = CLng(Split(strTextLine, ",")(1))

Schoolboy error. Missed that :oops:
 
Upvote 0
Hi, welcome to the forum.

Integers can only store numbers up to 32,767 - from your sample file it seems you are trying to store numbers larger than that.

Try changing this line to:

Rich (BB code):
Dim income_a(10) As Long, i As Integer

And this line to:

Rich (BB code):
income_a(i) = CLng(Split(strTextLine, ",")(1))


Thank you! :) I been spoiled by Python arrays which takes in anything!
 
Upvote 0
Thank you! :) I been spoiled by Python arrays which takes in anything!

Hi, happy to help :)

Not suggesting that you should, but you could have declared your array as Variant which would pretty much take anything.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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