.unlist方法不起作用

时间:2014-12-09 03:26:03

标签: excel macos vba methods

这可能是一个简单的问题,但我无法在网上的任何地方找到答案。我试图取消工作表上的所有表。此宏失败,并显示一条消息:"编译错误:未找到方法或数据成员"因为它突出了宏的.Unlist部分。我尝试过其他变种但.Unlist似乎不想工作。我在Excel for Mac 2011(版本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 个答案:

答案 0 :(得分:1)

似乎在Excel 2011中将ListObject转换为Range的方法称为ConvertToRange

请注意,您的代码中还有其他问题

  1. 不需要使用SelectActiveSheet
  2. 也没有选择列表对象(GoTo ...
  3. 一旦listObject被UnList编辑,变量oLo将不再设置,因此MsgBox oLo.Name ...将会出错
  4. 要使代码在PC或Mac上运行,请使用条件编译

    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