1

我在第 9 行收到“下标超出范围”错误。我不是程序员,也不声称自己是程序员。出于这个原因,我需要一些帮助来尝试让我的 Excel 按钮工作。我只知道一点VB。我很茫然,因为按钮将执行宏到某个点,然后我得到这个错误......

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

1 回答 1

4

我怀疑你拼错"Quanity"了——应该是"Quantity"。因此,未找到该元素,并认为该元素“超出范围”。

刚看到编辑。您有一个名为 的变量strActiveWorksheet,但您正在寻找一个名为 的工作表"strActiveWorksheet"。您需要删除引号:

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

下一行也一样,代码中可能还有其他地方。只是为了解释一下(因为你说你是 VBA 的新手):

类似的东西"hello"是一个字符串(常量)。您可以将字符串分配给变量

Dim goodbye
goodbye = "hello"

MsgBox goodbye

将显示“你好”

MsgBox "goodbye"

将显示“再见”。

说得通?

于 2013-10-23T15:35:34.120 回答