excel Error91中的VB脚本

时间:2016-02-15 23:23:27

标签: excel-vba vba excel

当我运行宏时,它会抛出运行时错误' 91':对象变量或With block变量未设置。

Sheets("POC").Select
Range("A2:M3000").Select
With Selection
    .HorizontalAlignment = xlLeft
    .VerticalAlignment = xlTop
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With

**ActiveWorkbook.Worksheets("POC Journal").AutoFilter.Sort.SortFields.Clear**
ActiveWorkbook.Worksheets("POC").AutoFilter.Sort.SortFields.Add Key:= _
    Range("K1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
    xlSortNormal
With ActiveWorkbook.Worksheets("POC").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With 

1 个答案:

答案 0 :(得分:0)

应该是工作表(“POC期刊”)还是工作表(“POC”)?

如果名称正确,可能是因为自动过滤器没有打开。您可以使用以下代码对其进行测试并启用它:

 If ActiveWorkbook.Worksheets("POC Journal").AutoFilterMode = False Then
        Selection.AutoFilter
 End If