I want to return a ListObject from a function but it seems like it implicitly converts it to a string. I am confused. Can someone point out what I am doing wrong?
Sub TestMyFunction()
Dim MyTable As ListObject
Set MyTable = RangeToTable
Debug.Print MyTable '---MyTable = "Table1"
MyTable.Unlist
End Sub
Private Function RangeToTable() As ListObject
Dim wks As Worksheet
Set wks = ActiveWorkbook.Worksheets(sREPORT_DATA_1)
Dim rngTable As Range
Set rngTable = wks.Range("A1").CurrentRegion
Set RangeToTable = wks.ListObjects.Add(xlSrcRange, rngTable, , xlYes)
End Function
Addressproperty will return a string corresponding to the range.MyTableit printsTable1as a string. But I need to return a table object, not the table name. My knowledge of VBA is progressing but I may not be understanding something.ListObjectvia the property. When I instantiate the class I want to set the class property = toMyTable.Debug.Print MyTableprints the value of the default property of MyTable - in this caseName, which is "Table1". This is similar to what you'd get using (eg) aRangeobject:Debug.Print Range("A1")will print the value from A1 becauseValueis the default property ofRange. See: cpearson.com/excel/DefaultMember.aspx You can useTypeName(MyTable)to confirm the type of your variable.