VBA Excel Sub运行速度非常慢

时间:2015-10-05 14:38:13

标签: excel vba excel-vba

我正在尝试编写此Sub,它会更新(已知)一定数量的列但行数不确定的表。我在表格的最后一行和表格的第一列中有值,我需要进行计算。到目前为止这是我的代码: (它的运行速度非常慢)

probe process("/lib*/libc.so.6").function("open").return {
    fd=$return
    path=user_string(@entry($filename))
    printf("open %s -> $d\n", path, fd)
}

非常感谢帮助

1 个答案:

答案 0 :(得分:2)

试试这个,我已使用'//标记了我的评论,并使用标准'

注释了您现有的代码
Sub MM()

'// You can use "," to Dim multiple variables but you still
'// need to declare the data type otherwise it will default
'// to type "Variant" which can cause issues later in your code.
Dim x As Integer, y As Integer
Dim discount As Double, margin As Double

''Looks dynamically for the largest row & column index
'numrows = Range("C2", Range("C2").End(xlDown)).Rows.count
'numcols = Range("C2", Range("C2").End(xlToRight)).Columns.count

For Each sh In ActiveWorkbook.Sheets

With sh
    '// Get the row number of the last row in C and minus 1 for the header.
    numRows = .Cells(.Rows.count, 3).End(xlUp).Row - 1

    '// Same logic for the columns
    numCols = .Cells(2, .Columns.count).End(xlToLeft).Column - 1

    '// Never need to ".Select" anything
    'Range("C2").Select

    'Dim discount, margin As Double (See first comment)

    For x = 1 To numRows - 1

      '// Just use x to determine the row number.
      'discount = ActiveCell.Offset(0, -1).value
      discount = .Cells(x + 1, 2).value

        For y = 1 To numCols
            margin = .Cells(x + 1, 3).Offset(numRows - x, y - 1).value

            If margin - discount <= 0.0001 Then
                .Cells(x + 1, 3).Offset(0, y - 1).value = ""
            Else
                .Cells(x + 1, 3).Offset(0, y - 1).value = discount / (margin - discount)
            End If
        Next

    Next

End With

Next

End Sub