Strange Listobject behaviour

aSwiftSolution

New Member
Joined
Aug 13, 2015
Messages
19
Hi all, hope some one can help. Recently I have begun working with ListObjects, handy things definitely but they seem temperamental. In my current project i have a handful of them to store lists of data, my code seems to manage them well adding entries, removing entries etc but after around 6 or 7 iterations of a process excel throws up an error. The example below has just run perfectly 4 times then thrown up an error on the 5th iteration, any help would be greatly appreciated.

Run-time error '1004':
Application-defined or object-defined error

Snippet: *Debug highlight in bold
Code:
        With tmpTable
            Select Case .ListRows.Count
                Case Is = 0
                    Set rng_TargetRow = .InsertRowRange
                Case Else
                    If WorksheetFunction.CountA(.ListRows(.ListRows.Count).Range) > 0 Then
                        Set rng_Prt1 = Cells(.HeaderRowRange.Row, .ListColumns(1).Range.Column)
                        Set rng_Prt2 = Cells(.ListRows(.ListRows.Count).Range.Row + 1, .ListColumns(.ListColumns.Count).Range.Column)
                        [SIZE=3][I][B][U].Resize Range(rng_Prt1, rng_Prt2)[/U][/B][/I][/SIZE]
                    End If
                    Set rng_TargetRow = .ListRows(.ListRows.Count).Range
            End Select
        End With

Full Route:
Code:
Sub AllocationMgr_TransferIn(ByVal TargetArea As String, ByVal TargetGame As String)
'---------------------------------------------------------------------------------------
' Procedure : AllocationMgr_TransferIn
' Author    : 
' Date      : 18/11/2015
' Purpose   : To take a default game, index it, then allocate to the chosen area
'---------------------------------------------------------------------------------------
Dim tmpTable As ListObject

Dim rng_TargetRow As Range, _
    rng_Prt1 As Range, _
    rng_Prt2 As Range

Dim LoopIndex As Byte, _
    GameIndex As Integer
    
Dim IndexedGame_str As String

Dim Unique As Boolean
    
    If ErrHandling = True Then On Error GoTo ErrHandler Else On Error GoTo 0
    
    '//Generate an indexed name of the default game
    GameIndex = 1
    Do Until Unique = True
        IndexedGame_str = TargetGame & " (" & GameIndex & ")"
        'If WorksheetFunction.CountIf(IndexedGames_tbl.ListColumns(1).DataBodyRange, IndexedGame_str) = 0 Then
        If WorksheetFunction.CountIf(Range(IndexedGames_tbl.Name & "[#Data]"), IndexedGame_str) = 0 Then
            Unique = True
        Else
            Unique = False
            GameIndex = GameIndex + 1
        End If
    Loop
    
    '//Add to IndexedGames_tbl & SingleAllocation_tbl
    For LoopIndex = 1 To 2
        Select Case LoopIndex
            Case 1
                Set tmpTable = Sheet2.ListObjects(IndexedGames_tbl.Name)
            Case 2
                Set tmpTable = Sheet2.ListObjects(SingleA_tbl.Name)
        End Select
        
        With tmpTable
            Select Case .ListRows.Count
                Case Is = 0
                    Set rng_TargetRow = .InsertRowRange
                Case Else
                    If WorksheetFunction.CountA(.ListRows(.ListRows.Count).Range) > 0 Then
                        Set rng_Prt1 = Cells(.HeaderRowRange.Row, .ListColumns(1).Range.Column)
                        Set rng_Prt2 = Cells(.ListRows(.ListRows.Count).Range.Row + 1, .ListColumns(.ListColumns.Count).Range.Column)
                        .Resize Range(rng_Prt1, rng_Prt2)
                    End If
                    Set rng_TargetRow = .ListRows(.ListRows.Count).Range
            End Select
        End With
        
        rng_TargetRow.Cells(1, 1).Value = IndexedGame_str
        
        Call SortTable(TargetTable:=tmpTable, KeyRng1:=tmpTable.ListColumns(1).DataBodyRange)
        tmpTable.HeaderRowRange.EntireColumn.AutoFit
        
    Next LoopIndex
    
    '//Add to GroupAllocation
    Call Sync_AllocationTables(Single2Group:=True, TargetArea:=TargetArea)
    Call AllocationMgr_ListBox_Link
    
ExitRoute:

    Set tmpTable = Nothing
    Set rng_Prt1 = Nothing
    Set rng_Prt2 = Nothing
    Set rng_TargetRow = Nothing
    
    On Error GoTo 0
    Exit Sub
    
