2013-10-23 211 views
1

第9行出現「下標超出範圍」錯誤。我不是程序員,也不聲稱是。出於這個原因,我需要一點幫助,試圖讓我的Excel按鈕工作。我只知道一點VB。我在一個損失,因爲該按鈕會執行宏到一定的點,然後我得到這個錯誤...下標超出範圍 - Excel

------Line 9:ActiveWorkbook.Worksheets("strActiveWorksheet").Sort.SortFields.Clear----- 

下面是代碼:

' 
' MakeParetoTable Macro 
' 
Dim strActiveWorkSheet As String 
Sub MakeParetoTable() 
strActiveWorkSheet = ActiveSheet.Name 
    Range("B6:B31,I6:I31").Select 
    Range("Table2[[#Headers],[Total Quanity]]").Activate 
    Selection.Copy 
    Range("P6").Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 
    Columns("P:P").EntireColumn.AutoFit 
    ActiveWindow.ScrollColumn = 2 
    Application.CutCopyMode = False 
    ActiveWorkbook.Worksheets("strActiveWorksheet").Sort.SortFields.Clear 
    ActiveWorkbook.Worksheets("strActiveWorksheet").Sort.SortFields.Add Key:=Range("Q7:Q31"), _ 
     SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal 
    With ActiveWorkbook.Worksheets("strActiveWorksheet").Sort 
     .SetRange Range("P6:Q31") 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone 
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone 
    With Selection.Borders(xlEdgeLeft) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlEdgeTop) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlEdgeBottom) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlEdgeRight) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlInsideVertical) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlInsideHorizontal) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    Application.WindowState = xlMinimized 
    Application.WindowState = xlNormal 
    Range("Q32").Select 
    ActiveCell.FormulaR1C1 = "=SUM(R[-25]C:R[-1]C)" 
    Range("Q33").Select 
    ActiveWindow.SmallScroll ToRight:=1 
    Range("R7").Select 
    ActiveCell.FormulaR1C1 = "=RC[-1]/R32C17" 
    Range("R7").Select 
    Selection.AutoFill Destination:=Range("R7:R31"), Type:=xlFillDefault 
    Range("R7:R31").Select 
    Range("S7").Select 
    ActiveCell.FormulaR1C1 = "=RC[-1]" 
    Range("S8").Select 
    ActiveCell.FormulaR1C1 = "=R[-1]C+RC[-1]" 
    Range("S8").Select 
    Selection.AutoFill Destination:=Range("S8:S31"), Type:=xlFillDefault 
    Range("S8:S31").Select 
End Sub 

回答

4

我懷疑你拼錯"Quanity" - 應該是"Quantity"。因此,找不到該元素,並將其視爲「超出範圍」。

剛剛看到了編輯。您有一個名爲strActiveWorksheet的變量,但您正在查找名稱爲"strActiveWorksheet"的工作表。您需要刪除引號:

ActiveWorkbook.Worksheets(strActiveWorksheet).Sort.SortFields.Clear 

和下一行中的相同,以及可能在您的代碼中的其他地方。只是爲了解釋(因爲你說你是VBA中的一個新手):

類似"hello"是一個字符串(常量)。您可以將字符串賦值給變量

Dim goodbye 
goodbye = "hello" 

MsgBox goodbye 

將顯示 「你好」

MsgBox "goodbye" 

將顯示 「再見」。

有意義嗎?

+0

+1可能原因也值得檢查'strActiveWorksheet'工作表名稱拼寫,並確保您合格範圍範圍。 – 2013-10-23 15:37:25

+0

哇,你是男人。這正是你所說的。有時候額外的眼睛會變得很長! – user2912090

+1

是的,先生。這對我有意義。我只需要多練習,注意細節。 – user2912090