Run-time error1004 in code when searching through large data set

ijhoeq

Board Regular
Joined
Jun 20, 2018
Messages
61
Hello,

I have a command button in my workbook that runs code which searches through a column of data (each cell in the column is text inputted in the form of a few sentences) and creates a word count on another sheet in the workbook. The code is written so that every time if finds a new word it is added to the new sheet and it counts how many time that word appears in the column. It then sorts the newly created word count table in order from most occurring word to least occurring. This code runs for two different columns ("Failure" column and "Maintenance Action" column).

The code works great until the data set is around 12,000+ cells long. When it is this long I get an error of the following type: "Run-time error '1004': Application-defined or object-defined error." When I click "Debug" it highlights the line "Sheets("Maintenance Action Word Counts").Range("A1").Resize(.Count, 2).Value = Application.Transpose(Array,.keys,.items))" It never has an error on the Failure Word Count which is of the same length as the Maintenance Action Word Count.

Please look at the code below and let me know if you have any suggestion or further questions.

Code:
    'Failure COUNTS CODE
    Dim Ary As Variant, Sp As Variant
    Dim I As Long, j As Long
   
    With Sheets("Word Search")
        Ary = .Range("C2", .Range("C" & Rows.Count).End(xlUp)).Value2
    End With
    With CreateObject("scripting.dictionary")
        For I = 1 To UBound(Ary)
            Sp = Split(Ary(I, 1))
        For j = 0 To UBound(Sp)
            .Item(Sp(j)) = .Item(Sp(j)) + 1
        Next j
        Next I
            Sheets("Failure Word Counts").Range("A1").Resize(.Count, 2).Value = Application.Transpose(Array(.keys, .items))
    End With
    
    'Sort in order from largest to smallest
    LastRow = Worksheets("Failure Word Counts").Range("B" & Rows.Count).End(xlUp).Row
    Worksheets("Failure Word Counts").Range("A1:B" & LastRow).Sort _
    key1:=Worksheets("Failure Word Counts").Range("B:B"), order1:=xlDescending, Header:=xlNo
    
'-----------------------------------------------------------------------------------------------------
    'Maintenance Action COUNTS CODE
    With Sheets("Word Search")
        Ary = .Range("D2", .Range("D" & Rows.Count).End(xlUp)).Value2
    End With
    With CreateObject("scripting.dictionary")
        For I = 1 To UBound(Ary)
            Sp = Split(Ary(I, 1))
        For j = 0 To UBound(Sp)
            .Item(Sp(j)) = .Item(Sp(j)) + 1
        Next j
        Next I
'CLICKING DEBUG HIGHLIGHTS THE FOLLOWING LINE
            Sheets("Maintenance Action Word Counts").Range("A1").Resize(.Count, 2).Value = Application.Transpose(Array(.keys, .items))
    End With
    
    'Sort in order from largest to smallest
    LastRow = Worksheets("Maintenance Action Word Counts").Range("B" & Rows.Count).End(xlUp).Row
    Worksheets("Maintenance Action Word Counts").Range("A1:B" & LastRow).Sort _
    key1:=Worksheets("Maintenance Action Word Counts").Range("B:B"), order1:=xlDescending, Header:=xlNo

Thank you in advance!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
If you put this just before the line that fails, what does the message box say?
Code:
Msgbox .Count
Also what version of Xl are you using?
 
Upvote 0
In that case I'm not sure why you would get an error. Transpose should be able to handle that amount without any problems.
 
Upvote 0
Could it have anything to do with using I and j or Ary as variables in both For loops? Do I need to switch variables in the Maintenance Action Count For loop?
 
Upvote 0
Does this work
Code:
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(Ary)
            Sp = Split(Ary(i, 1))
        For j = 0 To UBound(Sp)
            .item(Sp(j)) = .item(Sp(j)) + 1
        Next j
        Next i
        [COLOR=#0000ff]ReDim Ary(1 To .Count, 1 To 2)
        For i = 0 To .Count - 1
            Ary(i + 1, 1) = .Keys()(i)
            Ary(i + 1, 2) = .items()(i)
         Next i[/COLOR]
            Sheets("Maintenance Action Word Counts").Range("A1").Resize(.Count, 2).Value = [COLOR=#0000ff]Ary[/COLOR]
    End With
 
Upvote 0
I get the same error with that code. It works great every single time for the Failure Word Count which has the same amount of data. I've ran it with other sets of data containing 36,000 lines and I get the same error but the message box says 24,000 instead of 11,599
 
Upvote 0
Do you get the error on the same line?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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