.unlist method will not work

只谈情不闲聊 提交于 2019-12-11 22:27:42

问题


This is probably a simple question, but I cannot find the answer anywhere on the net. I am trying to unlist all the tables on a worksheet. This macro fails with a message that says "Compile Error: Method or Data Member not found" as it highlights the .Unlist part of the macro. I have tried other variations but .Unlist does not seem to want to work. I am on Excel for Mac 2011 (version 14.4)

Sub UnlistAllTablesOnSheet()
    Sheets("Role 1").Select
    Dim oSh As Worksheet
    Dim oLo As ListObject
    Set oSh = ActiveSheet
    For Each oLo In oSh.ListObjects
        Application.Goto oLo.Range
        MsgBox "Table found: " & oLo.Name & ", " & oLo.Range.Address
         oSh.ListObjects(oLo.Name).Unlist
         MsgBox oLo.Name & "now unlisted"
    Next
End Sub

回答1:


It seems that the Method to convert a ListObject to a Range is called ConvertToRange in Excel 2011.

Note that there are other issues in your code

  1. use of Select and ActiveSheet is not required
  2. nor is selecting the list object (GoTo ...)
  3. once a listObject has been UnListed, the variable oLo will no longer be set, so MsgBox oLo.Name ... will error

To make the code work on either PC or Mac, use Conditional Compilation

Sub UnlistAllTablesOnSheet()
    Dim oSh As Worksheet
    Dim oLo As ListObject
    Dim nm As String
    Set oSh = Sheets("Role 1")
    For Each oLo In oSh.ListObjects
        MsgBox "Table found: " & oLo.Name & ", " & oLo.Range.Address
        nm = oLo.Name
        #If Mac Then
            oLo.ConvertToRange
        #Else
            oLo.Unlist
        #End If
        MsgBox nm & "now unlisted"
    Next
End Sub


来源:https://stackoverflow.com/questions/27370944/unlist-method-will-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!