首页 > 解决方案 > Excel - 下标超出范围

问题描述

语境:

我正在将数据库中的数据导出为 .csv 文件,将其复制到主工作簿,格式化数据,然后将格式化的数据复制到另一张工作表。

错误代码:

ActiveWorkbook.Worksheets("Sheet3").AutoFilter.Sort.SortFields.Clear

如下图所示:

    Sub weekly_export_cleanup()
'
' weekly_export_cleanup Macro
'

'
    Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    Columns("H:L").Select
    Selection.Delete Shift:=xlToLeft
    Columns("J:K").Select
    Selection.Delete Shift:=xlToLeft
    Columns("K:Q").Select
    Selection.Delete Shift:=xlToLeft
    Cells.Select
    Cells.EntireColumn.AutoFit
    Columns("D:D").Select
    Selection.FormatConditions.Add Type:=xlTextString, String:="(PO", _
        TextOperator:=xlContains
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Range("D1").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Sheet3").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet3").AutoFilter.Sort.SortFields.Add(Range( _
        "D1:D15"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = _
        RGB(255, 199, 206)
    With ActiveWorkbook.Worksheets("Sheet3").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

标签: excelvba

解决方案


推荐阅读