+ Reply to Thread
Results 1 to 11 of 11

Thread: Speed up code

  1. #1
    Registered User
    Join Date
    01-11-2012
    Location
    Newbury
    MS-Off Ver
    Excel 2010
    Posts
    4

    Speed up code

    Hello all,

    I have a piece of vba code that puts various formulas into cells in the second row of a spreadsheet and then copies and pastes them to all the cells bellow. Is there any other way I can speed this up as it's taking around 10-20 seconds for a few thousand rows...

    Sub CONCURRENCY_Concurrency_Click()
    
    Dim T1 As Date
    T1 = Now()
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    Application.DisplayStatusBar = False
    
    Select Case True
        Case Range("B2").Value = vbNullString
        'DO NOTHING
        Case Else
    
        'Devices
        Range("K2").Value = "=CONCATENATE(B2,$H2)"
        'Package
        Range("L2").Value = "=CONCATENATE(C2,$H2)"
        'ParentPackage
        Range("M2").Value = "=CONCATENATE(D2,$H2)"
        'Users
        Range("N2").Value = "=CONCATENATE(E2,$H2)"
        'Conc. Sessions
        Range("O2").Value = "=IF(O1=""Conc. Sessions"",1,IF(H2=""S"",O1+1,O1-1))"
        'Conc. Devices
        Range("P2").Value = "=IF(P1=""Conc. Devices"",1,IF(AND(H2=""S"",K2=K1),P1,IF(AND(H2=""S"",K2<>K1,COUNTIF($K1:K$2,K2)=COUNTIF($K1:K$2,U2)),P1+1,IF(AND(H2=""S"",K2<>K1,COUNTIF($K$2:K2,K2)>1),P1,IF(AND(H2=""E"",COUNTIF($K$2:K2,K2)=COUNTIF($K$2:K2,U2)),P1-1,IF(AND(H2=""E"",COUNTIF($K$2:K2,K2)<>COUNTIF($K$2:K2,U2)),P1,0))))))"
        'Conc. Packages
        Range("Q2").Value = "=IF(Q1=""Conc. Packages"",1,IF(AND(H2=""S"",L2=L1),Q1,IF(AND(H2=""S"",L2<>L1,COUNTIF($L1:L$2,L2)=COUNTIF($L1:L$2,V2)),Q1+1,IF(AND(H2=""S"",L2<>L1,COUNTIF($L$2:L2,L2)>1),Q1,IF(AND(H2=""E"",COUNTIF($L$2:L2,L2)=COUNTIF($L$2:L2,V2)),Q1-1,IF(AND(H2=""E"",COUNTIF($L$2:L2,L2)<>COUNTIF($L$2:L2,V2)),Q1,0))))))"
        'Conc. ParentPackages
        Range("R2").Value = "=IF(R1=""Conc. Parent Packages"",1,IF(AND(H2=""S"",M2=M1),R1,IF(AND(H2=""S"",M2<>M1,COUNTIF($M1:M$2,M2)=COUNTIF($M1:M$2,W2)),R1+1,IF(AND(H2=""S"",M2<>M1,COUNTIF($M$2:M2,M2)>1),R1,IF(AND(H2=""E"",COUNTIF($M$2:M2,M2)=COUNTIF($M$2:M2,W2)),R1-1,IF(AND(H2=""E"",COUNTIF($M$2:M2,M2)<>COUNTIF($M$2:M2,W2)),R1,0))))))"
        'Conc. Users
        Range("S2").Value = "=IF(S1=""Conc. Users"",1,IF(AND(H2=""S"",N2=N1),S1,IF(AND(H2=""S"",N2<>N1,COUNTIF($N1:N$2,N2)=COUNTIF($N1:N$2,X2)),S1+1,IF(AND(H2=""S"",N2<>N1,COUNTIF($N$2:N2,N2)>1),S1,IF(AND(H2=""E"",COUNTIF($N$2:N2,N2)=COUNTIF($N$2:N2,X2)),S1-1,IF(AND(H2=""E"",COUNTIF($N$2:N2,N2)<>COUNTIF($N$2:N2,X2)),S1,0))))))"
        
        'Inverse S\E
        Range("T2").Value = "=IF(H2=""S"",""E"",""S"")"
        'Inverse Devices
        Range("U2").Value = "=CONCATENATE(B2,$T2)"
        'Inverse Packages
        Range("V2").Value = "=CONCATENATE(C2,$T2)"
        'Inverse ParentPackages
        Range("W2").Value = "=CONCATENATE(D2,$T2)"
        'Inverse Users
        Range("X2").Value = "=CONCATENATE(E2,$T2)"
        
        Range("K2:X2").Copy Destination:=Range("K2", Range("B2").End(xlDown).Offset(0, 22))
        
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
        Application.DisplayStatusBar = True
        
        MsgBox Format(Now() - T1, "s") & " Seconds"
    End Select
    End Sub
    Thanks

    Alex
    Last edited by ap1980; 01-11-2012 at 09:25 AM.

  2. #2
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: How can I speed up this code?

    At first sight, that would appear not unreasonable as you seem to have quite a lot of calculation to do. Your formulas for K:N, P:S and U:X appear to be the same (in R1C1 terms) for each group of columns, so you could enter them in one hit rather than a separate entry for each column, but I do not think that will make a lot of difference here.
    Good luck.

  3. #3
    Registered User
    Join Date
    01-11-2012
    Location
    Newbury
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: How can I speed up this code?

    Hi,

    Thanks for that. I can understand why it takes so long I just thought there may be other tricks that I am not aware of as I'm not very techie when it comes to vba. Thanks

  4. #4
    Forum Guru Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    Excel 2010
    Posts
    1,249

    Re: How can I speed up this code?

    I don't know if this will speed it up any, but it seems odd to me that you are setting a formula to a value.

    Range("K2").Formula= "=CONCATENATE(B2,$H2)"

  5. #5
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: How can I speed up this code?

    The only thing I can think that you might try is to reorder the last few lines to
       
        with Application
          .Calculation = xlCalculationAutomatic
          .EnableEvents = True
          .DisplayStatusBar = True
          .ScreenUpdating = True
         End With
    Good luck.

  6. #6
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: How can I speed up this code?

    It looks like it could be made a lot faster. But, it would really help if you could post a dummy file with the formulas in place?
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  7. #7
    Forum Guru Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    Excel 2010
    Posts
    1,249

    Re: How can I speed up this code?

    The slowness seems to come when the calculation is set back to automatic.

    I recommend you read this article http://www.excelhero.com/blog/2010/01/i-heart-if.html and re-write your formulas to be more efficient.

  8. #8
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: How can I speed up this code?

    Did you consider :

    Sub snb()
        [K1:N100] = [index(B1:E100,0,column(K1:N100)-11) & H1:H100]
        [U1:X100] = [index(B1:E100,0,column(K1:N100)-11) & if(H1:H100="S","E","S")]
    End Sub



  9. #9
    Registered User
    Join Date
    01-11-2012
    Location
    Newbury
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: How can I speed up this code?

    Hmm, snb you'll have to explain that one. Never heard of that before.

    Thanks

  10. #10
    Valued Forum Contributor JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    Japan
    MS-Off Ver
    Excel 2010
    Posts
    779

    Re: Speed up code

    Did you try to run the sub snb did for you?
    If you are happy with the answer, please click the Star icon in the below left hand corner.

    Good sites to start learning.

    snb's VBA Help Files
    Jerry Beaucaires Excel Assistant
    J & R Excel Consultancy Services

    How to post code correctly: Correct Code Posting

  11. #11
    Registered User
    Join Date
    01-11-2012
    Location
    Newbury
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Speed up code

    Hi all,

    I've amended the code a bit using the methods described in link Whizbang provided but the code still takes about the same time. I think it's literally just because of the 4 big formulas that need to be copied down.

    Sub CONCURRENCY_Concurrency_Click()
    
    Dim T1 As Date
    
    
    T1 = Now()
    
    With Application
          .Calculation = xlCalculationManual
          .EnableEvents = False
          .ScreenUpdating = False
    End With
    
    
    Select Case True
        Case Range("B2").Value = vbNullString
        'DO NOTHING
        Case Else
    
        'Devices
        Range("K2").Value = "=CONCATENATE(B2,$H2)"
        'Package
        Range("L2").Value = "=CONCATENATE(C2,$H2)"
        'ParentPackage
        Range("M2").Value = "=CONCATENATE(D2,$H2)"
        'Users
        Range("N2").Value = "=CONCATENATE(E2,$H2)"
    
        
        'FIRST ROW
        Range("O2:S2").Value = 1
        
        'Conc. Sessions
        Range("O3").Value = "=((H3=""S"")*(O2+1)) + ((H3<>""S"")*(O2-1))"
            
        'Conc. Devices
        Range("P3").Value = "=OR(AND(H3=""S"",K3=K2),AND(H3=""S"",K3<>K2,COUNTIF($K$2:K2,K3)<>COUNTIF($K$2:K2,U3)),AND(H3=""E"",COUNTIF($K$2:K3,K3)<>COUNTIF($K$2:K3,U3)))*P2 + AND(H3=""S"",K3<>K2,COUNTIF($K$2:K2,K3)=COUNTIF($K$2:K2,U3))*(P2+1) + AND(H3=""E"",COUNTIF($K$2:K3,K3)=COUNTIF($K$2:K3,U3))*(P2-1)"
        
        'Conc. Packages
        Range("Q3").Value = "=OR(AND(H3=""S"",L3=L2),AND(H3=""S"",L3<>L2,COUNTIF($L$2:L2,L3)<>COUNTIF($L$2:L2,V3)),AND(H3=""E"",COUNTIF($L$2:L3,L3)<>COUNTIF($L$2:L3,V3)))*Q2 + AND(H3=""S"",L3<>L2,COUNTIF($L$2:L2,L3)=COUNTIF($L$2:L2,V3))*(Q2+1) + AND(H3=""E"",COUNTIF($L$2:L3,L3)=COUNTIF($L$2:L3,V3))*(Q2-1)"
        
        'Conc. ParentPackages
        Range("R3").Value = "=OR(AND(H3=""S"",M3=M2),AND(H3=""S"",M3<>M2,COUNTIF($M$2:M2,M3)<>COUNTIF($M$2:M2,W3)),AND(H3=""E"",COUNTIF($M$2:M3,M3)<>COUNTIF($M$2:M3,W3)))*R2 + AND(H3=""S"",M3<>M2,COUNTIF($M$2:M2,M3)=COUNTIF($M$2:M2,W3))*(R2+1) + AND(H3=""E"",COUNTIF($M$2:M3,M3)=COUNTIF($M$2:M3,W3))*(R2-1)"
        
        'Conc. Users
        Range("S3").Value = "=OR(AND(H3=""S"",N3=N2),AND(H3=""S"",N3<>N2,COUNTIF($N$2:N2,N3)<>COUNTIF($N$2:N2,X3)),AND(H3=""E"",COUNTIF($N$2:N3,N3)<>COUNTIF($N$2:N3,X3)))*S2 + AND(H3=""S"",N3<>N2,COUNTIF($N$2:N2,N3)=COUNTIF($N$2:N2,X3))*(S2+1) + AND(H3=""E"",COUNTIF($N$2:N3,N3)=COUNTIF($N$2:N3,X3))*(S2-1)"
    
    
        'Inverse S\E
        Range("T2").Value = "=IF(H2=""S"",""E"",""S"")"
        'Inverse Devices
        Range("U2").Value = "=CONCATENATE(B2,$T2)"
        'Inverse Packages
        Range("V2").Value = "=CONCATENATE(C2,$T2)"
        'Inverse ParentPackages
        Range("W2").Value = "=CONCATENATE(D2,$T2)"
        'Inverse Users
        Range("X2").Value = "=CONCATENATE(E2,$T2)"
        
        Range("K2:N2").Copy Destination:=Range("K2", Range("B2").End(xlDown).Offset(0, 12))
        Range("O3:S3").Copy Destination:=Range("O3", Range("B2").End(xlDown).Offset(0, 17))
        Range("T2:X2").Copy Destination:=Range("T2", Range("B2").End(xlDown).Offset(0, 22))
        
    With Application
          .Calculation = xlCalculationAutomatic
          .EnableEvents = True
          .ScreenUpdating = True
    End With
        
    MsgBox Format(Now() - T1, "s") & " Seconds"
        
        
    End Select
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0