VBA在结束前退出

时间:2016-09-27 08:15:00

标签: excel vba excel-vba

代码:一个包含两部分的命令代码,第一部分是对单元格进行排序,第二部分是重新排序已排序的单元格。

问题:第一个宏退出后,没有做重新着色。如果我评论第一部分,代码会进行重新着色。

任何想法,可能是什么问题?

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False

Dim i As Integer
Dim WName As String


''First Part
WName = ActiveWorkbook.ActiveSheet.Name
Range("B2:B21").Select
ActiveWorkbook.Worksheets(WName).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(WName).Sort.SortFields.Add Key:=Range("B2:B21"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(WName).Sort
    .SetRange Range("B2:B21")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With


''Second Part
For i = 2 To 21
    If i Mod 2 = 0 Then
        Cells(i, 2).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent3
            .TintAndShade = 0.799981688894314
            .PatternTintAndShade = 0
        End With
    Else
        Cells(i, 2).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent3
            .TintAndShade = 0.399975585192419
            .PatternTintAndShade = 0
        End With
    End If
Next i

End Sub

0 个答案:

没有答案