Sum funtion returns error 13 incompatible data type

mtenorio

New Member
Joined
Sep 5, 2014
Messages
18
Hello!

I am trying to fetch data from a text file. After extracting the needed info on the file, I converted the quantity from a string to a number.

I would like to add all of the quantities, so I used the SUM function in excel VBA. However, I am getting an incompatible data type error for the sum function when the data lines reach a very large number ( around 10,000).

Can you help me on this? Below is the code I am using.

Code:
'Code for the previous month
If FileName(a, d, b) = "" Then
    Range("I7").Value = "NO DATA"
    MsgBox "There is no file found for the previous month."
Else
    Open FileName(a, d, b) For Input As #1                            'opens the sales file for the previous month
    row_number = 0
    Do Until EOF(1)                                                          'reads the text line per line until the end of file
        Line Input #1, LineFromFile
        product_previous = Mid(LineFromFile, 104, 80)                         'extracts the product name
        quantity_previous = Mid(LineFromFile, 237, 10)                        'extracts the quantity
        product_previous_all = product_previous_all & product_previous & ";"    'stores the product name into an delimited array
        quantity_previous_all = quantity_previous_all & quantity_previous & ";" 'stores the quantity into an delimited array
        row_number = row_number + 1
    Loop
    Close #1
    'Splits the extracted string
    stext_previous() = Split(product_previous_all, ";")
    snumber_previous() = Split(quantity_previous_all, ";")
    
    For I = LBound(snumber_previous) + 1 To UBound(snumber_previous) - 2
        ReDim Preserve SrtTemp_previous(I)
            'MsgBox snumber_previous(I)
            SrtTemp_previous(I) = CLng(snumber_previous(I))
            Debug.Print TypeName(SrtTemp_previous(I))
            'MsgBox SrtTemp(I)
    Next I
    
   [COLOR=#ff0000][B] Range("I7").Value = WorksheetFunction.Sum(SrtTemp_previous())[/B][/COLOR]
    
    top10_previous(1) = WorksheetFunction.Large(SrtTemp_previous(), 1)
    top10_previous(2) = WorksheetFunction.Large(SrtTemp_previous(), 2)
    top10_previous(3) = WorksheetFunction.Large(SrtTemp_previous(), 3)
    top10_previous(4) = WorksheetFunction.Large(SrtTemp_previous(), 4)
    top10_previous(5) = WorksheetFunction.Large(SrtTemp_previous(), 5)
    top10_previous(6) = WorksheetFunction.Large(SrtTemp_previous(), 6)
    top10_previous(7) = WorksheetFunction.Large(SrtTemp_previous(), 7)
    top10_previous(8) = WorksheetFunction.Large(SrtTemp_previous(), 8)
    top10_previous(9) = WorksheetFunction.Large(SrtTemp_previous(), 9)
    top10_previous(10) = WorksheetFunction.Large(SrtTemp_previous(), 10)
    
    Worksheets("Sheet1").Activate
    
    Range("M12").Value = top10_previous(1)
    Range("M13").Value = top10_previous(2)
    Range("M14").Value = top10_previous(3)
    Range("M15").Value = top10_previous(4)
    Range("M16").Value = top10_previous(5)
    Range("M17").Value = top10_previous(6)
    Range("M18").Value = top10_previous(7)
    Range("M19").Value = top10_previous(8)
    Range("M20").Value = top10_previous(9)
    Range("M21").Value = top10_previous(10)
    
    
    For l = 1 To 10
        For k = LBound(SrtTemp_previous()) To UBound(SrtTemp_previous())
            If top10_previous(l) = SrtTemp_previous(k) Then
                Worksheets("Sheet1").Activate
                Range("K" & l + 11).Value = stext_previous(k)
            Else
            End If
        Next k
    Next l
        
End If
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,831
Office Version
  1. 365
Platform
  1. Windows
What data type(s) are the values in the array you are trying to sum?
 

mtenorio

New Member
Joined
Sep 5, 2014
Messages
18
What data type(s) are the values in the array you are trying to sum?

Hi Norie. The data type is Long. I have converted the string text to a number. Please see the code I have used for the conversion.

Code:
For I = LBound(snumber_previous) + 1 To UBound(snumber_previous) - 2
        ReDim Preserve SrtTemp_previous(I)
            'MsgBox snumber_previous(I)
            SrtTemp_previous(I) = CLng(snumber_previous(I))
            Debug.Print TypeName(SrtTemp_previous(I))
            'MsgBox SrtTemp(I)
    Next I
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,831
Office Version
  1. 365
Platform
  1. Windows
No nulls in the array?
 

Watch MrExcel Video

Forum statistics

Threads
1,109,385
Messages
5,528,396
Members
409,817
Latest member
JiNXX9500

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top