简体   繁体   中英

How to optimize the time of a process in excel VBA MMult

I want to do a loop for multiply matrixes, but I keep getting interrupted by a long time of the process.

I attach the code:

    Call back_setup_OFF


n_col = 105
dia = 1

Do

dia = dia + 1
Hoja7.Cells(2, n_col) = dia
Hoja7.Cells(105, n_col) = dia

    Do

        n_row = 106

        If Hoja7.Cells(n_row + 103, 5) >= dia Then
            J_usar = Hoja7.Cells(n_row + 103, 3)
        Else
            J_usar = 0
        End If


    Hoja7.Cells(n_row, n_col) = -Hoja7.Cells(n_row + 103, 2) * Hoja7.Cells(n_row - 103, n_col - 1) - J_usar * Hoja7.Cells(n_row + 103, 4)

    n_row = n_row + 1

    Loop Until n_row = 206
    
Hoja7.Range(Hoja7.Cells(3, n_col), Hoja7.Cells(102, n_col)) = Hoja7.Application.WorksheetFunction.MMult(Hoja7.Range("B106:CW205"), Hoja7.Range(Hoja7.Cells(106, n_col), Hoja7.Cells(205, n_col)))

n_col = n_col + 1

Loop Until dia = 100

Call back_setup

And the back_setup and back_setup_OFF

Sub back_setup()

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.StatusBar = False
End Sub

Sub back_setup_OFF()


Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = False

End Sub

The problem is that Excel closes itself for the long process, any idea?

You reset n_row each time through the loop so it will only ever get to 107...

Do
    n_row = 106 '<<<<<< ???

    If Hoja7.Cells(n_row + 103, 5) >= dia Then
        J_usar = Hoja7.Cells(n_row + 103, 3)
    Else
        J_usar = 0
    End If

    Hoja7.Cells(n_row, n_col) = -Hoja7.Cells(n_row + 103, 2) * Hoja7.Cells(n_row - 103, n_col - 1) - J_usar * Hoja7.Cells(n_row + 103, 4)

    n_row = n_row + 1

Loop Until n_row = 206

FYI you have fixed start/end points and a fixed increment, but you are using Do...Loop - you should probably be using a For...Next loop instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM