2017-01-09 83 views
-1

您好所有我繼承了這個VBA這是目前回到了我兩個錯誤:VBA在格式化表格按預期

1)運行時錯誤「91」: 對象變量或帶塊變量未設置

2)運行時錯誤'1004': 對象'範圍'的方法'偏移'失敗

VBA很長,正如我所說的,我繼承了它的道歉。

Sub SortMain() 
' 
' SortMain Macro 
' Sortingandcoloring 
' 

' 
    Sheets("Key Performance Audience Metric").Select 
    Range("B5:H5").Select 
    Selection.AutoFilter 
    ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _ 
     SortFields.Clear 
    ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _ 
     SortFields.Add Key:=Range("C5:C55"), SortOn:=xlSortOnValues, Order:= _ 
     xlDescending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
    Selection.AutoFilter 
    Range("K5:O5").Select 
    Selection.AutoFilter 
    ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _ 
     SortFields.Clear 
    ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _ 
     SortFields.Add Key:=Range("L5:L55"), SortOn:=xlSortOnValues, Order:= _ 
     xlDescending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
    Selection.AutoFilter 
    Range("R5:W5").Select 
    Selection.AutoFilter 
    ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _ 
     SortFields.Clear 
    ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort. _ 
     SortFields.Add Key:=Range("S5:S55"), SortOn:=xlSortOnValues, Order:= _ 
     xlDescending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("Key Performance Audience Metric").AutoFilter.Sort 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
    Selection.AutoFilter 
    Range("A1").Select 
    Sheets("Engagement Quality Metrics").Select 
    Range("B5:L5").Select 
    Selection.AutoFilter 
    ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _ 
     SortFields.Clear 
    ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _ 
     SortFields.Add Key:=Range("C5:C54"), SortOn:=xlSortOnValues, Order:= _ 
     xlDescending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
    Selection.AutoFilter 
    Range("O5:W5").Select 
    Selection.AutoFilter 
    ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _ 
     SortFields.Clear 
    ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _ 
     SortFields.Add Key:=Range("P5:P54"), SortOn:=xlSortOnValues, Order:= _ 
     xlDescending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
    Selection.AutoFilter 
    Range("Z5:AH5").Select 
    Selection.AutoFilter 
    ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _ 
     SortFields.Clear 
    ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort. _ 
     SortFields.Add Key:=Range("AA5:AA54"), SortOn:=xlSortOnValues, Order:= _ 
     xlDescending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("Engagement Quality Metrics").AutoFilter.Sort 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    Selection.AutoFilter 
    End With 

    Sheets("Video Views").Select 
    Range("B5:D5").Select 
    Selection.AutoFilter 
    ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort. _ 
     SortFields.Clear 
    ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort. _ 
     SortFields.Add Key:=Range("C5:C55"), SortOn:=xlSortOnValues, Order:= _ 
     xlDescending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
    Selection.AutoFilter 
    Range("H5:J5").Select 
    Selection.AutoFilter 
    ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort. _ 
     SortFields.Clear 
    ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort. _ 
     SortFields.Add Key:=Range("I5:I55"), SortOn:=xlSortOnValues, Order:= _ 
     xlDescending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("Video Views").AutoFilter.Sort 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 


    Sheets("Video Views").Select 
    Range("B5:D6").Select 
    Selection.Copy 
    Range("B5:D55").Select 
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=False 
    Application.CutCopyMode = False 
    Range("H5:J6").Select 
    Selection.Copy 
    Range("H5:J55").Select 
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=False 


    Sheets("Key Performance Audience Metric").Select 
    Range("B5:H6").Select 
    Selection.Copy 
    Range("B5:H55").Select 
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=False 
    Application.CutCopyMode = False 
    Range("K5:O6").Select 
    Selection.Copy 
    Range("K5:O55").Select 
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=False 
    Application.CutCopyMode = False 
    Range("R5:W6").Select 
    Selection.Copy 
    Range("R5:W55").Select 
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=False 
    Application.CutCopyMode = False 
    Sheets("Key Performance Audience Metric").Select 
    Range("A1").Select 
    Sheets("Engagement Quality Metrics").Select 
    Range("B5:L6").Select 
    Selection.Copy 
    Range("B5:L54").Select 
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=False 
    Application.CutCopyMode = False 
    Range("O5:W6").Select 
    Selection.Copy 
    Range("O5:W54").Select 
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=False 
    Application.CutCopyMode = False 
    Range("Z5:AH6").Select 
    Selection.Copy 
    Range("Z5:AH54").Select 
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _ 
     False, Transpose:=False 
    Application.CutCopyMode = False 
    Range("A1").Select 
    Sheets("MACROS").Select 

