热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

范围已转向表格,宏将无法运行-Rangehasturnedtoatableandmacrowon'trun

Ihavehadtochangetheformattingonaspreadsheetandtherangereferredtobelowisnowatable.

I have had to change the formatting on a spreadsheet and the range referred to below is now a table.

我不得不更改电子表格上的格式,下面提到的范围现在是一个表格。

Since changing this, my macro reports "Method 'Sort' of object 'Range' failed".

自更改此设置后,我的宏报告“方法'排序'对象'范围'失败”。

I can't name the table directly as the code cycles through all sheets in a spreadsheet (except excluded) and new sheets will be added that I won't know the table name/number for.

我无法直接命名表格,因为代码循环显示电子表格中的所有工作表(除外),并且将添加新工作表,我将不知道表名称/编号。

How do I select the table (location B72:L86) and sort it by column I?

如何选择表格(位置B72:L86)并按列I排序?

For Each wsSheet In ThisWorkbook.Worksheets
   Select Case wsSheet.Name
    Case "Affiliates", "New Report", "Pasted Report", "New Month Or Client", "Set Up Data"
        'Do nothing.

    Case Else
        With wsSheet
    .Range("B72:L86").Sort Key1:=.Range("I72"), Order1:=xlDescending
        End With
End Select

1 个解决方案

#1


0  

If there's only one table per sheet, you can use the .ListObjects(*index*) property to refer to it.

如果每张表只有一个表,则可以使用.ListObjects(* index *)属性来引用它。

Dim lo as ListObject
With wsSheet
    For Each lo in .ListObjects
    lo.Sort.SortFields.Add Key:=Range(lo.Name & _
    "[[#All],[" & .Range("I72").value & "]]"), SortOn:=xlSortOnValues, Order:= xlDescending
    With lo.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Next    
End With

推荐阅读
author-avatar
小賑賑_533
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有