运行时错误“483”“对象不支持此属性或方法”

我试图为特定的工作表有一个自定义sorting表,但我得到运行时错误“483”“对象不支持此属性或方法”。

我将工作表名称和自定义列表顺序作为来自用户的stringinput。

Option Explicit Sub SortRiskArea() Dim wk As Worksheet Dim Tb, Rb Dim shtName As String shtName = InputBox(Prompt:="Enter the Worksheet Name that you want to sort." & vbNewLine & " Ex: Risk Register ", Title:="Hello", Default:="Risk Register") shtName = Trim(shtName) Dim strName As String strName = InputBox(Prompt:="Enter the Sort Order for Risk Area" & vbNewLine & " Ex: Commercial, Technological, Management, Reputational, Governance, Operational", Title:="Hello", Default:="Commercial, Technological, Management, Reputational, Governance, Operational") strName = Replace(strName, " ", "") Set wk = Sheets(shtName) If shtName = "Risk Register" Then Tb = "Table1" If shtName = "SAP BI" Then Tb = "Table13" If shtName = "SAP BO" Then Tb = "Table14" If shtName = "SAP BW" Then Tb = "Table15" If shtName = "SAP PM" Then Tb = "Table16" If shtName = "Mobility" Then Tb = "Table17" If shtName = "SAP FI" Then Tb = "Table18" If shtName = "SAP Service Desk" Then Tb = "Table19" Rb = "[Risk Area]" Rb = Tb & Rb Error Lines > ActiveWorkbook.wk.ListObjects(Tb).Sort. _ SortFields.Add Key:=Range(Rb), SortOn:=xlSortOnValues, _ Order:=xlAscending, CustomOrder:= _ strName, _ DataOption:=xlSortNormal With ActiveWorkbook.wk.ListObjects(Tb).Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Range("B5").Select End Sub 

由于大多数元素几乎都是正确的,所以你的代码相当不吉利,但不幸的是,关键的元素缺less最后一点精确性。 列表如下:

  1. ActiveWorkbook.wk.ListObjects(Tb).Sort正在尝试访问不存在的ActiveWorkbook属性。 wk本身就是一个Sheet对象,通过它在这一行中缺lessSet wk = Sheets(shtName)就是ActiveWorkbook 。 所以wk.ListObjects(Tb).Sort行应该只是wk.ListObjects(Tb).Sort 。 更好的是,你也可以像这样明确地Set wk = ActiveWorkbook.Sheets(shtName)Set wk = ActiveWorkbook.Sheets(shtName)Set wk = ThisWorkbook.Sheets(shtName)
  2. 因为您没有明确地设置它,所以这一行Key:=Range(Rb)假设ActiveSheet与您的目标工作表相反。 所以应该说Key:=wk.Range(Rb)
  3. 自定义sorting顺序是棘手的野兽。 即使您觉得自己几乎完全复制了自动生成的macros代码,恐怕您的代码仍然无法正常工作。 它的实际工作方式是在Application对象中创build一个CustomList ,然后用一个Integer引用它的索引。 在下面的示例代码中,您将看到如何做到这一点,但是您应该知道,只有当您的自定义项目是Strings才能使用。
  4. 您的最后一行可能不会做你想做的事情,因为Range("xx").Select将再次出现在ActiveSheet上,而你希望目标工作表被选中。

其他一些更一般的编码点:

  1. 你应该明确地声明每个variables。 所以这行Dim Tb, Rb没有那么大,因为每个Variants只是增加了不必要的处理时间,并且使得debugging更加困难。
  2. 用户input框正在询问很多用户。 他/她必须确定没有单个错字或错误的工作表/自定义值input,否则会发生未处理的错误。 此任务非常适合用户Userform ,您可以将一个ComboBox与您的所有目标工作表名称和一个ListBox一起用于您的自定义订单项目。 如果您将ComboBox ColumnCount更改为2,则可以创build表名称 – 表名称映射。 也许有一个快速阅读的用户Userforms ,看看如何做到这一点; 这真的很容易。
  3. 如果您创build了Sheet to ListObject的映射,代码将更容易pipe理。 你只需要这样做一次,而且每次都可以按照自己喜欢的次数运行你的程序,而不需要所有这些If语句。 您还可以更好地控制任何更改和对象设置。

下面的代码告诉你如何做到这一切。 这不是完美的编码,但它使每个点没有过分的分心:

 Sub SortRiskArea() Dim tableMapping As Collection Dim map(1) As Variant Dim sortItems As Variant Dim sortSheet As Worksheet Dim sortObject As ListObject Dim sortKey As Range Dim sortOrder As Integer Dim userInput As String 'Create the map of sheets to tables 'Note: you'd do this at module-level if there are repeated sorts. Set tableMapping = New Collection Set map(0) = ThisWorkbook.Sheets("Risk Register") Set map(1) = map(0).ListObjects("Table1") tableMapping.Add map, map(0).Name Set map(0) = ThisWorkbook.Sheets("SAP BI") Set map(1) = map(0).ListObjects("Table13") tableMapping.Add map, map(0).Name Set map(0) = ThisWorkbook.Sheets("SAP BO") Set map(1) = map(0).ListObjects("Table14") tableMapping.Add map, map(0).Name Set map(0) = ThisWorkbook.Sheets("SAP BW") Set map(1) = map(0).ListObjects("Table15") tableMapping.Add map, map(0).Name Set map(0) = ThisWorkbook.Sheets("SAP PM") Set map(1) = map(0).ListObjects("Table16") tableMapping.Add map, map(0).Name Set map(0) = ThisWorkbook.Sheets("Mobility") Set map(1) = map(0).ListObjects("Table17") tableMapping.Add map, map(0).Name Set map(0) = ThisWorkbook.Sheets("SAP FI") Set map(1) = map(0).ListObjects("Table18") tableMapping.Add map, map(0).Name Set map(0) = ThisWorkbook.Sheets("SAP Service Desk") Set map(1) = map(0).ListObjects("Table19") tableMapping.Add map, map(0).Name 'Acquire the target sheet On Error Resume Next Do userInput = InputBox(Prompt:="Enter the Worksheet Name that you want to sort." & vbNewLine & " Ex: Risk Register ", Title:="Hello", Default:="Risk Register") sortItems = Empty sortItems = tableMapping(userInput) If IsEmpty(sortItems) Then MsgBox "Invalid entry." Loop Until Not IsEmpty(sortItems) On Error GoTo 0 Set sortSheet = sortItems(0) Set sortObject = sortItems(1) Set sortKey = sortSheet.Range(sortObject.Name & "[Risk Area]") 'Acquire the custom sort order userInput = InputBox(Prompt:="Enter the Sort Order for Risk Area" & vbNewLine & " Ex: Commercial, Technological, Management, Reputational, Governance, Operational", Title:="Hello", Default:="Commercial, Technological, Management, Reputational, Governance, Operational") userInput = Replace(userInput, " ", "") Application.AddCustomList Split(userInput, ",") sortOrder = Application.CustomListCount 'Conduct the sort With sortObject.Sort .SortFields.Clear .SortFields.Add Key:=sortKey, SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:=sortOrder, DataOption:=xlSortNormal .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 'Safe select "B5" sortSheet.Activate sortSheet.Range("B5").Select End Sub