运行特定的宏冻结Microsoft Excel

时间:2016-02-19 19:55:07

标签: excel excel-vba vba

我目前在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

1 个答案:

答案 0 :(得分:2)

是的,你已经在宏的开头用2 For循环来解决问题了 - 你循环通过~2百万个单元格来检查它们的值。相反,您应该将搜索范围限制为仅包含值中的值的区域。您已经在下面进一步说明了这一行:

   int flag = 0;
   if (*loan < 1) {
       printf("\nStarting amount must be at least one dollar.\n");
       flag = 1;
   }
   if (*rate < .1) {
       printf("Interest rate must be at least .1%.\n");
       flag = 1;
   }
   if (*years < 1) {   
       printf("Number of years must be at least 1.\n");
       flag = 1;
   }
   if (flag) {
        exit(100);
   }

所以你应该同样改变你的For循环 - 如果你愿意,你可以像为A一样定义一个变量,并检查最低单元格#是什么在F列中有一个值然后是H - 但我会显示另一种方式:

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

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