2017-12-25 31 views
0

클래스에 추가하는 경우 느려집니다 : 100100 000Clients와 배열을 생성하고 배열을 통해 루프마다 설정 서로 다른 난수를 각 Client에 입력하십시오.엑셀 VBA : 같은 코드를 얻을 속성이 나는 <code>Client</code> 클래스를 사용합니다 메인 서브를

Sub start() 

    Application.ScreenUpdating = False 

    Dim i As Long 
    Dim j As Long 

    Dim clientsColl() As Client 
    ReDim clientsColl(1 To 100000) As Client 

    For j = 1 To 100000 
     Set clientsColl(j) = New Client 
     clientsColl(j).setClientName = "Client_" & j 

     Application.StatusBar = "Getting client " & j 

     DoEvents 
    Next 

    Dim clientCopy As Variant 
    For i = 1 To 100 
     For Each clientCopy In clientsColl 
      clientCopy.generateRandom 
     Next 

     Application.StatusBar = "Calculating " & i 

     DoEvents 
    Next 

    MsgBox ("done") 

    Application.StatusBar = False 
    Application.ScreenUpdating = True 

End Sub 

클라이언트 클래스 :

Option Explicit 

Private clientname As String 
Private identityNumber As String 
Private creditRating As String 

Private contractTenor As Long 
Private contractNumber As String 
Private contractRate As Double 

Private totalReserves As Double 
Private totalReservesRate As Double 

Private debtType As String 
Private totalDebt As Double 

Private lossRatio As Double 
Private totalLoss As Variant 
Private totalProfit As Double 

Private totalPd As Double 
Private totalLgd As Double 

Private simulationCount As Long 
Private randomNumber As Double 
Private outcome As Integer 

Private loss As Double 
Private profit As Double 

Private sumLosses() As Double 
Private sumProfits() As Double 
Private sumResults() As Double 

Private averageDebtInfo As Double 

Public Sub generateRandom() 
    randomNumber = Rnd() 
End Sub 

Public Property Get getClientName() 
    getClientName = clientName 
End Property 

Public Property Let setClientName(value As String) 
    clientName = value 
End Property 

그러나,이 코드는 Client 클래스는 GetLet 특성을 가지고 여부에 따라 실행하는 다른 시간이 걸립니다. 위에 게시 된 클래스의 현재 버전은 약 25 초에 실행됩니다. identityNumber 또는 contractRate과 같은 것을 얻으려면 몇 가지 Get 속성을 추가하면 약 1 minute 25 seconds이 걸립니다.

왜 간단한 코드를 추가하면 코드에 많은 영향을 줍니까? 그것에 대해 아무 것도 할 수 있습니까? 이 코드는 30 초 이상 실행될 수 없습니다.

답변

0

다음 코드를 사용하여 매크로를 실행하는 동안 응용 프로그램 기능을 비활성화 할 수 있습니다.

참고 :

http://www.vbaexpress.com/kb/getarticle.php?kb_id=1035
https://wellsr.com/vba/2017/excel/speed-up-VBA-macro-with-these-subroutines/

Sub SpeedOff() 
    'Turns on the time wasters 
    With Application 
     .Calculation = xlCalculationAutomatic 
     .ScreenUpdating = True 
     .EnableEvents = True 
     .DisplayAlerts = True 
     .Cursor = xlDefault 
     .StatusBar = False 
     .EnableCancelKey = xlInterrupt 
    End With 
End Sub 

Sub SpeedOn() 
    'Turns off the time wasters 
    With Application 
     .Calculation = xlCalculationManual 
     .ScreenUpdating = False 
     .EnableEvents = False 
     .DisplayAlerts = False 
     .Cursor = xlWait 
     .EnableCancelKey = xlErrorHandler 
    End With 
End Sub 
+0

이 확실히 도움이되는 정보이지만 명시된 변경이 전체 실행 시간에 영향을 미치지 않는 이유의 질문에 대답 도움이되지 않습니다. –