2016-02-19 72 views
0

我目前在Microsoft Excel中使用以下宏。然而,无论何时我运行它,应用程序都会冻结很长一段时间。不太清楚是哪里的问题可能是在我下面的代码:运行特定的宏会冻结Microsoft Excel

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 
+0

使用范围'F:F'表示它适用于整个'F'列。你是否在整个列中填充了每一行“F”?如果不是,请缩小范围以仅包含包含数据的行。同样的事情适用于'H:H'。 –

+0

是的,不要为范围内的每个cel(“H:H”)做',因为它会检查*整列*。找到最后一行(类似Dim'LastRow为Integer // lastRow = cells(rows.count,1).End(xlUp).Row',并且执行Range(“H1:H”&lastRow)''等等。 (只需更改'cells()'中的'1'以匹配要检查的数据的列) – BruceWayne

+0

好的,我会尽力感谢 –

回答

2

是的,你已经在宏的开头用2个For循环在头上钉住了问题 - 你正在循环约2百万个单元格来检查它们的值。相反,您应该只将搜索范围限制在其中包含值的区域。你已经这样做了下文进一步,这条线:

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

所以,你应该改变你的For循环同样 - 如果你喜欢,你可以定义一个变量,你都做了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) in。

+1

请注意,UsedRange在技术上不是完美的解决方案,因为格式化一个没有任何值的单元被认为是工作表的UsedRange的一部分,但是,对于这样的事情,我发现实现起来更快一些,并且可以快速地使用它们来混合和匹配搜索范围,比如在这种情况下,需要找到两个列的个别限制。 –