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...
ThanksSub 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
Alex
Last edited by ap1980; 01-11-2012 at 09:25 AM.
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.
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
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)"
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.
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
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.
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
Hmm, snb you'll have to explain that one. Never heard of that before.
Thanks
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
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks