运行特定的macros会冻结Microsoft Excel

我目前在Microsoft Excel中使用下面的macros。 然而,无论何时我运行它,应用程序冻结了相当长的一段时间。 不太确定问题可能出现在我的代码中:

Dim a As Integer Dim b As Integer Dim c As Integer Dim d As Integer Cells.Select Selection.RowHeight = 20.25 Columns("E:E").Insert Columns("E:E").ColumnWidth = 7 Columns("J:J").Insert Columns("J:J").ColumnWidth = 7 Columns("L:L").Insert Columns("L:L").ColumnWidth = 7 Columns("M:M").Insert Columns("M:M").ColumnWidth = 7 Columns("M:M").Insert Columns("M:M").ColumnWidth = 7 Columns("L:L").Copy Range("J1").PasteSpecial xlPasteFormats Application.CutCopyMode = flase For Each cel In Range("F:F") If cel.Font.Underline = xlUnderlineStyleSingle Then cel.Value = "x" & cel.Value End If Next For Each cel In Range("H:H") If cel.Font.Underline = xlUnderlineStyleSingle Then cel.Value = "x" & cel.Value End If Next Application.ScreenUpdating = False a = Cells(Rows.Count, "C").End(xlUp).Row For b = 1 To a If IsNumeric(Cells(b, "C").Value) Then st = Cells(b, "G").Value t1 = Cells(b, "F") t2 = Cells(b, "H") v1 = 1.72 v2 = 2.1 v3 = 1.9 v4 = 1.8 v5 = 2 If InStr(st, "+10") > 0 And Left(Cells(b, "F"), 1) = "x" Then Cells(b, "E") = v1 Cells(b, "J") = v2 ElseIf InStr(st, "-10") > 0 And Left(Cells(b, "F"), 1) = "x" Then Cells(b, "E") = v3 Cells(b, "J") = v3 ElseIf InStr(st, "-5") > 0 And Left(Cells(b, "F"), 1) = "x" Then Cells(b, "E") = v5 Cells(b, "J") = v4 ElseIf Left(Cells(b, "F"), 1) = "x" Then Cells(b, "E") = v4 Cells(b, "J") = v5 ElseIf InStr(st, "+10") > 0 And Left(Cells(b, "H"), 1) = "x" Then Cells(b, "J") = v1 Cells(b, "E") = v2 ElseIf InStr(st, "-10") > 0 And Left(Cells(b, "H"), 1) = "x" Then Cells(b, "J") = v3 Cells(b, "E") = v3 ElseIf InStr(st, "-5") > 0 And Left(Cells(b, "H"), 1) = "x" Then Cells(b, "J") = v5 Cells(b, "E") = v4 ElseIf Left(Cells(b, "H"), 1) = "x" Then Cells(b, "J") = v4 Cells(b, "E") = v5 ElseIf InStr(st, "-10") > 0 Then Cells(b, "J") = v3 Cells(b, "E") = v3 Else Cells(b, "E") = 0 Cells(b, "J") = 0 End If End If Next Application.ScreenUpdating = True End Sub 

我相信这个问题可能是RAM或下面的代码片段,我已经厌倦了修改它,但没有任何运气:

  For Each cel In Range("F:F") If cel.Font.Underline = xlUnderlineStyleSingle Then cel.Value = "x" & cel.Value End If Next For Each cel In Range("H:H") If cel.Font.Underline = xlUnderlineStyleSingle Then cel.Value = "x" & cel.Value End If Next 

是的,你已经在macros的开头用2个For循环指出了问题,你正在循环200万个单元来检查它们的值。 相反,你应该限制你的search只有在其中有值的地方。 您已经在下面进一步完成了这一行:

 a = Cells(Rows.Count, "C").End(xlUp).Row 

所以你应该改变你的For循环类似的 – 如果你喜欢你可以定义一个variables,你已经做了A,并检查看最低的单元格是什么在列F中有一个值然后H – 但我会显示另一种方式:

 For Each cel In Intersect(Sheets(1).Range("F:F"), Sheets(1).UsedRange) If cel.Font.Underline = xlUnderlineStyleSingle Then cel.Value = "x" & cel.Value End If Next For Each cel In Intersect(Sheets(1).Range("H:H"), Sheets(1).UsedRange) If cel.Font.Underline = xlUnderlineStyleSingle Then cel.Value = "x" & cel.Value End If Next 

请注意,您可能需要根据您的工作表所在的索引号来更改对上面表格(1)的引用。