VBA退出前结束

代码:一个包含两部分的命令代码,第一部分对单元格进行sorting,第二部分对正在sorting的单元格进行重新着色。

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

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

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