2015-11-28 76 views
0

我想爲特定工作表的表格自定義排序,但我得到運行時錯誤「483」「對象不支持這個屬性或方法「。運行時錯誤「483」「對象不支持此屬性或方法」

我將工作表名稱和自定義列表順序作爲來自用戶的字符串輸入。

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 
+0

什麼行錯誤? – Davesexcel

+0

@Davesexcel我編輯了這個問題請看看 – newguy

回答

1

你一直不走運,而在你的代碼,大多數元件與接近直角的,但不幸的是關鍵的人缺少準確的最後一點。列表如下:

  1. ActiveWorkbook.wk.ListObjects(Tb).Sort正試圖訪問ActiveWorkbook不存在的屬性。 wk本身是一個Sheet對象,由於它在這一行中不存在,所以Set wk = Sheets(shtName)假設ActiveWorkbook。所以這兩行應該是wk.ListObjects(Tb).Sort。好,你想也設置wk明確,像這樣:Set wk = ActiveWorkbook.Sheets(shtName)Set wk = ThisWorkbook.Sheets(shtName)
  2. 因爲您沒有明確設置它,這條線Key:=Range(Rb)假設ActiveSheet,而不是你的目標板。所以它應該說Key:=wk.Range(Rb)
  3. 自定義排序順序是棘手的野獸。即使您覺得您幾乎完全複製了自動生成的宏代碼,恐怕您的代碼仍然無法正常工作。它的實際工作方式是在Application對象中創建一個CustomList,然後使用Integer引用其索引。在下面的示例代碼中,您將看到如何做到這一點,但您應該意識到只有在您的自定義項目爲Strings時纔會起作用。
  4. 您的最後一行可能不會做你想做的事情,因爲Range("xx").Select將再次出現在ActiveSheet上,而您希望選擇目標工作表。

其他一些更普遍的編碼點:

  1. 你應該明確地聲明每個變量。所以這條線Dim Tb, Rb不是每個都會是Variants,這只是增加了不必要的處理時間,並且使得調試更加困難。
  2. 用戶輸入框正在詢問很多用戶。他/她必須確定沒有單個錯字或錯誤的工作表/自定義值條目,否則會發生未處理的錯誤。此任務非常適用於Userform,您可以在其中使用一個ComboBox包含您的所有目標工作表名稱和一個ListBox以及您的自定義訂單項目。如果您將ComboBox ColumnCount更改爲2,則可以創建工作表名稱 - 表名稱映射。也許有快速閱讀Userforms看看如何做到這一點;這真的很容易。
  3. 如果您創建了SheetListObject的地圖,代碼將更易於管理。你只需要這樣做一次,並且可以按照自己的喜好多次運行程序,而不必每次都發出這些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 
相關問題