Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA ActiveWorkbook / RefersToLocal - 1004 Application defined or object defined error

Tags:

excel

vba

When executing the following line, the 1004 runtime error (Application defined or object defined error) is thrown

rangeString = ActiveWorkbook.Names.Item("MyTableName").RefersToLocal

I cant see anything wrong with my code, and the table name appears to be correct. This code is inside a class module and I am not a vba expert, so I dont know if that introduces any issues with scope etc.

like image 219
Neil P Avatar asked Aug 12 '14 09:08

Neil P


3 Answers

If your object (MyTableName) is an Excel data table, use the Range used by the ListObject, e.g.

Sub test()
    Dim L As ListObject

    Set L = ActiveSheet.ListObjects("MyTableName")
    ' at this point you can inspect the properties of L in the debugger

    ' retrieve the local address of tue underlying range
    Debug.Print L.Range.AddressLocal

End Sub
like image 191
MikeD Avatar answered Nov 01 '22 14:11

MikeD


Please do not select this as an answer. This is only for value add :)

If you just have the name of the table and are not sure which sheet the table is in then you can try this

Sub Sample()
    Dim oSh As Worksheet
    Dim oLo As ListObject

    For Each oSh In ThisWorkbook.Worksheets
        For Each oLo In oSh.ListObjects
            If oLo.Name = "MyTableName" Then
                Debug.Print "=" & oSh.Name & "!" & oLo.Range.Address
                Exit For
            End If
        Next
    Next
End Sub
like image 26
Siddharth Rout Avatar answered Nov 01 '22 16:11

Siddharth Rout


I think Range Object can access any table at any sheet just fine:

Debug.Print "=" & Range("MyTableName").Parent.Name & "!" & Range("MyTableName").Address
like image 25
L42 Avatar answered Nov 01 '22 15:11

L42