vba. about looping to optimize code.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
986
Office Version
  1. 2010
Platform
  1. Windows
Hi All.
Working on
VBA Code:
Sub j()
    Range("B2").Formula = "=Countif(Q2:WAK2)"
    Range("C2").Formula = "=Max(Q2:WAK2)"
    Range("D2").Formula = "=Countif(Q2:WAK2, 0)"
    Range("E2").Formula = "=COUNTIF(Q2:WAK2, 1)"
    Range("F2").Formula = "=COUNTIF(Q2:WAK2, 2)"
    Range("G2").Formula = "=COUNTIF(Q2:WAK2, 3)"
    Range("H2").Formula = "=COUNTIF(Q2:WAK2, 4)"
    Range("I2").Formula = "=COUNTIF(Q2:WAK2, 5)"
    Range("J2").Formula = "=COUNTIF(Q2:WAK2, 6)"
    Range("K2").Formula = "=Average(Q2:WAK2)"
End Sub
How is possible to make shorter.
thanks.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hardly worth the effort though !!
Note the error in the formula on line 2 of your code !
VBA Code:
Sub j()
Dim n As Integer, c As Integer
    Range("B2").Formula = "=Countif(Q2:WAK2)" 
    Range("C2").Formula = "=Max(Q2:WAK2)"
    Range("K2").Formula = "=Average(Q2:WAK2)"
    n = 0
    For c = 4 To 11
        Cells(2, c).Formula = "=Countif(Q2:WAK2," & n & ")"
        n = n + 1
    Next c
End Sub
 
Upvote 0
Noting Michael's comment about an invalid formula in your post, I've replaced it with a guessed formula. You can enter them all at once with a one-liner like this

VBA Code:
Sub j_1()
  Range("B2:K2").Formula = Array("=Countif(Q2:WAK2,-1)", "=Max(Q2:WAK2)", "=Countif(Q2:WAK2, 0)", "=COUNTIF(Q2:WAK2, 1)", "=COUNTIF(Q2:WAK2, 2)", "=COUNTIF(Q2:WAK2, 3)", "=COUNTIF(Q2:WAK2, 4)", "=COUNTIF(Q2:WAK2, 5)", "=COUNTIF(Q2:WAK2, 6)", "=Average(Q2:WAK2)")
End Sub
 
Upvote 0
Hardly worth the effort though !!
Note the error in the formula on line 2 of your code !
VBA Code:
Sub j()
Dim n As Integer, c As Integer
    Range("B2").Formula = "=Countif(Q2:WAK2)"
    Range("C2").Formula = "=Max(Q2:WAK2)"
    Range("K2").Formula = "=Average(Q2:WAK2)"
    n = 0
    For c = 4 To 11
        Cells(2, c).Formula = "=Countif(Q2:WAK2," & n & ")"
        n = n + 1
    Next c
End Sub
Hello, Michael M: thanks for your time
when I run the code this is what happen
1621169811121.png

1621169852244.png
 
Upvote 0
Thank you Peter SSs for your time
this is what I get from your code
1621169996939.png


and this is what I am looking for
1621170095469.png

the difference is the "B" I didn't get any return on "count"
Thank you Peter
 
Upvote 0
the difference is the "B" I didn't get any return on "count"
That is because we do not know exactly what formula you want in column B. You showed us "=Countif(Q2:WAK2)" but that is not a valid formula.
 
Upvote 0
Not wanting to interfere in an already busy thread, I would just make an observation that there is one further difference between preferred/actual outcomes that doesn't appear to be getting picked up (and it's a very minor one) - the layout.

In the actual outcome, the average is in column K, whereas it appears from screen capture 2 that you prefer it to be in column L. Your original code actually puts the AVERAGE formula in column K, and Peter_SSs's array method properly reflects that. If, however, you want it to appear in Column L (as your screen capture suggests), this can be easily accomplished by slightly adjusting the range and including an empty string:
VBA Code:
Range("B2:L2").Formula = Array("=Countif(Q2:WAK2,-1)", "=Max(Q2:WAK2)", "=Countif(Q2:WAK2, 0)", "=COUNTIF(Q2:WAK2, 1)", "=COUNTIF(Q2:WAK2, 2)", "=COUNTIF(Q2:WAK2, 3)", "=COUNTIF(Q2:WAK2, 4)", "=COUNTIF(Q2:WAK2, 5)", "=COUNTIF(Q2:WAK2, 6)", "", "=Average(Q2:WAK2)"

Again, like Peter's, this code stills include the problematic formula(s), which I assume is being addressed.
 
Upvote 0
Thank you Peter SSs, you are right I must have

►Range("B2").Formula = "=COUNT(Q2:WAK2)"◄ ["sorry Peter"]

Dan_W Thank you. interfere ? never(y), you are more than welcome
and thank you for your idea.

Now everything is working.

Thank you for the great lesson all of you.
 
Upvote 0
According to your initial post and your corrected formula so maybe :​
VBA Code:
Sub Demo1()
    Const A = "Q2:WAK2", C = "=COUNTIF(" & A & ","
    [B2:K2].Formula = Split(Replace(Replace("=COUNT(¤) =MAX(¤) #,0) #,1) #,2) #,3) #,4) #,5) #,6) =AVERAGE(¤)", "¤", A), "#", C))
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,714
Members
452,995
Latest member
isldboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top