DIGIT-ARC read-only archive · snapshot: July 23, 2026

need logic in vba

12 posts · originally from https://geek.digit.in/community/threads/need-logic-in-vba.140662/

A
arian29 Original post #1
May 12, 2011 at 11:51 AM
i need the logic in vba(using any loops) for this >

if col A has repeted vales then col B should have the first instance value for such repetations as x and rest as y. basically i need to populate col B in this way, col A is only present.

eg,

A B
2 x
2 y
1 y
3 x
3 y
3 y
asingh
asingh #2
May 12, 2011 at 12:04 PM
^^
The first instance of 2 = x, for 1 = y, how can 3 = y again.

Give me the logic more clear, will help.
A
arian29 #3
May 12, 2011 at 12:08 PM
No its not that 2 = anything. Nothing is equal to anything. There are 2 columns A and B. Column A is already populated with the given numbers. some numbers are repeted like number 2 and 3. 2 is always repeted twice and 3 is repeted thrice. now i want x in column B , against the first instance of 2 or 3 and y against the rest of the instances. So i am filling column B.
its in vba (excel)
Thanks
asingh
asingh #4
May 12, 2011 at 12:16 PM
^^
But this logic will not apply to the first instance of '1'..?

Only for '2' and '3'.
A
arian29 #5
May 12, 2011 at 12:20 PM
ya.. actually the data in column A is like

1
2
2
1
1
1
1
3
3
3
1
1
2
2
1
2
2
1
1
3
3
3
1
1
1
3
3
3
1
2
2


like this.. so i need x against the 'first' instance of 2 and 3. getting y against 1 is easy as there is only one instance but having problem with 2 and 3. rest of the instances will have a y.
A
arian29 #7
May 12, 2011 at 12:30 PM
you can have x or y against 1, dosent matter, prob is with 2 and 3.
asingh
asingh #8
May 12, 2011 at 1:46 PM
Would a formulaic approach work here......

I pasted the data from post number 5 on a range of D10 : D40.

Then I wrote this formula in E10:
=IF(COUNTIF($D$10 : D10,D10)=1,"x","y")

Once you type the last bracket, press CONTROL+SHIFT+ENTER, and the formula will get {} around it.

Now double click down..>!
A
arian29 #9
May 12, 2011 at 2:44 PM
no its not working..
Zangetsu
Zangetsu #10
May 12, 2011 at 3:08 PM
you can have x or y against 1, dosent matter, prob is with 2 and 3.

sorry i m n00b in vba.But
the logic will be like this

consider range of column A is A1:A20 => output will be B1:B20

start from A1 -> store it in a temp variable
Read A2 cell value & compare it with the temp variable if = then put y else put x
this will go on till A20
temp variable will always store the previous cell value
abhidev
abhidev #11
May 12, 2011 at 4:26 PM
I don't think this logic will work...coz what happens if any of those numbers repeat in the sequence....e.g 1 2 2 2 3 3 3 1 1 1 2 2 2....correct me if I am wrong...the output for this sequence should be x x y y x y y y y y y y y ...right?
asingh
asingh #12
May 12, 2011 at 4:53 PM
no its not working..

Yes, because you did not follow the way I asked you to.

sorry i m n00b in vba.But
the logic will be like this

consider range of column A is A1:A20 => output will be B1:B20

start from A1 -> store it in a temp variable
Read A2 cell value & compare it with the temp variable if = then put y else put x
this will go on till A20
temp variable will always store the previous cell value

There is absolutely no USE for VBA for this. VBA is used only when native excel cannot do the job.

Please see the sheet I have attached. Look at the yellow cells, and understand the formula I wrote. It is an array function, and does the job perfect.

Sample.xls - 4shared.com - online file sharing and storage - download

And I repeat again, there is no need to do this with VBA. It is shooting a fly with a cannon. Unless your data is crossing 50,000 rows or something.