ErrHandler:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure AllocationMgr_TransferIn of Module F13_AllocationMgr"
End Sub
 

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.
Just to add a note, upon the error both rng_Prt1 & rng_Prt2 have populated as expected, seems to be an issue actually resizing the table.
 
Upvote 0
I haven't been able to re-produce your error. Does this help?

Code:
[color=darkblue]With[/color] tmpTable
    [color=darkblue]Select[/color] [color=darkblue]Case[/color] .ListRows.Count
        [color=darkblue]Case[/color] [color=darkblue]Is[/color] = 0
            [color=darkblue]Set[/color] rng_TargetRow = .InsertRowRange
        [color=darkblue]Case[/color] [color=darkblue]Else[/color]
            [color=darkblue]Set[/color] rng_TargetRow = .ListRows(.ListRows.Count).Range
            [color=darkblue]If[/color] WorksheetFunction.CountA(rng_TargetRow) > 0 [color=darkblue]Then[/color]
                [color=darkblue]Set[/color] rng_TargetRow = .ListRows.Add.Range
            [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]End[/color] [color=darkblue]Select[/color]
[color=darkblue]End[/color] [color=darkblue]With[/color]
 
Upvote 0
Thank you for your reply Domenic. Unfortunately no, i swapped out the original with the section you posted and again it ran perfectly for the first 4 times but on the 5th it threw up the error 'Run-time error '-2147417848 (80010108)': Method 'Add' of object 'ListRows' failed'. Pressing debug highlights 'Set rng_TargetRow = .ListRows.Add.Range'. IF i press F8 then debug again the same line is highlighted but the error changes to 'Run-time error '1004': Application-defined or object-defined error'. Either was excel has hung and can only be closed via task manager.

Another example of this strange ListObject behavior can be seen in a routine i use to reset tables, again it runs fine a few times then errors. The part highlighted by debug is '.DataBodyRange.delete', this is after passing the 'If Not .DataBodyRange is Nothing then' and the table does have a populated data range.

Code:
Sub ResetTable(ByVal TargetTable As ListObject)
'---------------------------------------------------------------------------------------
' Procedure : ResetTable
' Author    :
' Date      : 20/11/2015
' Purpose   : To reset any targeted table
'
' Comments  :
'---------------------------------------------------------------------------------------

    '//Invoke error handling
    If ErrHandling = True Then On Error GoTo ErrHandler Else On Error GoTo 0
    
    '//Delete DataBodyRange if present
    With TargetTable
        If Not .DataBodyRange Is Nothing Then .DataBodyRange.Delete
    End With
    
    On Error GoTo 0
    Exit Sub
    
ErrHandler:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ResetTable of Module Y2_Misc_Table"
End Sub
 
Upvote 0
After extensive testing and amending my cause thoughts are
  • Possibly something to do with needing to re-state the ListObjects after dimensional changes (although some routines use procedure level ListObject other routines use module level ListObjects)
  • Perhaps having two different active ListObjects variables (in separate routines) reference the same table is causing some sort of clash?
 
Upvote 0
So it looks like it doesn't like the addition of a listrow and assigning its range to a variable all in one go. That's strange, though, I tested it in Excel 2013, 2010 and 2007, and it works fine. In any case, I guess the best practice is to split it up. But it doesn't look that it would help, since you say you have the same sort of problem with your ResetTable procedure. Again, I tested it many times, with and without data in the databody range, and everything seemed fine. It ensures that there's always a blank row in which to enter data. I also tested it with the line invoking the error handling commented out and, again, no errors.
 
Upvote 0
Yeah I'm stumped, to the best of my knowledge it should work and from your testing obviously it does, just not consistently in my project. I read a post where the writer stated as a rule he only puts one table per worksheet, mine is 5 on 1, i don't think it should make a difference (never heard it would) but who knows I've tried everything else. Would you mind checking back in a day or 2, really appreciate your input in trying to identify whats going on.
 
Upvote 0
I don't think having more than one table in a worksheet would be a problem. I thought an error might occur if you're trying to add a row when there's either no row or only one row between tables. But the second table just gets pushed downward. No error occurs. So something else must be going on to cause your error.
 
Upvote 0
Yeah i didn't think the tables location would be an issue either. I don't suppose there is a way for me to share my workbook with you for you to take a look?
 
Upvote 0
I don't think I'll have the time to look through your workbook but if you provide a link someone else may have a look...
 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

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