Count upper values only if the line below has 0

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,354
Office Version
  1. 2010
Hello,</SPAN></SPAN>

I need a VBA solution, which can count upper value only if the line below has 0. Result in column S</SPAN></SPAN>

Example counts of the row 7 values, only if the lower row 8 has 0...Row 8 has 5 0's over it in the row 7 has numbers 0, 1, 3 & 4 (0=2, 1=1, 3=3 & 4=1) result S7 = 2 | 1 | 1 | 1, and the count method will be applied the same for rest of the rows.</SPAN></SPAN>

Here is an example....</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRS
1
2
3
4
5n1n2n3n4n5n6n7n8n9n10n11n12n13n14Count 0 To Increment Values
6
7411060401350212 | 1 | 1 | 1
8502171002060323
9603282003171431 | 1 | 1
10710093114282041 | 1 | 1
11821004205393151 | 1 | 1 | 1
129321153060104001 | 1 | 1
131043026417100111
141154137528011221 | 2 | 1 | 1 | 1
15005208639102002 | 2 | 2 | 1 | 1
16106019700210101 | 1 | 1
172071210811021214 | 1 | 1
18318030920130002 | 1 | 1 | 2 | 1
19029100001040111 | 2 | 1 | 1
201310001112101023 | 2 | 1
210411010223202001 | 1
22150121334013111 | 1 | 1 | 1
23200032405124221 | 1
24310143510235331 | 1 | 2 | 2
25421250001040401
26532361112150511 | 1 | 1 | 1
27603402203261021 | 1 | 1 | 1 | 1
28710003314070131 | 1 | 2
29820110425181001 | 2 | 1 | 1 | 1
30030201006092111 | 1 | 1 | 1
311013101071100221 | 1
32212401218211033
Sheet2


Thank you all</SPAN></SPAN>

Excel 2000</SPAN></SPAN>
Regards,</SPAN></SPAN>
Moti</SPAN></SPAN>
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG03Jan13
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("D7", Range("D" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
   [COLOR="Navy"]If[/COLOR] Not Dn.Address = Rng(Rng.Count).Address [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
        .CompareMode = vbTextCompare

    [COLOR="Navy"]For[/COLOR] Ac = 0 To 13
      [COLOR="Navy"]If[/COLOR] Dn.Offset(1, Ac).Value = 0 [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Offset(, Ac).Value) [COLOR="Navy"]Then[/COLOR]
                .Add Dn.Offset(, Ac).Value, 1
            [COLOR="Navy"]Else[/COLOR]
                .Item(Dn.Offset(, Ac).Value) = .Item(Dn.Offset(, Ac).Value) + 1
            [COLOR="Navy"]End[/COLOR] If
      [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Ac

 ReDim ray(1 To 14, 1 To 2)
 [COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
 c = 0
 [COLOR="Navy"]For[/COLOR] n = 0 To 10
 [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    [COLOR="Navy"]If[/COLOR] K = n [COLOR="Navy"]Then[/COLOR]
        c = c + 1
        ray(c, 1) = K
        ray(c, 2) = .Item(K)
    [COLOR="Navy"]End[/COLOR] If
   [COLOR="Navy"]Next[/COLOR] K
 [COLOR="Navy"]Next[/COLOR] n
 
[COLOR="Navy"]Dim[/COLOR] nStr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
 nStr = ""
 [COLOR="Navy"]For[/COLOR] n = 1 To UBound(ray, 1)
    [COLOR="Navy"]If[/COLOR] Not IsEmpty(ray(n, 1)) [COLOR="Navy"]Then[/COLOR]
        nStr = nStr & IIf(nStr = "", ray(n, 2), "|" & ray(n, 2))
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
    Dn.Offset(, 15) = nStr
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn


[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
Code:
[COLOR=navy]Sub[/COLOR] MG03Jan13
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Ac [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Set[/COLOR] Rng = Range("D7", Range("D" & Rows.Count).End(xlUp))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
   [COLOR=navy]If[/COLOR] Not Dn.Address = Rng(Rng.Count).Address [COLOR=navy]Then[/COLOR]
    [COLOR=navy]With[/COLOR] CreateObject("scripting.dictionary")
        .CompareMode = vbTextCompare

    [COLOR=navy]For[/COLOR] Ac = 0 To 13
      [COLOR=navy]If[/COLOR] Dn.Offset(1, Ac).Value = 0 [COLOR=navy]Then[/COLOR]
            [COLOR=navy]If[/COLOR] Not .Exists(Dn.Offset(, Ac).Value) [COLOR=navy]Then[/COLOR]
                .Add Dn.Offset(, Ac).Value, 1
            [COLOR=navy]Else[/COLOR]
                .Item(Dn.Offset(, Ac).Value) = .Item(Dn.Offset(, Ac).Value) + 1
            [COLOR=navy]End[/COLOR] If
      [COLOR=navy]End[/COLOR] If
    [COLOR=navy]Next[/COLOR] Ac

 ReDim ray(1 To 14, 1 To 2)
 [COLOR=navy]Dim[/COLOR] K [COLOR=navy]As[/COLOR] Variant, c [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
 c = 0
 [COLOR=navy]For[/COLOR] n = 0 To 10
 [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] K [COLOR=navy]In[/COLOR] .keys
    [COLOR=navy]If[/COLOR] K = n [COLOR=navy]Then[/COLOR]
        c = c + 1
        ray(c, 1) = K
        ray(c, 2) = .Item(K)
    [COLOR=navy]End[/COLOR] If
   [COLOR=navy]Next[/COLOR] K
 [COLOR=navy]Next[/COLOR] n
 
[COLOR=navy]Dim[/COLOR] nStr [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
 nStr = ""
 [COLOR=navy]For[/COLOR] n = 1 To UBound(ray, 1)
    [COLOR=navy]If[/COLOR] Not IsEmpty(ray(n, 1)) [COLOR=navy]Then[/COLOR]
        nStr = nStr & IIf(nStr = "", ray(n, 2), "|" & ray(n, 2))
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] n
    Dn.Offset(, 15) = nStr
[COLOR=navy]End[/COLOR] With
[COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] Dn


[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
MickG, the VBA is workings as treat. Thank you very much for the help!</SPAN></SPAN>

Kind Regards,</SPAN></SPAN>
Moti :biggrin:</SPAN></SPAN>
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,243
Members
448,555
Latest member
RobertJones1986

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