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

ijhoeq

Board Regular
Joined
Jun 20, 2018
Messages
51
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!
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,877
Office Version
365
Platform
Windows
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?
 

ijhoeq

Board Regular
Joined
Jun 20, 2018
Messages
51
When I insert that code right before the line that fails I get "11,599"
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,877
Office Version
365
Platform
Windows
In that case I'm not sure why you would get an error. Transpose should be able to handle that amount without any problems.
 

ijhoeq

Board Regular
Joined
Jun 20, 2018
Messages
51
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?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,877
Office Version
365
Platform
Windows
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
 

ijhoeq

Board Regular
Joined
Jun 20, 2018
Messages
51
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,877
Office Version
365
Platform
Windows
Do you get the error on the same line?
 

ijhoeq

Board Regular
Joined
Jun 20, 2018
Messages
51
Yes the error shows up on the same line. The msgbox just displays a different value.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,731
Messages
5,488,543
Members
407,645
Latest member
suyoggore

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top