Count 1 with VBA

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000

Hi,

I am looking VBA that can count 1 from the specific point, the idea is simple add +1 if find next character 1 is followed by 1 subtract if next cheater is followed by either X or 2

Example sheet1 there are my data in column C,
Step-1 check C6 if it is not 1 result in E6= -1, check C7 if it is not 1 result in E7= -2, check C8 if it is not 1 result in E8= -3, check C9 if it is not 1 result in E9= -4, check C10 if it is find 1 result in E11= -3, for this I am using formulas. E6=IF(C6=1,1,-1) , E7=IF(C7=1,E6+1,IF(C7="X",E6-1,IF(C7=2,E6-1))) Copied to down.

Book1
ABCDEF
1
2
3
4
5JorTempP1P1
6X-1
7X-2
8X-3
92-4
101-3
11X-4
12X-5
131-4
141-3
151-2
16X-3
171-2
181-1
19X-2
201-1
2110
222-1
2310
24X-1
2510
26X-1
2710
2811
2912
3013
3114
3215
3324
3415
3524
3615
37X4
38X3
39X2
4013
41X2
4221
43X0
44X-1
45
Sheet1


This is what I want as shown in the below sheet2 VBA solution for example in the column D where ever I mark 1 counting start from that row to down as shown below

Book1
ABCDEF
1
2
3
4
5JorTempP1P1
6X
7X
8X
92
101
11X
12X
131
141
151
16X
171
181
19X
201
211
222
231
24X
25111
26X0
2711
2812
2913
3014
3115
3216
3325
3416
3525
3616
37X5
38X4
39X3
4022
41X1
4220
43X-1
44X-2
45
Sheet2



Thank you in advance

Regards,
Kishan
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Nov09
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Rw = Range("D:D").Find(1, LookIn:=xlValues).Row
[COLOR="Navy"]Set[/COLOR] Rng = Range("C" & Rw, Range("C" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
[COLOR="Navy"]If[/COLOR] Dn.Address = Rng(1).Address [COLOR="Navy"]Then[/COLOR]
    Dn.Offset(, 2).Value = IIf(Dn.Value = 1, 1, -1)
[COLOR="Navy"]Else[/COLOR]
    Dn.Offset(, 2).Value = IIf(Dn.Value = 1, Dn.Offset(-1, 2).Value + 1, Dn.Offset(-1, 2).Value - 1)
[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] MG12Nov09
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, Rw [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
Rw = Range("D:D").Find(1, LookIn:=xlValues).Row
[COLOR=navy]Set[/COLOR] Rng = Range("C" & Rw, Range("C" & Rows.Count).End(xlUp))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
[COLOR=navy]If[/COLOR] Dn.Address = Rng(1).Address [COLOR=navy]Then[/COLOR]
    Dn.Offset(, 2).Value = IIf(Dn.Value = 1, 1, -1)
[COLOR=navy]Else[/COLOR]
    Dn.Offset(, 2).Value = IIf(Dn.Value = 1, Dn.Offset(-1, 2).Value + 1, Dn.Offset(-1, 2).Value - 1)
[COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
Thanks MickG,

This is doing exactly what I require. I checked it and it works as demand.

Regards,
Kishan

 
Upvote 0

Forum statistics

Threads
1,224,748
Messages
6,180,721
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