Sheets("Cross Platform Table").Select 
    Range("A1").Select 

Sheets("Circle Charts").Select 
Dim i As Long 
    i = Application.Intersect(Range("A:A"), ActiveSheet.UsedRange).End(xlDown).Row 
    Do While Range("A1").Offset(i, 0).Value = 0 
    Range("A1").Offset(i, 0).EntireRow.Delete xlShiftUp 
    i = i - 1 
    Loop 

Sheets("Key Performance Audience Metric").Select 
Dim j As Long 
    j = Application.Intersect(Range("V:V"), ActiveSheet.UsedRange).End(xlDown).Row 
    Do While Range("V1").Offset(j, 0).Value = 0 
    Range("V1").Offset(j, 0).EntireRow.Delete xlShiftUp 
    j = j - 1 
    Loop 

Sheets("Engagement Quality Metrics").Select 
Dim k As Long 
    k = Application.Intersect(Range("AJ:AJ"), ActiveSheet.UsedRange).End(xlDown).Row 
    Do While Range("AJ1").Offset(k, 0).Value = 0 
    Range("AJ1").Offset(k, 0).EntireRow.Delete xlShiftUp 
    k = k - 1 
    Loop 

    Sheets("MACROS").Select 
    Range("C8:D12").Select 
    With Selection.Font 
     .color = -11489280 
     .TintAndShade = 0 
    End With 
    With Selection.Font 
     .ThemeColor = xlThemeColorLight1 
     .TintAndShade = 0 
    End With 
    ExecuteExcel4Macro "PATTERNS(1,0,5287936,TRUE,2,3,0,0)" 
    ActiveCell.FormulaR1C1 = "DONE! Ready to Use!" 
    Range("A1").Select 


End Sub 
+0

每個錯誤發生在哪裏? – Rory

+0

彈出一個提示,它不會告訴我哪一行是問題 –

+0

按'F8'開始分支,然後反覆按'F8'可以進入下一行。直到發生錯誤爲止,您將知道哪一行導致問題。由記錄器完全生成的宏(因爲這看起來是)通常很難適應,應該更多地用作提示如何實現特定的任務... – Wolfie

回答

1

如果編寫得很清楚,你會發現代碼更容易診斷和適應。我不打算瀏覽整個宏,但這裏是你的代碼的開始應該是什麼樣子的示例!這從您的第一行到行Range("K5:O5").Select

Dim KPAMSheet as Worksheet 
Set KPAMSheet = Sheets("Key Performance Audience Metric") 

With KPAMSheet.AutoFilter.Sort 

    .SortFields.Clear 

    .SortFields.Add Key:=KPAMSheet.Range("C5:C55"), _ 
     SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal 

    .Header = xlYes 
    .MatchCase = False 
    .Orientation = xlTopToBottom 
    .SortMethod = xlPinYin 
    .Apply 

End With 

KPAMSheet.Range("B5:H5").AutoFilter 

注意你應該避免使用SelectSelection,這將遲早咬你!如果你花了半個小時修訂像上面的代碼,這將是更短,更清晰,不容易出錯...

看到這個職位:How to avoid using Select in Excel VBA macros

而且我將在這裏重複我的評論關於如何調試:

按F8鍵啓動子菜單,然後重複按F8鍵可以進入下一行。直到發生錯誤爲止,您將知道哪一行導致問題。我相信Mac上的等效快捷鍵是Cmd + Shift + I。或者,轉至Debug菜單並選擇Step Into

+0

我收到您提供的這一行的語法錯誤:'.SortFields.Add Key:= KPAMSheet.Range(「C5:C55」)_ SortOn:= xlSortOnValues,Order:= xlDescending,DataOption:= xlSortNormal ' –

+0

I錯過了換行符之前的逗號,現在修復了......請注意,我沒有對上面的代碼進行測試,這只是第一次通過我如何嘗試格式化它,我很不熟悉'AutoFilter'功能!佈局是否有意義? – Wolfie

+0

佈局很有意義,我會嘗試這種格式,非常感謝!不幸的是,VBA仍然有我提到的錯誤 –